diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs index 48cc72f13e..eedd089c84 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs @@ -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; diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs index a529c8f17e..4cbabfae25 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs @@ -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 Cases => new() { {|CA1861:new object[0]|} }; -}"; - var fixedSource = @"using System.Collections.Generic; + public List Cases => new() { new object[0] }; +}", + 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 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]); + } +}"); } } }