diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs index fbb1a53eb1..2a4de24242 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs @@ -123,7 +123,7 @@ public override void Initialize(AnalysisContext context) context.RegisterCompilationStartAction(context => { var voidType = context.Compilation.GetSpecialType(SpecialType.System_Void); - var publicOrInternalColl = Collector.GetInstance(voidType, symbol => context.Options.MatchesConfiguredVisibility(UseConcreteTypeForMethodReturn, symbol, context.Compilation, SymbolVisibilityGroup.Private)); + var publicOrInternalColl = Collector.GetInstance(voidType, symbol => symbol.IsInSource() && context.Options.MatchesConfiguredVisibility(UseConcreteTypeForMethodReturn, symbol, context.Compilation, SymbolVisibilityGroup.Private)); context.RegisterSymbolStartAction(context => { @@ -134,7 +134,7 @@ public override void Initialize(AnalysisContext context) return; } - var coll = Collector.GetInstance(voidType, symbol => context.Options.MatchesConfiguredVisibility(UseConcreteTypeForMethodReturn, symbol, context.Compilation, SymbolVisibilityGroup.Private)); + var coll = Collector.GetInstance(voidType, symbol => symbol.IsInSource() && context.Options.MatchesConfiguredVisibility(UseConcreteTypeForMethodReturn, symbol, context.Compilation, SymbolVisibilityGroup.Private)); // we accumulate a bunch of info in the collector object context.RegisterOperationAction(context => coll.HandleInvocation((IInvocationOperation)context.Operation), OperationKind.Invocation); diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs index 75d79c032f..cd1b66c316 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs @@ -1367,6 +1367,24 @@ public bool GetIFoo(out IFoo ifoo) await TestCSAsync(Source); } + [Fact, WorkItem(6852, "/~https://github.com/dotnet/roslyn-analyzers/issues/6852")] + public static async Task ShouldNotCrashForInvocationsIntoMetadata() + { + const string Source = @" +using System; + +class C +{ + private void M(ValueTuple vt) + { + vt.Item1(); + } +} + "; + + await TestCSAsync(Source); + } + private static async Task TestCSAsync(string source, params DiagnosticResult[] diagnosticResults) { var test = new VerifyCS.Test