diff --git a/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs b/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs index 8fc621bf3eae..27cd3a9822fd 100644 --- a/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs +++ b/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeAnalyzer.cs @@ -268,12 +268,19 @@ void Evaluate(ISymbol affectedSymbol, ITypeSymbol fromType, PooledConcurrentSet< return; } - // if any of the methods that are invoked on toType are explicit implementations of interface methods, then we don't want - // to recommend upgrading the type otherwise it would break those call sites if (targets != null) { foreach (var t in targets) { + // if any of the methods that are invoked on fromType are default implementations of interface methods, + // then we don't want to recommend upgrading the type because it would break those call sites. + if (!t.IsAbstract && fromType.TypeKind is TypeKind.Interface) + { + return; + } + + // if any of the methods that are invoked on toType are explicit implementations of interface methods, + // then we don't want to recommend upgrading the type because it would break those call sites. var check = toType; while (check != null) { diff --git a/src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs b/src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs index 5e15e693402d..ad9c1d967e22 100644 --- a/src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs +++ b/src/Microsoft.CodeAnalysis.NetAnalyzers/tests/Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/Microsoft.NetCore.Analyzers/Performance/UseConcreteTypeTests.cs @@ -662,6 +662,32 @@ static int Bar() await TestCSAsync(Source); } + [Fact] + [WorkItem(50328, "https://github.com/dotnet/sdk/issues/50328")] + public static async Task ShouldNotTrigger6() + { + const string Source = @" +#nullable enable + interface IFoo + { + int M() => 42; + } + public class C : IFoo + { + } + public class Use + { + static int Bar() + { + IFoo f = new C(); + return f.M(); + } + } + "; + + await TestCSAsync(Source); + } + [Fact] public static async Task ShouldTrigger_InterpolatedString_Mameof() {