From fca5b4e8faed5db4d89c499ba380ef89f9c50a77 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Fri, 27 Sep 2024 17:33:49 +0200 Subject: [PATCH] Fix analyzer RCS1077 - Handle IQueryable (#1544) --- ChangeLog.md | 1 + .../Analysis/OptimizeLinqMethodCallAnalysis.cs | 8 ++++++++ .../RCS1077OptimizeLinqMethodCallTests.cs | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 53c8de6666..856b5f0583 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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)) - Fix analyzer [RCS1140](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1140) ([PR](https://github.com/dotnet/roslynator/pull/1524)) +- Fix analyzer [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1077) ([PR](https://github.com/dotnet/roslynator/pull/1544)) ## [4.12.6] - 2024-09-23 diff --git a/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs b/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs index 4a1dec85be..935cc02a0d 100644 --- a/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs +++ b/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs @@ -718,6 +718,14 @@ public static void AnalyzeOrderByIdentity(SyntaxNodeAnalysisContext context, in if (lambdaExpression.Body is not IdentifierNameSyntax identifier || identifier.Identifier.Text != lambdaExpression.Parameter.Identifier.Text) return; + if (context.SemanticModel + .GetTypeSymbol(invocationInfo.Expression, context.CancellationToken)? + .OriginalDefinition + .HasMetadataName(MetadataNames.System_Linq_IQueryable_T) == true) + { + return; + } + TextSpan span = TextSpan.FromBounds(invocationInfo.Name.SpanStart, invocationExpression.Span.End); Report(context, invocationExpression, span, checkDirectives: true); } diff --git a/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs b/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs index 990a40c3d1..e3ed4de296 100644 --- a/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs @@ -1560,6 +1560,23 @@ void M() list.FirstOrDefault(predicate); } } +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)] + public async Task TestNoDiagnostic_OrderByToOrder_IQueryable() + { + await VerifyNoDiagnosticAsync(@" +using System.Linq; + +class C +{ + void M() + { + IQueryable q = null; + var x = q.OrderBy(f => f); + } +} "); } }