Skip to content

Two AD0001 (unreachable and KeyNotFoundException) from valid code in CSharpRemoveUnusedParametersAndValuesDiagnosticAnalyzer  #34830

@Tragetaschen

Description

@Tragetaschen

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)
-----
'.

Metadata

Metadata

Assignees

Labels

4 - In ReviewA fix for the issue is submitted for review.Area-IDEBugIDE-CodeStyleBuilt-in analyzers, fixes, and refactorings

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions