Skip to content

Commit 4a8e1a1

Browse files
authored
Fix RCS1246 (#1451)
1 parent 9247ccc commit 4a8e1a1

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

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

1516
## [4.12.1] - 2024-04-15
1617

src/Core/SymbolUtility.cs

+3
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ public static bool HasAccessibleIndexer(
121121
return hasIndexer.Value;
122122
}
123123

124+
if (originalDefinition.EqualsOrInheritsFrom(MetadataNames.System_Collections_Generic_List_T))
125+
return true;
126+
124127
if (originalDefinition.ImplementsAny(
125128
SpecialType.System_Collections_Generic_IList_T,
126129
SpecialType.System_Collections_Generic_IReadOnlyList_T,

src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs

+60
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@ void M()
4040
", source, expected);
4141
}
4242

43+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
44+
public async Task Test_UseElementAccessInsteadOfFirst_DerivedFromList()
45+
{
46+
await VerifyDiagnosticAndFixAsync(@"
47+
using System.Linq;
48+
using System.Collections.Generic;
49+
50+
class C : List<string>
51+
{
52+
void M()
53+
{
54+
var list = new C();
55+
var x = list.[|First()|];
56+
}
57+
}
58+
", @"
59+
using System.Linq;
60+
using System.Collections.Generic;
61+
62+
class C : List<string>
63+
{
64+
void M()
65+
{
66+
var list = new C();
67+
var x = list[0];
68+
}
69+
}
70+
");
71+
}
72+
4373
[Theory, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
4474
[InlineData("((List<object>)x).[|ElementAt(1)|]", "((List<object>)x)[1]")]
4575
[InlineData("((IList<object>)x).[|ElementAt(1)|]", "((IList<object>)x)[1]")]
@@ -68,6 +98,36 @@ void M()
6898
", source, expected);
6999
}
70100

101+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
102+
public async Task Test_UseElementAccessInsteadOfElementAt_DerivedFromList()
103+
{
104+
await VerifyDiagnosticAndFixAsync(@"
105+
using System.Linq;
106+
using System.Collections.Generic;
107+
108+
class C : List<string>
109+
{
110+
void M()
111+
{
112+
var list = new C();
113+
var x = list.[|ElementAt(1)|];
114+
}
115+
}
116+
", @"
117+
using System.Linq;
118+
using System.Collections.Generic;
119+
120+
class C : List<string>
121+
{
122+
void M()
123+
{
124+
var list = new C();
125+
var x = list[1];
126+
}
127+
}
128+
");
129+
}
130+
71131
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
72132
public async Task TestNoDiagnostic_UseElementAccessInsteadOfElementAt()
73133
{

0 commit comments

Comments
 (0)