Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA1861: Do not warn if not an array initializer. #6714

Merged
merged 1 commit into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public override void Initialize(AnalysisContext context)
}

// Must be literal array
if (arrayCreationOperation.Initializer is { } initializer &&
if (arrayCreationOperation.Initializer is not { } initializer ||
initializer.ElementValues.Any(x => x is not ILiteralOperation))
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,28 +806,34 @@ private void M(string eventName, string msg)
}

[Fact, WorkItem(6686, "https://github.com/dotnet/roslyn-analyzers/issues/6686")]
public Task ArrayWithoutInitializer_Diagnostic()
public Task ArrayWithoutInitializer_NoDiagnostic()
{
var source = @"using System.Collections.Generic;
return new VerifyCS.Test
{
TestCode = @"using System.Collections.Generic;

public class MyClass
{
public List<object> Cases => new() { {|CA1861:new object[0]|} };
}";
var fixedSource = @"using System.Collections.Generic;
public List<object> Cases => new() { new object[0] };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should trigger CA1825 (https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1825). Do we know why it doesn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is expected for unit testing, I did not see any unrelated diagnostics triggered in unit testing except compiler warnings. By my understanding the unit tests only trigger the diagnostics related to:

using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.NetCore.Analyzers.Runtime.AvoidConstArraysAnalyzer,

In normal case the CA1825 is being triggered:
image

}",
LanguageVersion = LanguageVersion.CSharp10
}.RunAsync();
}

[Fact, WorkItem(6686, "https://github.com/dotnet/roslyn-analyzers/issues/6697")]
public async Task ArrayWithoutInitializer_NoDiagnostic2()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;

public class MyClass
{
public List<object> Cases => new() { item };

private static readonly object[] item = new object[0];
}";
return new VerifyCS.Test
{
TestCode = source,
FixedCode = fixedSource,
LanguageVersion = LanguageVersion.CSharp10
}.RunAsync();
public void M1(Type[] types) { }
public void M2(int length)
{
M1(new Type[length]);
}
}");
}
}
}