Skip to content

Commit

Permalink
Fix analyzer RCS1246 (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Sep 27, 2024
1 parent dc7bf9d commit e7ed6ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

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

## [4.12.6] - 2024-09-23

Expand Down
2 changes: 1 addition & 1 deletion src/Common/CSharp/Analysis/UseElementAccessAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static bool IsFixableLast(
if (((CSharpCompilation)semanticModel.Compilation).LanguageVersion < LanguageVersion.CSharp8)
return false;

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

IMethodSymbol methodSymbol = semanticModel.GetReducedExtensionMethodInfo(invocationInfo.InvocationExpression, cancellationToken).Symbol;
Expand Down
18 changes: 18 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,24 @@ void M()
", source, expected);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)]
public async Task Test_UseElementAccessInsteadOfLast_CSharp7()
{
await VerifyNoDiagnosticAsync(@"
using System.Linq;
using System.Collections.Generic;
class C
{
void M()
{
List<string> x = null;
var y = x.Last();
}
}
", options: WellKnownCSharpTestOptions.Default_CSharp7);
}

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

0 comments on commit e7ed6ad

Please sign in to comment.