Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SA1201 throws KeyNotFoundException for certain (invalid syntax) code #3160

Closed
AArnott opened this issue Jun 8, 2020 · 1 comment · Fixed by #3179
Closed

SA1201 throws KeyNotFoundException for certain (invalid syntax) code #3160

AArnott opened this issue Jun 8, 2020 · 1 comment · Fixed by #3179
Assignees

Comments

@AArnott
Copy link
Contributor

AArnott commented Jun 8, 2020

Severity	Code	Description	Project	File	Line	Tool
Warning	AD0001	Analyzer 'StyleCop.Analyzers.OrderingRules.SA1201ElementsMustAppearInTheCorrectOrder' threw an exception of type 'System.Collections.Generic.KeyNotFoundException' with message 'The given key was not present in the dictionary.'.
Exception occurred with following context:
Compilation: VSServiceBrokerSample
SyntaxTree: D:\Users\andarno\source\repos\VSServiceBrokerSample\VSServiceBrokerSample\FilePicker\IWorkspace.cs
SyntaxNode: using System.Threading; using System ... [CompilationUnitSyntax]@[64..1246) (2,0)-(41,0)

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at StyleCop.Analyzers.OrderingRules.SA1201ElementsMustAppearInTheCorrectOrder.HandleMemberList(SyntaxNodeAnalysisContext context, ImmutableArray`1 elementOrder, Int32 kindIndex, SyntaxList`1 members, ImmutableArray`1 order)
   at StyleCop.Analyzers.OrderingRules.SA1201ElementsMustAppearInTheCorrectOrder.HandleCompilationUnit(SyntaxNodeAnalysisContext context, StyleCopSettings settings)
   at StyleCop.Analyzers.AnalyzerExtensions.<>c__DisplayClass3_0`1.<RegisterSyntaxNodeAction>b__0(SyntaxNodeAnalysisContext c)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__58`1.<ExecuteSyntaxNodeAction>b__58_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----

Suppress the following diagnostics to disable this analyzer: SA1201	VSServiceBrokerSample		1	StyleCop.Analyzers

Repro

Add the following code to any .cs source file. It doesn't matter that there will be many compile errors. Just look for the AD0001 error.

    public class Foo
    {
                private void Execute(object sender, EventArgs e)
        {
            this.package.JoinableTaskFactory.RunAsync(async delegate
            {
                try
                {
                    using (var c = await this.serviceBrokerClient.GetProxyAsync<x>(y))
                    {
if (c.Proxy is object) {)
                    }

                
                }
                catch (Exception ex)
                {
                    await this.package.JoinableTaskFactory.SwitchToMainThreadAsync();
                    MessageDialog.Show("Error", ex.Message, MessageDialogCommandSet.RetryCancel);
                }
            });
        }

    }
vweijsters added a commit to vweijsters/StyleCopAnalyzers that referenced this issue Jul 26, 2020
@vweijsters vweijsters self-assigned this Jul 26, 2020
@vweijsters
Copy link
Contributor

I can't reproduce this in VS2019 16.6.5. Based on the provided call stack I've made a pull request to workaround this issue.

sharwell added a commit that referenced this issue Jul 30, 2020
@sharwell sharwell added this to the 1.2-beta.next milestone Jul 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants