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

Fix RCS1080 #986

Merged
merged 2 commits into from Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.md
Expand Up @@ -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<T>` ([#986](https://github.com/josefpihrt/roslynator/pull/986).

## [4.1.2] - 2022-10-31

### Added
Expand Down
25 changes: 20 additions & 5 deletions src/Core/SymbolUtility.cs
Expand Up @@ -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;
Expand Down
Expand Up @@ -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<string>
{
}
", @"
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<string>
{
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseCountOrLengthPropertyInsteadOfAnyMethod)]
public async Task TestNoDiagnostic_ImmutableArray()
{
Expand Down