From ac5c16ba916763673da2ccd81404f5266fb99979 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 11 Nov 2022 19:35:45 +0100 Subject: [PATCH] Fix RCS1080 (#986) --- ChangeLog.md | 4 ++ src/Core/SymbolUtility.cs | 25 ++++++++--- ...OrLengthPropertyInsteadOfAnyMethodTests.cs | 44 +++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 7d8ce00d7a..7e7ec8cb8d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Disable [RCS1080](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1080.md) by default ([#980](https://github.com/josefpihrt/roslynator/pull/980). +### Fixed + +- Fix ([RCS1080](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1080.md)) when collection is derived from `List` ([#986](https://github.com/josefpihrt/roslynator/pull/986). + ## [4.1.2] - 2022-10-31 ### Added diff --git a/src/Core/SymbolUtility.cs b/src/Core/SymbolUtility.cs index cde1727d63..50c5e7e84f 100644 --- a/src/Core/SymbolUtility.cs +++ b/src/Core/SymbolUtility.cs @@ -191,15 +191,30 @@ public static bool IsEventHandlerMethod(IMethodSymbol methodSymbol) if (originalDefinition.TypeKind == TypeKind.Interface) return "Count"; - foreach (ISymbol symbol in typeSymbol.GetMembers()) + while (typeSymbol is not null + && typeSymbol.SpecialType != SpecialType.System_Object) { - if (symbol.Kind == SymbolKind.Property - && StringUtility.Equals(symbol.Name, "Count", "Length") - && semanticModel.IsAccessible(position, symbol)) + foreach (ISymbol symbol in typeSymbol.GetMembers("Count")) { - return symbol.Name; + if (symbol.Kind == SymbolKind.Property + && semanticModel.IsAccessible(position, symbol)) + { + return symbol.Name; + } } + + foreach (ISymbol symbol in typeSymbol.GetMembers("Length")) + { + if (symbol.Kind == SymbolKind.Property + && semanticModel.IsAccessible(position, symbol)) + { + return symbol.Name; + } + } + + typeSymbol = typeSymbol.BaseType; } + } return null; diff --git a/src/Tests/Analyzers.Tests/RCS1080UseCountOrLengthPropertyInsteadOfAnyMethodTests.cs b/src/Tests/Analyzers.Tests/RCS1080UseCountOrLengthPropertyInsteadOfAnyMethodTests.cs index 593c6617c3..1cf187c36a 100644 --- a/src/Tests/Analyzers.Tests/RCS1080UseCountOrLengthPropertyInsteadOfAnyMethodTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1080UseCountOrLengthPropertyInsteadOfAnyMethodTests.cs @@ -548,6 +548,50 @@ void M() "); } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseCountOrLengthPropertyInsteadOfAnyMethod)] + public async Task Test_DerivedFromListOfT() + { + await VerifyDiagnosticAndFixAsync(@" +using System.Collections.Generic; +using System.Linq; + +class Program +{ + static void Main(string[] args) + { + List2 items = new List2(); + + if (items.[|Any()|]) + { + } + } +} + +public class List2 : List +{ +} +", @" +using System.Collections.Generic; +using System.Linq; + +class Program +{ + static void Main(string[] args) + { + List2 items = new List2(); + + if (items.Count > 0) + { + } + } +} + +public class List2 : List +{ +} +"); + } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseCountOrLengthPropertyInsteadOfAnyMethod)] public async Task TestNoDiagnostic_ImmutableArray() {