SonarAnalyzer.Rules.CSharp.UnusedPrivateMember exception

  • Operating system: Windows 10
  • Visual Studio version: 17.8
  • SonarLint plugin version: 7.3.0.77872
  • Programming language you’re coding in: C# 12 (.NET SDK 8.0.100)
  • Is connected mode used: no

In a project with only one source file yet the analyzer reports

|Warning|AD0001|Analyzer 'SonarAnalyzer.Rules.CSharp.UnusedPrivateMember' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.

Tried to clean everything (git clean -fdx) and rebuild the solution. The warning remains.

As only one short source file is affected, I include the code here:

using System.Numerics;
using SkiaSharp;

namespace Test.Geometry;

public class Geometry2D : IDisposable
{
    private readonly SKPath geometry = new();
    private readonly SKPathMeasure geometryEval = new();
    private bool disposedValue;

    public Geometry2D(Vector2 start, Vector2 end)
    {
        SKPoint[] points = [new(start.X, start.Y), new(end.X, end.Y)];
        geometry.AddPoly(points, close: false);
        geometryEval.SetPath(geometry);
    }

    public double Length => geometryEval.Length;

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // dispose managed state (managed objects)
                geometryEval.Dispose();
                geometry.Dispose();
            }

            // free unmanaged resources (unmanaged objects) and override finalizer
            // set large fields to null
            disposedValue = true;
        }
    }

    public void Dispose()
    {
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
        Dispose(disposing: true);
        GC.SuppressFinalize(this);
    }
}
1 Like

Same here, since VS2022 updated to v17.8. Sonarlint v7.4.0.80741

Avertissement AD0001 L’analyseur ‘SonarAnalyzer.Rules.CSharp.UnusedPrivateMember’ a levé une exception de type ‘System.NullReferenceException’ avec le message ‘Object reference not set to an instance of an object.’.

Full stack trace :

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 SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.VisitAssignmentExpression(AssignmentExpressionSyntax 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.b__1(SyntaxReference x)
at System.Linq.Enumerable.All[TSource](IEnumerable1 source, Func2 predicate)
at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.NamedSymbolAction(SonarSymbolReportingContext context, HashSet1 removableInternalTypes) at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

1 Like

Thanks y’all for the feedback. I’ve pinged the right team.

Hello @milbrandt, @PTR,

Prior to version 9.13 of the C# Analyzer, we do not support C# 12 new syntaxes, such as Collection Expression:

SKPoint[] points = [new(start.X, start.Y), new(end.X, end.Y)];

Both SonarLint 7.4 and 7.3 embed a version that does not support C# 12 new syntaxes

@PTR, I am assuming your issue is also C# 12 related. If this is not the case, could you provide a reproducer?

At the moment both 9.13 and 9.14 versions are not available from NuGet due to technical issues during publishing. Hopefully, 9.14 will be published and available soon.
It might take a bit more time for the next version of SonarLint to be released with the latest version of the C# analyzer.

However, both SonarQube (10.3) and SonarCloud are compatible with C# 12.

Sorry for the inconvenience.