From 7791fc24b5a8df6abaa8617f1f952716358d7b7e Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 27 Sep 2024 11:43:55 +0200 Subject: [PATCH 1/3] update --- .../CSharp/Analysis/UseElementAccessAnalysis.cs | 2 +- .../RCS1246UseElementAccessTests.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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..858f36e28f 100644 --- a/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs @@ -182,6 +182,23 @@ void M() ", source, expected); } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseElementAccess)] + public async Task Test_UseElementAccessInsteadOfLast_CSharp7() + { + await VerifyNoDiagnosticAsync(@" +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() { From ff516e8d64647a45eefb0769bf0a46f0ab433599 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 27 Sep 2024 11:44:59 +0200 Subject: [PATCH 2/3] changelog --- ChangeLog.md | 1 + 1 file changed, 1 insertion(+) 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 From 7085342063e440103194d410fcd0513e475441e3 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 27 Sep 2024 15:25:23 +0200 Subject: [PATCH 3/3] fix --- src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs b/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs index 858f36e28f..f401ebbe28 100644 --- a/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1246UseElementAccessTests.cs @@ -186,6 +186,7 @@ void M() public async Task Test_UseElementAccessInsteadOfLast_CSharp7() { await VerifyNoDiagnosticAsync(@" +using System.Linq; using System.Collections.Generic; class C