diff --git a/ChangeLog.md b/ChangeLog.md index 978246f645..60cf048e59 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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 diff --git a/src/Common/CSharp/Analysis/UseElementAccessAnalysis.cs b/src/Common/CSharp/Analysis/UseElementAccessAnalysis.cs index a8f93f4103..184eac1abb 100644 --- a/src/Common/CSharp/Analysis/UseElementAccessAnalysis.cs +++ b/src/Common/CSharp/Analysis/UseElementAccessAnalysis.cs @@ -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; diff --git a/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs b/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs index 6f84f80c7d..f401ebbe28 100644 --- a/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs @@ -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 x = null; + var y = x.Last(); + } +} +", options: WellKnownCSharpTestOptions.Default_CSharp7); + } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)] public async Task TestNoDiagnostic_UseElementAccessInsteadOfLast() {