AD0001 errors when using WarningsNotAsErrors

  • Operating system: Windows 10
  • Visual Studio version: 17.8.0
  • SonarLint plugin version: 7.4.0.80741
  • Programming language you’re coding in: C# (.Net Framework 4.8)
  • Is connected mode used: No
    • Connected to SonarCloud or SonarQube (and which version):

I have enabled SonarLint on Visual studio with an existing project which has the “Warning as Errors” switched on, and it has many detected Sonar issues which can’t be resolved in one go.
The suggested solution to this (How to exclude sonar lint warnings from - build settings (Treat warnings as error) in project (visual studio) ) is to have use the “WarningsNotAsErrors” with the excluded rules.
To get started I have added all numbers from S1 to S10000 , which works in terms of moving the items from the error to the warning.

However there are several rules that come up with an AD0001 warning saying that the analyzer has had an exception. These exceptions do not happen if I let them appear as errors (e.g. I have several S2259 as errors but none when I have them as warnings). I also get the correct results if I switch of TreatWarningsAsErrors.

The list of exceptions are:

Rules S1944, S2053, S2222, S2259, S2583, S2589, S3329, S3655, S3900, S3949, S3966, S4158, S5773

System.ArgumentNullException: Value cannot be null. (Parameter 'type')
   at SonarAnalyzer.SymbolicExecution.Roslyn.ExceptionState..ctor(ITypeSymbol type)
   at SonarAnalyzer.SymbolicExecution.Roslyn.ExceptionCandidate.FromConversion(IOperationWrapperSonar operation)
   at SonarAnalyzer.SymbolicExecution.Roslyn.ExceptionCandidate.FromOperation(ProgramState state, IOperationWrapperSonar operation)
   at SonarAnalyzer.SymbolicExecution.Roslyn.RoslynSymbolicExecution.ProcessOperation(ExplodedNode node)+MoveNext()
   at SonarAnalyzer.SymbolicExecution.Roslyn.RoslynSymbolicExecution.Execute()
   at SonarAnalyzer.Rules.SymbolicExecutionRunnerBase.AnalyzeRoslyn(SonarAnalysisContext analysisContext, SonarSyntaxNodeReportingContext nodeContext, SyntaxNode body, ISymbol symbol)

Rule S1871

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Extensions.InvocationExpressionSyntaxExtensions.IsEqualTo(InvocationExpressionSyntax first, InvocationExpressionSyntax second, SemanticModel model)
   at SonarAnalyzer.Rules.CSharp.ConditionalStructureSameImplementation.HaveTheSameInvocations(SyntaxList`1 first, SyntaxList`1 second, SemanticModel model)
   at SonarAnalyzer.Rules.CSharp.ConditionalStructureSameImplementation.<>c__DisplayClass5_0.<Initialize>b__2(SwitchSectionSyntax preceding)
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
   at SonarAnalyzer.Rules.CSharp.ConditionalStructureSameImplementation.<>c.<Initialize>b__5_1(SonarSyntaxNodeReportingContext c)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

Rule S6588

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Rules.UseUnixEpochBase`3.IsUnixEpochSupported(Compilation compilation)
   at SonarAnalyzer.Rules.UseUnixEpochBase`3.<>c__DisplayClass10_0.<Initialize>b__0(SonarCompilationStartAnalysisContext start)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

Rule S6618

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Rules.CSharp.UseStringCreate.CompilationTargetsValidNetVersion(Compilation compilation)
   at SonarAnalyzer.Rules.CSharp.UseStringCreate.<>c__DisplayClass6_0.<Initialize>b__0(SonarCompilationStartAnalysisContext start)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

Rule S6610

System.NullReferenceException: Object reference not set to an instance of an object.
   at SonarAnalyzer.Rules.UseCharOverloadOfStringMethodsBase`2.CompilationTargetsValidNetVersion(Compilation compilation)
   at SonarAnalyzer.Rules.UseCharOverloadOfStringMethodsBase`2.<Initialize>b__5_0(SonarCompilationStartAnalysisContext start)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)

Is there anything that I can do to avoid the exceptions without changing the behavior for the other analyzers?

Hello @Zadj,

Welcome to the community!

Thank you for reaching out.

I was not able to reproduce your issue, would you be able to provide me with a small project that reproduces those issues?
Not necessarily all issues but at least a few of them, then we will see how to proceed for the rest.

Thank you,

Hi @Sebastien Marichal

I’ve created a project that shows the issue with some of the rules.

As part of the cause of the issue is the configuration of the project, I’ve put my example up on GitHub.

When creating the example, I was able to get a better idea of the factors that trigger the issue.

  • It only happens with the .Net 4.8 project and not the .Net 8 project.

  • It does not happen if I use a smaller set of rules in the “WarningsNotAsErrors” element, only when I have the generated list of all numbers from S1 to S10000.

  • Some of the AD001 errors happen even if the rule is not going to appear (i.e. they always appear, independent of the code)

I think a possible workaround may be to have a more accurate list of exclusions instead of the generated one.

If it is possible to get a list of all the C#, CSS, Javascript, TypeScript, and Html rules then that may be small enough to not trigger the issue.

Thanks,

Zadj

2 Likes

Hello @Zadj,

Sorry for the delay; the investigation took more time than expected.

From what I found, the issue is coming from MSBuild as, for .NET Framework projects, the property WarningsNotAsErrors seems to limit the number of warnings you can specify in total. Something around 840 based on my tests.
It also silences other warnings, such as Visual Studio warnings.

As of today, the only workaround I can give you is, as you already mentioned, to have an accurate list of the rules to not treat as errors. Be aware that you will have the same issue again if you need to specify more than 840 rules.

To retrieve a list of active rules, you can take a look at this repository: GitHub - SonarSource/rspec: Rule Specification

I hope this helps.

Have a good day,

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.