Skip to content

Commit

Permalink
added exclusion for readonly field assignments, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steveberdy committed Mar 17, 2023
1 parent 8a3cd50 commit fec9a2f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
SyntaxNode node = root.FindNode(context.Span);
SemanticModel model = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
DocumentEditor editor = await DocumentEditor.CreateAsync(document, context.CancellationToken).ConfigureAwait(false);
SyntaxGenerator generator = editor.Generator;

string title = MicrosoftNetCoreAnalyzersResources.AvoidConstArraysCodeFixTitle;
context.RegisterCodeFix(CodeAction.Create(
title,
async c => await ExtractConstArrayAsync(root, node, model, editor, generator, context.Diagnostics.First().Properties, c).ConfigureAwait(false),
async ct => await ExtractConstArrayAsync(root, node, model, editor, editor.Generator,
context.Diagnostics.First().Properties, ct).ConfigureAwait(false),
equivalenceKey: title),
context.Diagnostics);
}
Expand Down Expand Up @@ -98,15 +98,14 @@ private static Task<Document> ExtractConstArrayAsync(SyntaxNode root, SyntaxNode
else
{
// add any extra trivia that was after the original argument
editor.ReplaceNode(node, generator.Argument(identifier).WithTrailingTrivia(arrayArgument.Syntax.GetTrailingTrivia()));
editor.ReplaceNode(node, generator.Argument(identifier).WithTriviaFrom(arrayArgument.Syntax));
}

// Return changed document
return Task.FromResult(editor.GetChangedDocument());
}

private static IArrayCreationOperation GetArrayCreationOperation(SyntaxNode node, SemanticModel model, CancellationToken cancellationToken,
out bool isInvoked)
private static IArrayCreationOperation GetArrayCreationOperation(SyntaxNode node, SemanticModel model, CancellationToken cancellationToken, out bool isInvoked)
{
// The analyzer only passes a diagnostic for two scenarios, each having an IArrayCreationOperation:
// 1. The node is an IArgumentOperation that is a direct parent of an IArrayCreationOperation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public override void Initialize(AnalysisContext context)
string? paramName = null;
if (argumentOperation is not null)
{
IFieldInitializerOperation? fieldInitializer = argumentOperation.GetAncestor<IFieldInitializerOperation>(OperationKind.FieldInitializer);
if (fieldInitializer is not null && fieldInitializer.InitializedFields.Any(x => x.IsReadOnly))
{
return;
}
ITypeSymbol originalDefinition = argumentOperation.Parameter.Type.OriginalDefinition;
// Can't be a ReadOnlySpan, as those are already optimized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ public class A
{
public void B()
{
Console.WriteLine(string.Join("" "", {|CA1861:new[] { ""a"", ""b"" }|} /* test comment */));
Console.WriteLine(string.Join(
""a"",
{|CA1861:new[] { ""b"", ""c"" }|}, /* test comment */
""d""
));
}
}
}
Expand All @@ -237,11 +241,15 @@ namespace Z
{
public class A
{
private static readonly string[] value = new[] { ""a"", ""b"" };
private static readonly string[] values = new[] { ""b"", ""c"" };
public void B()
{
Console.WriteLine(string.Join("" "", value /* test comment */));
Console.WriteLine(string.Join(
""a"",
values, /* test comment */
""d""
));
}
}
}
Expand Down Expand Up @@ -686,6 +694,28 @@ private void C(params bool[] booleans)
}
}
}
");
}

[Fact]
public async Task IgnoreReadonlyFieldAssignment_NoDiagnostic()
{
// Ignore when we're an argument used in a method/constructor that is assigned to a readonly field
await VerifyCS.VerifyAnalyzerAsync(@"
namespace Z
{
public class A
{
private static readonly B s = new B(new string[] { ""a"" });
}
public class B
{
public B(string[] s)
{
}
}
}
");
}
}
Expand Down

0 comments on commit fec9a2f

Please sign in to comment.