Roslyn: Two AD0001 (unreachable and KeyNotFoundException) from valid code in CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer

Created on 8 Apr 2019  路  5Comments  路  Source: dotnet/roslyn

Version Used:
16.0.0

Steps to Reproduce:

using System;
using System.Threading.Tasks;

public interface IFoo { event Action Fooed; }

public sealed class C : IDisposable
{
    private readonly Task<IFoo> foo;

    public C(Task<IFoo> foo)
    {
        this.foo = foo;
        Task.Run(async () => (await foo).Fooed += fooed);
    }

    private void fooed() { }

    public void Dispose() => foo.Result.Fooed -= fooed;
}

Opening my solution with this file already open immediately results in two AD0001 diagnostics:

Warning AD0001  Analyzer 'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw an exception of type 'System.InvalidOperationException' with message 'This program location is thought to be unreachable.'.  *       1   Active  Analyzer 'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: *
ISymbol: .ctor (Method)

System.InvalidOperationException: This program location is thought to be unreachable.
   at Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues.AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.ShouldReportUnusedParameters(ISymbol symbol, UnusedParametersPreference unusedParametersPreference, ReportDiagnostic unusedParametersSeverity)
   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)
-----
'.

and

Warning AD0001  Analyzer 'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw an exception of type 'System.Collections.Generic.KeyNotFoundException' with message 'The given key was not present in the dictionary.'.  *       1   Active  Analyzer 'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: *
IOperation: ExpressionStatement
SyntaxTree: *
SyntaxNode: Task.Run(async () => (await foo ... [ExpressionStatementSyntax]@[247..296) (12,8)-(12,57)

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues.AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.SymbolStartAnalyzer.BlockAnalyzer.AnalyzeExpressionStatement(OperationAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteOperationAction>b__51_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.
4 - In Review Area-IDE Bug IDE-CodeStyle

Most helpful comment

This needed a followup fix, which was just checked in yesterday. You can meanwhile workaround by going to Tools Options TextEditor C# CodeStyle and explicitly change the Avoid unused parameter preference and click OK.

All 5 comments

This gets annoying rather quickly as the warnings continue to pile up without ever being removed in the running VS instance

image

Frankly, I don't agree with that milestone change. The analyzer is broken and the net effect is that the Warning tab in VS' Error List is entirely useless. I think this issue (or any other resulting in AD0001) deserves more or less immediate fixing.

I think the fix here is just changing the values of fields of this enum to start from 0.

Is the fix available in 16.2.0 Preview 4.0?

I just got the following AD0001 KeyNotFoundException stack trace, which looks similar to the one reported in the OP.

'Microsoft.CodeAnalysis.CSharp.RemoveUnusedParametersAndValues.CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: *
ISymbol: Stabilize (Method)

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Immutable.ImmutableDictionary`2.get_Item(TKey key)
   at Microsoft.CodeAnalysis.RemoveUnusedParametersAndValues.AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer.SymbolStartAnalyzer.BlockAnalyzer.<AnalyzeUnusedValueAssignments>g__ShouldReportUnusedValueDiagnostic|15_0(ISymbol symbol, IOperation unreadWriteOperation, SymbolUsageResult resultFromFlowAnalysis, ImmutableDictionary`2& properties)
   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)

This needed a followup fix, which was just checked in yesterday. You can meanwhile workaround by going to Tools Options TextEditor C# CodeStyle and explicitly change the Avoid unused parameter preference and click OK.

Was this page helpful?
0 / 5 - 0 ratings