Sonar-dotnet: UnusedPrivateMember throws 'NullReferenceException' in SonarLint

Created on 22 Feb 2020  路  8Comments  路  Source: SonarSource/sonar-dotnet

Description

Unfortunatly, I couldn't reproduce it a second time (I did not spend that much time on it, I have to admit), but given the stack trace, I think it should be doable to make the code more defensive:

Repro steps

Severity    Code    Description Project File    Line    Suppression State   Detail Description
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.'.  <myProject>     1   Active  Analyzer 'SonarAnalyzer.Rules.CSharp.UnusedPrivateMember' threw the following exception:
'Exception occurred with following context:
Compilation:  <myProject>
ISymbol: IBitsOperator (NamedType)

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.GetDefaultConstructor(INamedTypeSymbol namedType)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.GetImplicitlyCalledConstructor(IMethodSymbol constructor)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
   at Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorDeclarationSyntax.Accept(CSharpSyntaxVisitor visitor)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.VisiSeverity Code    Description Project File    Line    Suppression State   Detail Description
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.'.  SmartAss        1   Active  Analyzer 'SonarAnalyzer.Rules.CSharp.UnusedPrivateMember' threw the following exception:
'Exception occurred with following context:
Compilation: SmartAss
ISymbol: IBitsOperator (NamedType)

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.GetDefaultConstructor(INamedTypeSymbol namedType)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.GetImplicitlyCalledConstructor(IMethodSymbol constructor)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
   at Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorDeclarationSyntax.Accept(CSharpSyntaxVisitor visitor)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor.VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
   at Microsoft.CodeAnalysis.CSharp.Syntax.InterfaceDeclarationSyntax.Accept(CSharpSyntaxVisitor visitor)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSyntaxWalkerHelper.SafeVisit(CSharpSyntaxWalker syntaxWalker, SyntaxNode syntaxNode)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.VisitDeclaringReferences(INamedTypeSymbol namedType, CSharpSyntaxWalker visitor, Compilation compilation, Boolean includeGeneratedFile)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.<>c__DisplayClass10_0.<Initialize>b__1(SymbolAnalysisContext cc)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteSymbolActionsCore>b__45_1(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.
t(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor.VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
   at Microsoft.CodeAnalysis.CSharp.Syntax.InterfaceDeclarationSyntax.Accept(CSharpSyntaxVisitor visitor)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSyntaxWalkerHelper.SafeVisit(CSharpSyntaxWalker syntaxWalker, SyntaxNode syntaxNode)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.VisitDeclaringReferences(INamedTypeSymbol namedType, CSharpSyntaxWalker visitor, Compilation compilation, Boolean includeGeneratedFile)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.<>c__DisplayClass10_0.<Initialize>b__1(SymbolAnalysisContext cc)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteSymbolActionsCore>b__45_1(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.

Related information

  • SonarC# Version 8.3
  • Visual Studio Version 16.4.5
C# Bug

All 8 comments

Getting this one too. More precisely:
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.'.

  • SonarLint for Visual Studio 4.17.0.14702
  • Visual Studio Version 16.4.5

Thanks @Corniel and @jensbrak

@Corniel if you could provide the snippet that is in

Compilation: SmartAss
ISymbol: IBitsOperator (NamedType)

It seems there's an interface declaration (VisitInterfaceDeclaration) and then inside a ConstructorDeclarationSyntax for which CSharpSymbolUsageCollector.VisitConstructorDeclaration fails when reaching GetDefaultConstructor - maybe the interface declaration will help us narrow down the problem.

@andrei-epure-sonarsource - Let me know if there's anything I can add or do to help to understand the problem. Below is the full exception, I forgot to attach it previously. It seems to come when I add an interface or perhaps open a file with one...

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.'.  Phiddle.Core        1   Active  Analyzer 'SonarAnalyzer.Rules.CSharp.UnusedPrivateMember' threw the following exception:
'Exception occurred with following context:
Compilation: <manually disclosed>
ISymbol: ILoggingService (NamedType)

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.GetDefaultConstructor(INamedTypeSymbol namedType)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.GetImplicitlyCalledConstructor(IMethodSymbol constructor)
   at SonarAnalyzer.Helpers.CSharpSymbolUsageCollector.VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
   at Microsoft.CodeAnalysis.CSharp.Syntax.ConstructorDeclarationSyntax.Accept(CSharpSyntaxVisitor visitor)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.DefaultVisit(SyntaxNode node)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor.VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
   at Microsoft.CodeAnalysis.CSharp.Syntax.InterfaceDeclarationSyntax.Accept(CSharpSyntaxVisitor visitor)
   at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxWalker.Visit(SyntaxNode node)
   at SonarAnalyzer.Helpers.CSharpSyntaxWalkerHelper.SafeVisit(CSharpSyntaxWalker syntaxWalker, SyntaxNode syntaxNode)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.VisitDeclaringReferences(INamedTypeSymbol namedType, CSharpSyntaxWalker visitor, Compilation compilation, Boolean includeGeneratedFile)
   at SonarAnalyzer.Rules.CSharp.UnusedPrivateMember.<>c__DisplayClass10_0.<Initialize>b__1(SymbolAnalysisContext cc)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteSymbolActionsCore>b__45_1(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----

My best guess would be that it happens while you're typing, and some stuff can not be resolved yet.

``` C#
///

Operator that can tweak/manipulate bits.
public interface IBitsOperator where T : struct
{
/// Gets the bit size of the type.
int BitSize { get; }

/// <summary>Counts the number of bits with the value 1.</summary>
int Count(T bits);

/// <summary>Gets the number of bits needed to represent the number.</summary>
int Size(T bits);

/// <summary>Gets the index of the first bit needed to represent the number.</summary>
int First(T bits);

/// <summary>Flags the bit at the specified position.</summary>
T Flag(T bits, int position);

/// <summary>Unflags the bit at the specified position.</summary>
T Unflag(T bits, int position);

/// <summary>Mirrors the bits.</summary>
T Mirror(T bits);

/// <summary>Represents the bits as binary string.</summary>
string ToString(T bits);

}
```

And the solution can be found here: https://github.com/Corniel/smart-ass

@Corniel @jensbrak Thank's for reporting this.
I was indeed able to reproduce the issue.

To reproduce, create an invalid interface with an instance constructor. VS shows CS0526 error, but UnusedPrivateMember rule is still called and crashes with AD0001 NullReferenceException.
Copy-paste the following snippet inside IDE:

    interface A
    {
        A() { }
    }

@christophe-zurn-sonarsource it might be the same path as I added the T Mirror(T bits); method to the contract.

@Corniel Thanks for the update. I was curious after your response, so I had a quick look at how it behaved. Basically, any method found without a return type will be considered a constructor declaration even when the syntax tree is not stable. Ex:
constructor

So when typing a new signature in an interface, if one omit the return type at the beginning, it will result in the AD0001 error being thrown for the UnusedPrivateMember rule.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

franciscoarturoalonso picture franciscoarturoalonso  路  5Comments

jcurl picture jcurl  路  5Comments

valhristov picture valhristov  路  4Comments

dougheeren picture dougheeren  路  4Comments

perlun picture perlun  路  3Comments