Skip to content

Commit 1a36608

Browse files
committed
Fix another fixer
1 parent 6d2b79b commit 1a36608

File tree

5 files changed

+13
-53
lines changed

5 files changed

+13
-53
lines changed

src/NetAnalyzers/CSharp/Microsoft.NetCore.Analyzers/Runtime/CSharpPreferStreamAsyncMemoryOverloads.Fixer.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

3-
using System.Collections.Generic;
3+
using System.Composition;
44
using System.Linq;
55
using Microsoft.CodeAnalysis;
66
using Microsoft.CodeAnalysis.CodeFixes;
@@ -11,7 +11,7 @@
1111

1212
namespace Microsoft.NetCore.CSharp.Analyzers.Runtime
1313
{
14-
[ExportCodeFixProvider(LanguageNames.CSharp)]
14+
[ExportCodeFixProvider(LanguageNames.CSharp), Shared]
1515
public sealed class CSharpPreferStreamAsyncMemoryOverloadsFixer : PreferStreamAsyncMemoryOverloadsFixer
1616
{
1717
protected override SyntaxNode? GetArgumentByPositionOrName(IInvocationOperation invocation, int index, string name, out bool isNamed)
@@ -54,19 +54,6 @@ public sealed class CSharpPreferStreamAsyncMemoryOverloadsFixer : PreferStreamAs
5454
return null;
5555
}
5656

57-
protected override bool IsSystemNamespaceImported(IReadOnlyList<SyntaxNode> importList)
58-
{
59-
foreach (SyntaxNode import in importList)
60-
{
61-
if (import is UsingDirectiveSyntax { Name: IdentifierNameSyntax { Identifier.Text: nameof(System) } })
62-
{
63-
return true;
64-
}
65-
}
66-
67-
return false;
68-
}
69-
7057
protected override bool IsPassingZeroAndBufferLength(SemanticModel model, SyntaxNode bufferValueNode, SyntaxNode offsetValueNode, SyntaxNode countValueNode)
7158
{
7259
// First argument should be an identifier name node

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/PreferStreamAsyncMemoryOverloads.Fixer.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

3-
using System.Collections.Generic;
43
using System.Collections.Immutable;
54
using System.Threading;
65
using System.Threading.Tasks;
6+
using Analyzer.Utilities;
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CodeActions;
99
using Microsoft.CodeAnalysis.CodeFixes;
@@ -30,12 +30,11 @@ namespace Microsoft.NetCore.Analyzers.Runtime
3030
/// </summary>
3131
public abstract class PreferStreamAsyncMemoryOverloadsFixer : CodeFixProvider
3232
{
33+
private static readonly SyntaxAnnotation s_asMemorySymbolAnnotation = new("SymbolId", "System.MemoryExtensions");
34+
3335
// Checks if the argument in the specified index has a name. If it doesn't, returns that arguments. If it does, then looks for the argument using the specified name, and returns it, or null if not found.
3436
protected abstract SyntaxNode? GetArgumentByPositionOrName(IInvocationOperation invocation, int index, string name, out bool isNamed);
3537

36-
// Verifies if a namespace has already been added to the usings/imports list.
37-
protected abstract bool IsSystemNamespaceImported(IReadOnlyList<SyntaxNode> importList);
38-
3938
// Verifies if the user passed `0` as the 1st argument (`offset`) and `buffer.Length` as the 2nd argument (`count`),
4039
// where `buffer` is the name of the variable passed as the 0th argument.
4140
protected abstract bool IsPassingZeroAndBufferLength(SemanticModel model, SyntaxNode bufferValueNode, SyntaxNode offsetValueNode, SyntaxNode countValueNode);
@@ -152,7 +151,7 @@ private Task<Document> FixInvocationAsync(SemanticModel model, Document doc, Syn
152151
SyntaxNode asMemoryInvocationNode = generator.InvocationExpression(
153152
asMemoryExpressionNode,
154153
namedStartNode.WithTriviaFrom(offsetNode),
155-
namedLengthNode.WithTriviaFrom(countNode));
154+
namedLengthNode.WithTriviaFrom(countNode)).WithAddImportsAnnotation().WithAdditionalAnnotations(s_asMemorySymbolAnnotation);
156155

157156
// Generate the new buffer argument, ensuring we include the buffer argument name if the user originally indicated one
158157
replacedInvocationNode = GetNamedArgument(generator, asMemoryInvocationNode, isBufferNamed, "buffer")
@@ -175,14 +174,9 @@ private Task<Document> FixInvocationAsync(SemanticModel model, Document doc, Syn
175174
}
176175

177176
SyntaxNode newInvocationExpression = generator.InvocationExpression(asyncMethodNode, nodeArguments).WithTriviaFrom(streamInstanceNode);
178-
179-
bool containsSystemImport = IsSystemNamespaceImported(generator.GetNamespaceImports(root));
180-
181-
// The invocation needs to be replaced before adding the import/using, it won't work the other way around
182177
SyntaxNode newRoot = generator.ReplaceNode(root, invocation.Syntax, newInvocationExpression.WithTriviaFrom(invocation.Syntax));
183-
SyntaxNode newRootWithImports = containsSystemImport ? newRoot : generator.AddNamespaceImports(newRoot, generator.NamespaceImportDeclaration(nameof(System)));
184178

185-
return Task.FromResult(doc.WithSyntaxRoot(newRootWithImports));
179+
return Task.FromResult(doc.WithSyntaxRoot(newRoot));
186180
}
187181
}
188182
}

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferStreamReadAsyncMemoryOverloadsTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,9 @@ public async void M()
594594
}
595595
}";
596596
string fixedCode = @"
597+
using System;
597598
using System.IO;
598599
using System.Threading;
599-
using System;
600-
601600
class C
602601
{
603602
public async void M()
@@ -906,10 +905,9 @@ End Sub
906905
End Class
907906
";
908907
string fixedCode = @"
908+
Imports System
909909
Imports System.IO
910910
Imports System.Threading
911-
Imports System
912-
913911
Class C
914912
Public Async Sub M()
915913
Using s As FileStream = New FileStream(""path.txt"", FileMode.Create)

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/PreferStreamWriteAsyncMemoryOverloadsTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,9 @@ public async void M()
584584
}
585585
}";
586586
string fixedCode = @"
587+
using System;
587588
using System.IO;
588589
using System.Threading;
589-
using System;
590590
591591
class C
592592
{
@@ -866,10 +866,9 @@ End Sub
866866
End Class
867867
";
868868
string fixedCode = @"
869+
Imports System
869870
Imports System.IO
870871
Imports System.Threading
871-
Imports System
872-
873872
Class C
874873
Public Async Sub M()
875874
Using s As FileStream = File.Open(""path.txt"", FileMode.Open)

src/NetAnalyzers/VisualBasic/Microsoft.NetCore.Analyzers/Runtime/BasicPreferStreamAsyncMemoryOverloads.Fixer.vb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
22

3+
Imports System.Composition
34
Imports Microsoft.CodeAnalysis
45
Imports Microsoft.CodeAnalysis.CodeFixes
56
Imports Microsoft.CodeAnalysis.Editing
@@ -9,7 +10,7 @@ Imports Microsoft.NetCore.Analyzers.Runtime
910

1011
Namespace Microsoft.NetCore.VisualBasic.Analyzers.Runtime
1112

12-
<ExportCodeFixProvider(LanguageNames.VisualBasic)>
13+
<ExportCodeFixProvider(LanguageNames.VisualBasic), [Shared]>
1314
Public NotInheritable Class BasicPreferStreamAsyncMemoryOverloadsFixer
1415

1516
Inherits PreferStreamAsyncMemoryOverloadsFixer
@@ -39,25 +40,6 @@ Namespace Microsoft.NetCore.VisualBasic.Analyzers.Runtime
3940
Return Nothing
4041
End Function
4142

42-
Protected Overrides Function IsSystemNamespaceImported(importList As IReadOnlyList(Of SyntaxNode)) As Boolean
43-
For Each import As SyntaxNode In importList
44-
Dim importsStatement = TryCast(import, ImportsStatementSyntax)
45-
If importsStatement IsNot Nothing Then
46-
For Each clause As ImportsClauseSyntax In importsStatement.ImportsClauses
47-
Dim simpleClause = TryCast(clause, SimpleImportsClauseSyntax)
48-
If simpleClause IsNot Nothing Then
49-
Dim identifier = TryCast(simpleClause.Name, IdentifierNameSyntax)
50-
If identifier IsNot Nothing AndAlso String.Equals(identifier.Identifier.Text, "System", StringComparison.OrdinalIgnoreCase) Then
51-
Return True
52-
End If
53-
End If
54-
Next
55-
End If
56-
Next
57-
58-
Return False
59-
End Function
60-
6143
Protected Overrides Function IsPassingZeroAndBufferLength(model As SemanticModel, bufferValueNode As SyntaxNode, offsetValueNode As SyntaxNode, countValueNode As SyntaxNode) As Boolean
6244
' First argument should be an identifier name node
6345
Dim arg1 = TryCast(bufferValueNode, ArgumentSyntax)

0 commit comments

Comments
 (0)