Roslyn: Warning AD0001 Analyzer 'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw an exception of type 'System.NullReferenceException'

Created on 9 Jan 2019  路  18Comments  路  Source: dotnet/roslyn

Version Used: VS 2019 Preview 28508.105.d16.0
C# Tools 3.0.0-beta2-19055-10+15504ac30ab7b6f5bfedcdeee0a180915689f66f

Steps to Reproduce:
I don't have a repro yet. The following warnings are showing in my Error List


Severity    Code    Description Project File    Line    Suppression State   Detail Description
Warning AD0001  Analyzer 'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.   Microsoft.CodeAnalysis.Converter        1   Active  Analyzer 'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: Microsoft.CodeAnalysis.Converter
ISymbol: VisitSimpleArgument (Method)

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.FlowAnalysis.SymbolUsageAnalysis.SymbolUsageAnalysis.AnalysisData.OnWriteReferenceFound(ISymbol symbol, IOperation operation, Boolean maybeWritten)
   at Microsoft.CodeAnalysis.FlowAnalysis.SymbolUsageAnalysis.SymbolUsageAnalysis.Walker.OnReferenceFound(ISymbol symbol, IOperation operation)
   at Microsoft.CodeAnalysis.FlowAnalysis.SymbolUsageAnalysis.SymbolUsageAnalysis.Walker.VisitDeclarationPattern(IDeclarationPatternOperation operation)
   at Microsoft.CodeAnalysis.Operations.DeclarationPatternOperation.Accept(OperationVisitor visitor)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.Visit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.VisitArray[T](IEnumerable`1 operations)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.DefaultVisit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationVisitor.VisitPatternCaseClause(IPatternCaseClauseOperation operation)
   at Microsoft.CodeAnalysis.Operations.BasePatternCaseClauseOperation.Accept(OperationVisitor visitor)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.Visit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.VisitArray[T](IEnumerable`1 operations)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.DefaultVisit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationVisitor.VisitSwitchCase(ISwitchCaseOperation operation)
   at Microsoft.CodeAnalysis.Operations.BaseSwitchCaseOperation.Accept(OperationVisitor visitor)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.Visit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.VisitArray[T](IEnumerable`1 operations)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.DefaultVisit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationVisitor.VisitSwitch(ISwitchOperation operation)
   at Microsoft.CodeAnalysis.Operations.BaseSwitchOperation.Accept(OperationVisitor visitor)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.Visit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.VisitArray[T](IEnumerable`1 operations)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.DefaultVisit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationVisitor.VisitBlock(IBlockOperation operation)
   at Microsoft.CodeAnalysis.Operations.BaseBlockOperation.Accept(OperationVisitor visitor)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.Visit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.VisitArray[T](IEnumerable`1 operations)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.DefaultVisit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationVisitor.VisitConditional(IConditionalOperation operation)
   at Microsoft.CodeAnalysis.Operations.BaseConditionalOperation.Accept(OperationVisitor visitor)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.Visit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.VisitArray[T](IEnumerable`1 operations)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.DefaultVisit(IOperation operation)
   at Microsoft.CodeAnalysis.Operations.OperationVisitor.VisitBlock(IBlockOperation operation)
   at Microsoft.CodeAnalysis.Operations.BaseBlockOperation.Accept(OperationVisitor visitor)
   at Microsoft.CodeAnalysis.Operations.OperationWalker.Visit(IOperation operation)
   at Microsoft.CodeAnalysis.FlowAnalysis.SymbolUsageAnalysis.SymbolUsageAnalysis.Walker.Visit(IEnumerable`1 operations, AnalysisData analysisData, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.FlowAnalysis.SymbolUsageAnalysis.SymbolUsageAnalysis.Walker.AnalyzeOperationsAndUpdateData(IEnumerable`1 operations, AnalysisData analysisData, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.FlowAnalysis.SymbolUsageAnalysis.SymbolUsageAnalysis.Run(IOperation rootOperation, ISymbol owningSymbol, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues.AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.SymbolStartAnalyzer.BlockAnalyzer.AnalyzeUnusedValueAssignments(OperationBlockAnalysisContext context, Boolean isComputingUnusedParams, PooledHashSet`1 symbolUsageResultsBuilder, Boolean& hasBlockWithAllUsedSymbolWrites)
   at Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues.AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.SymbolStartAnalyzer.BlockAnalyzer.AnalyzeOperationBlockEnd(OperationBlockAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__55`2.<ExecuteBlockActions>b__55_1(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.

Expected Behavior:

No AD0001 warnings

Actual Behavior:

4 - In Review Area-IDE Bug

All 18 comments

tagging @mavasani

Seems like IDeclarationPatternOperation.DeclaredSymbol can now be null with this change: https://github.com/dotnet/roslyn/commit/6224a5c2feb8c9096cf5765f3d8d2ded396e7e88#diff-b68aa1802251151217571a56d9bfd2b8R31 Tagging @gafter to confirm. I'll fix the analyzer accordingly.

When can that happen?

Got a repro:

class C
{
    void M(object o)
    {
        if (o is int _)
        {
        }
    }
}

I never ran into this on Preview 1, but my error list is filled with a dozen of these in Preview 2. Is there a workaround (besides filtering out all AD0001 lines)?

@jnm2 The feature that required a breaking change to semantics of IDeclarationPatternOperation.DeclaredSymbol was checked in preview2, hence it only repros on preview2. You have few choices here:

  1. Wait for a build that has https://github.com/dotnet/roslyn/pull/32293 - I believe that is preview3, but @jinujoseph to confirm.
  2. Disable all diagnostic IDs reported by this analyzer, which is the following three:
        public const string ExpressionValueIsUnusedDiagnosticId = "IDE0058";
        public const string ValueAssignedIsUnusedDiagnosticId = "IDE0059";
        public const string UnusedParameterDiagnosticId = "IDE0060";
  1. Suppress or filter out AD0001 from the error list.

yes #32293 should be part of our 16.0.preview3 release

This happened to me on preview 4 when calling an Event triggered function specifically in the select component onchange="@hasChanged"

I just started receiving this error building against a method we marked Obsolete. We may optimize the method, but for now we are leaving it parked as-is. Is it safe to ignore this warning, otherwise?

@mwpowellhtx Yes, AD0001 is not a violation in your code, but rather a crash in the analyzer. It should be fine to ignore this warning in your code. Please do file a separate issue on this repo if you can repro the AD0001 on latest VS2019 builds.

@mavasani Thanks for the feedback. Today, at this point, that is a non-sequitur for what we are doing. But it does beggar the question, is the fix going to be merged for VS2017 generation analyzers?

@mwpowellhtx AD0001 error is not specific to any particular analyzer or VS version. It indicates that some analyzer (either built-in or a 3rd party one) hit an exception while executing. The analyzer for the issue being discussed was first added in VS2019 16.2, so it is not applicable for VS2017. Very likely that AD0001 you are seeing on VS2017 is for a different analyzer. You would need to look at the exception message to figure out the crashing analyzer.

@mavasani You are incorrect, I think. We are targeting packages that are contemporary with VS2017, MSBuild environment, to our knowledge. That is, v15.9 and so on.

This was never a problem prior to installing VS2019 side-by-side with VS2017, to our knowledge.

Severity    Code    Description Project File    Line    Suppression State
Warning AD0001  Analyzer 'Microsoft.CodeAnalysis.CSharp.Analyzers.MetaAnalyzers.CSharpDiagnosticAnalyzerApiUsageAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.  Kingdom.Roslyn.Compilation.Services G:\Source\Spikes\Kingdom.Roslyn.Compilation.Services\Prototype\src\Kingdom.Roslyn.Compilation.Services\CSC  1   Active

@mwpowellhtx The AD0001 message you pasted points to an exception in CSharpDiagnosticAnalyzerApiUsageAnalyzer, while this issue is about AD0001 in CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer, the latter was first added in VS2019.

You should be able to resolve the AD0001 in CSharpDiagnosticAnalyzerApiUsageAnalyzer by applying the suggestion from https://github.com/dotnet/roslyn-analyzers/issues/2961#issuecomment-545438365

@mavasani We will try that, thank you. Does it matter the version? i.e. aligned with the 15.9 generation bits.

That worked, thank you. Notwithstanding version alignment concerns. There are no apparent dependencies in the Analyzers, anyway, so we should be good.

Was this page helpful?
0 / 5 - 0 ratings