Skip to content

Commit

Permalink
Fix RCS1080 (dotnet#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored and JochemHarmes committed Oct 30, 2023
1 parent 4f8c1ae commit ac5c16b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
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

0 comments on commit ac5c16b

Please sign in to comment.