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

Fix AD0001 in S1144: Implicit object creation inside collection expression #8024

Closed
akurone opened this issue Sep 17, 2023 · 2 comments · Fixed by #8255
Closed

Fix AD0001 in S1144: Implicit object creation inside collection expression #8024

akurone opened this issue Sep 17, 2023 · 2 comments · Fixed by #8255
Assignees
Labels
Area: C# C# rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: Bug Exceptions and blocking issues during analysis.
Milestone

Comments

@akurone
Copy link

akurone commented Sep 17, 2023

Description

Hi,
I am currently migrating a lib project to .net 8 and I received an exception from UnusedPrivateMember analyzer.
Here is the AD0001 message:

Severity	Code	Description	Project	File	Line	Suppression State
Warning	AD0001	Analyzer 'SonarAnalyzer.Rules.CSharp.UnusedPrivateMember' threw an exception of type 'System.NullReferenceException' with the message 'Object reference not set to an instance of an object.'.
Exception occurred with the following context:
Compilation: SonarAnalyzerUnusedPrivateMemberNullReferenceException
ISymbol: Repro (NamedType)

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Wrappers.ObjectCreationFactory.ImplicitObjectCreation.TypeAsString(SemanticModel semanticModel)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.SafeCSharpSyntaxWalker.SafeVisit(SyntaxNode syntaxNode)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.<>c__DisplayClass24_0.<VisitDeclaringReferences>b__1(SyntaxReference x)
   at System.Linq.Enumerable.All[TSource](IEnumerable`1 source, Func`2 predicate)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.NamedSymbolAction(SonarSymbolReportingContext context, HashSet`1 removableInternalTypes)
   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: S1144, S4487	SonarAnalyzerUnusedPrivateMemberNullReferenceException	D:\tmp\SonarAnalyzerUnusedPrivateMemberNullReferenceException\SonarAnalyzerUnusedPrivateMemberNullReferenceException.csproj	1	Active

Repro steps

Create a new console app using VS 2022 17.8.0 Preview 2.0. Add SonarAnalyzer.CSharp, .csproj would be like:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="SonarAnalyzer.CSharp" Version="9.10.0.77988">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>

Add this file to the project:

public sealed record Line(decimal Field);

public sealed class Repro
{
    public Line[] ThisCausesTheException() => ThisIsTheRootCause();

    private static Line[] ThisIsTheRootCause() => [new(0)];
}

Execute "Run Code Analysis on Solution" command:

image

Known workarounds

Suppressing the mentioned analyzers (S1144, S4487) in exception message does NOT resolve the issue.

Related information

  • Visual Studio: 17.8.0 Preview 2.0
  • dotnet SDK: 8.0.100-rc.1.23455.8
  • SonarAnalyzer.CSharp: 9.10.0.77988
  • Operating System: Win 11 22H2
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added Type: Bug Exceptions and blocking issues during analysis. Area: C# C# rules related issues. labels Sep 18, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Analyzer 'SonarAnalyzer.Rules.CSharp.UnusedPrivateMember' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.' Fix AD0001 in S1144: NullReferenceException when obtaining type information for implicit object creation inside collection expression Sep 18, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Fix AD0001 in S1144: NullReferenceException when obtaining type information for implicit object creation inside collection expression Fix AD0001 in S1144: NRE when obtaining type information for implicit object creation inside collection expression Sep 18, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource changed the title Fix AD0001 in S1144: NRE when obtaining type information for implicit object creation inside collection expression Fix AD0001 in S1144: Implicit object creation inside collection expression Sep 18, 2023
@zsolt-kolbay-sonarsource
Copy link
Contributor

Root cause: Microsoft.CodeAnalysis.SemanticModel.GetTypeInfo() cannot resolve the given syntax node:

private static Line[] ThisIsTheRootCause() => [new(0)]; // Fails here: implicit object creation inside a collection literal (new in C# 12)
//                                            ^^^^^^^^

This results in SonarAnalyzer.Wrappers.ObjectCreationFactory.TypeAsString() throwing a NullReferenceException.
This isn't difficult to fix for the UnusedPrivateMember analyzer, but several other analyzers use the SemanticModel.GetTypeInfo() method. So it needs further investigation if other analyzers are affected as well.

@csaba-sagi-sonarsource
Copy link
Contributor

Should be an issue on Roslyn`s side: dotnet/roslyn#70041

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Sprint: Hardening Fix FPs/FNs/improvements Type: Bug Exceptions and blocking issues during analysis.
Projects
None yet
6 participants