dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19k stars 4.03k forks source link

AD0001 (KeyNotFoundException) from AbstractRemoveUnusedParametersAndValuesDiagnosticAnalyzer #38668

Open Tragetaschen opened 5 years ago

Tragetaschen commented 5 years ago

Version Used: 16.3.0 Preview 3.0

Steps to Reproduce:

using System.Text;

internal static class C1
{
    public const int A = 42;
}

internal class C2
{
    public int A { get; set; }
}

internal class C3
{
    public string M(C2 c2)
    {
        var result = new StringBuilder();
        switch (c2.A)
        {
            case C1.A:
                result.Append("A");
                break;
            default:
                result.Append("*");
                break;
        }
        return result.ToString();
    }
}

Expected Behavior: No AD0001

Actual Behavior:

Severity    Code    Description Project File    Line    Suppression State   Detail Description
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: C:\Users\…\Test.cs
SyntaxNode: result.Append("A"); [ExpressionStatementSyntax]@[319..338) (20,16)-(20,35)

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__54_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----
'.
mavasani commented 5 years ago

Fires when user has an invalid editorconfig option value.

Tragetaschen commented 5 years ago

Do you have a (couple of) candidates, then I can check. I have changed

csharp_style_unused_value_expression_statement_preference = true:error

to

csharp_style_unused_value_expression_statement_preference = discard_variable:error

to make AD0001 go away. That was from a time when those settings weren't yet documented.

Is there a better way to check the contents of .editorconfig files?

mavasani commented 5 years ago

Is there a better way to check the contents of .editorconfig files?

I believe @sharwell had a tracking issue to create an IDE analyzer that validates our IDE options in .editorconfig files.

Tragetaschen commented 5 years ago

19055?

OoLunar commented 2 years ago

Good to know that it's an invalid editorconfig option. Are there plans to warn us which option is invalid instead of getting an unhelpful CSC KeyNotFound warning?

CyrusNajmabadi commented 1 year ago

@jmarolf Does thsi go away now that the user can set these with the editorconfig ui? Does it tell you if you wrote things badly?

jmarolf commented 1 year ago

We don't have a mechanism to surface diagnostics in editorconfig yet if you manually edit the file, however if these options are set via the UI you should be "protected" from setting them to invalid values.