Skip to content

Commit e7ed6ad

Browse files
authored
Fix analyzer RCS1246 (#1543)
1 parent dc7bf9d commit e7ed6ad

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Fix analyzer [RCS1202](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1202) ([PR](https://github.com/dotnet/roslynator/pull/1542))
13+
- Fix analyzer [RCS1246](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1246) ([PR](https://github.com/dotnet/roslynator/pull/1543))
1314

1415
## [4.12.6] - 2024-09-23
1516

src/Common/CSharp/Analysis/UseElementAccessAnalysis.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static bool IsFixableLast(
7777
if (((CSharpCompilation)semanticModel.Compilation).LanguageVersion < LanguageVersion.CSharp8)
7878
return false;
7979

80-
if (semanticModel.Compilation.GetTypeByMetadataName("System.Index") is null)
80+
if (semanticModel.Compilation.GetTypeByMetadataName("System.Index")?.DeclaredAccessibility != Accessibility.Public)
8181
return false;
8282

8383
IMethodSymbol methodSymbol = semanticModel.GetReducedExtensionMethodInfo(invocationInfo.InvocationExpression, cancellationToken).Symbol;

src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ void M()
182182
", source, expected);
183183
}
184184

185+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
186+
public async Task Test_UseElementAccessInsteadOfLast_CSharp7()
187+
{
188+
await VerifyNoDiagnosticAsync(@"
189+
using System.Linq;
190+
using System.Collections.Generic;
191+
192+
class C
193+
{
194+
void M()
195+
{
196+
List<string> x = null;
197+
var y = x.Last();
198+
}
199+
}
200+
", options: WellKnownCSharpTestOptions.Default_CSharp7);
201+
}
202+
185203
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
186204
public async Task TestNoDiagnostic_UseElementAccessInsteadOfLast()
187205
{

0 commit comments

Comments
 (0)