Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [CLI] Fix loading of `slnf` files ([PR](https://github.com/dotnet/roslynator/pull/1447))
- [CLI] Fix `--severity-level` ([PR](https://github.com/dotnet/roslynator/pull/1449))
- Fix analyzer [RCS1246](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1246) ([PR](https://github.com/dotnet/roslynator/pull/1451))

## [4.12.1] - 2024-04-15

Expand Down
3 changes: 3 additions & 0 deletions src/Core/SymbolUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ public static bool HasAccessibleIndexer(
return hasIndexer.Value;
}

if (originalDefinition.EqualsOrInheritsFrom(MetadataNames.System_Collections_Generic_List_T))
return true;

if (originalDefinition.ImplementsAny(
SpecialType.System_Collections_Generic_IList_T,
SpecialType.System_Collections_Generic_IReadOnlyList_T,
Expand Down
60 changes: 60 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ void M()
", source, expected);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task Test_UseElementAccessInsteadOfFirst_DerivedFromList()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Linq;
using System.Collections.Generic;

class C : List<string>
{
void M()
{
var list = new C();
var x = list.[|First()|];
}
}
", @"
using System.Linq;
using System.Collections.Generic;

class C : List<string>
{
void M()
{
var list = new C();
var x = list[0];
}
}
");
}

[Theory, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
[InlineData("((List<object>)x).[|ElementAt(1)|]", "((List<object>)x)[1]")]
[InlineData("((IList<object>)x).[|ElementAt(1)|]", "((IList<object>)x)[1]")]
Expand Down Expand Up @@ -68,6 +98,36 @@ void M()
", source, expected);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task Test_UseElementAccessInsteadOfElementAt_DerivedFromList()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Linq;
using System.Collections.Generic;

class C : List<string>
{
void M()
{
var list = new C();
var x = list.[|ElementAt(1)|];
}
}
", @"
using System.Linq;
using System.Collections.Generic;

class C : List<string>
{
void M()
{
var list = new C();
var x = list[1];
}
}
");
}

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