From 5595184e1a89f55b1e3f9bed1fbea4bc86df4610 Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Mon, 19 Jun 2023 16:21:21 +0530 Subject: [PATCH 1/2] Handle null initializer for array creation operation Fixes #6686 --- .../Runtime/AvoidConstArrays.cs | 5 ++-- .../Runtime/AvoidConstArraysTests.cs | 27 ++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs index 9b1e46274b..d2836db873 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. using System.Linq; using System.Collections.Generic; @@ -103,7 +103,8 @@ public override void Initialize(AnalysisContext context) } // Must be literal array - if (arrayCreationOperation.Initializer.ElementValues.Any(x => x is not ILiteralOperation)) + if (arrayCreationOperation.Initializer != null && + arrayCreationOperation.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 c30dd335a2..a529c8f17e 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArraysTests.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. using System.Threading.Tasks; using Microsoft.CodeAnalysis.CSharp; @@ -804,5 +804,30 @@ private void M(string eventName, string msg) LanguageVersion = LanguageVersion.CSharp8 }.RunAsync(); } + + [Fact, WorkItem(6686, "https://github.com/dotnet/roslyn-analyzers/issues/6686")] + public Task ArrayWithoutInitializer_Diagnostic() + { + var source = @"using System.Collections.Generic; + +public class MyClass +{ + public List Cases => new() { {|CA1861:new object[0]|} }; +}"; + var fixedSource = @"using System.Collections.Generic; + +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(); + } } } From 1a70e8976767a2e92745b3cc3e28a10034d0fc5e Mon Sep 17 00:00:00 2001 From: Manish Vasani Date: Mon, 19 Jun 2023 17:09:43 +0530 Subject: [PATCH 2/2] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs Co-authored-by: Youssef Victor --- .../Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs index d2836db873..48cc72f13e 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/AvoidConstArrays.cs @@ -103,8 +103,8 @@ public override void Initialize(AnalysisContext context) } // Must be literal array - if (arrayCreationOperation.Initializer != null && - arrayCreationOperation.Initializer.ElementValues.Any(x => x is not ILiteralOperation)) + if (arrayCreationOperation.Initializer is { } initializer && + initializer.ElementValues.Any(x => x is not ILiteralOperation)) { return; }