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

NullReferenceException from CA1861 #6686

Closed
jnyrup opened this issue Jun 14, 2023 · 5 comments · Fixed by #6696
Closed

NullReferenceException from CA1861 #6686

jnyrup opened this issue Jun 14, 2023 · 5 comments · Fixed by #6696
Assignees
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design

Comments

@jnyrup
Copy link

jnyrup commented Jun 14, 2023

Analyzer

Diagnostic ID: CA1861: Avoid constant arrays as arguments

Analyzer source

SDK: Built-in CA analyzers in .NET 8 Preview 5 SDK

Version: 8.0.100-preview.5.23303.2

Describe the bug

When running analysis on the snippet below I get

Error	AD0001	Analyzer 'Microsoft.NetCore.Analyzers.Runtime.AvoidConstArraysAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
Exception occurred with following context:
Compilation: ClassLibrary8
IOperation: ArrayCreation
SyntaxTree: C:\dev\repos\ClassLibrary8\ClassLibrary8\Class1.cs
SyntaxNode: new object[0] [ArrayCreationExpressionSyntax]@[131..144) (6,41)-(6,54)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.NetCore.Analyzers.Runtime.AvoidConstArraysAnalyzer.<>c__DisplayClass5_0.<Initialize>b__1(OperationAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteOperationAction>b__50_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
-----

Suppress the following diagnostics to disable this analyzer: CA1861	ClassLibrary8	C:\dev\repos\ClassLibrary8\ClassLibrary8\ClassLibrary8.csproj	1	Active

Steps To Reproduce

using System.Collections.Generic;

namespace ClassLibrary8;

public class MyClass
{
    public List<object> Cases => new() { new object[0] };
}

Expected behavior

There should not be an AD0001 in the Error List

Actual behavior

There is an AD0001 in the Error List

Additional context

@Evangelink
Copy link
Member

Hi @mavasani,

I am starting to face this issue on our projects. This seems to correlate with the upgrade of Microsoft.Net.Compilers.Toolset package from arcade.

public TestObjectConverter7()
    {
        TestPropertyCtor = typeof(TestProperty).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
    }

error

Error	AD0001	Analyzer 'Microsoft.NetCore.Analyzers.Runtime.AvoidConstArraysAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
Exception occurred with following context:
Compilation: Microsoft.TestPlatform.CommunicationUtilities
IOperation: ArrayCreation
SyntaxTree: C:\src\vstest\src\Microsoft.TestPlatform.CommunicationUtilities\Serialization\TestObjectConverter.cs
SyntaxNode: new Type[0] [ArrayCreationExpressionSyntax]@[4004..4015) (109,117)-(109,128)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.NetCore.Analyzers.Runtime.AvoidConstArraysAnalyzer.<>c__DisplayClass5_0.<Initialize>b__1(OperationAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteOperationAction>b__50_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

Is it just waiting for deployment?

@mavasani
Copy link
Contributor

mavasani commented Jul 12, 2023

Is it just waiting for deployment?

@Evangelink - that is correct. Can you try the latest preview NuGet package: https://dev.azure.com/dnceng/public/_artifacts/feed/dotnet8/NuGet/Microsoft.CodeAnalysis.NetAnalyzers/overview/8.0.0-preview.23360.1?

@Evangelink
Copy link
Member

@mavasani I confirm that works by manually referencing this package. Are analyzers only inserted in SDK or also in VS? For the SDK, do you know in which preview they will get shipped (we current use 8.0.100-preview.5.23303.2 - https://github.com/microsoft/vstest/blob/main/global.json#L3)

@mavasani
Copy link
Contributor

Are analyzers only inserted in SDK or also in VS? For the SDK, do you know in which preview they will get shipped

@buyaa-n?

@buyaa-n
Copy link
Member

buyaa-n commented Jul 12, 2023

It seems the fix built into version is 8.0.0-preview.23319.2. The fix doesn't look like made it into SDK preview 5, though it does look inserted in SDK preview 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Microsoft.CodeAnalysis.NetAnalyzers Bug The product is not behaving according to its current intended design
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants