From 5aa65217fa928a42d56e2802e5364edf58696545 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 8 May 2024 22:23:51 +0200 Subject: [PATCH] Fix analyzer RCS1077 (#1463) --- ChangeLog.md | 1 + .../OptimizeLinqMethodCallAnalysis.cs | 3 ++- .../RCS1077OptimizeLinqMethodCallTests.cs | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 1b9117d4ab..72761bcad1 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix analyzer [RCS1246](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1246) ([PR](https://github.com/dotnet/roslynator/pull/1460)) - Fix analyzer [RCS1085](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1085) ([PR](https://github.com/dotnet/roslynator/pull/1461)) +- Fix analyzer [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1077) ([PR](https://github.com/dotnet/roslynator/pull/1463)) ## [4.12.2] - 2024-04-23 diff --git a/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs b/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs index 5a2ca58ee9..9718196baa 100644 --- a/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs +++ b/src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs @@ -232,7 +232,8 @@ public static void AnalyzeFirstOrDefault(SyntaxNodeAnalysisContext context, in S if (parameterCount == 2) { if (parameters[0].Type.OriginalDefinition.IsIEnumerableOfT() - && SymbolUtility.IsPredicateFunc(parameters[1].Type, methodSymbol.TypeArguments[0])) + && SymbolUtility.IsPredicateFunc(parameters[1].Type, methodSymbol.TypeArguments[0]) + && invocationInfo.Arguments[0].Expression is LambdaExpressionSyntax) { ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(invocationInfo.Expression, context.CancellationToken); diff --git a/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs b/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs index 398148e264..fb5f6719ba 100644 --- a/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs @@ -1503,6 +1503,28 @@ void M() Func> M2() => null; } +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)] + public async Task TestNoDiagnostic_CannotConvertFuncToPredicate() + { + await VerifyNoDiagnosticAsync(@" +using System; +using System.Collections.Generic; +using System.Linq; + +class C +{ + void M() + { + var list = new List(); + + Func predicate = null; + + list.FirstOrDefault(predicate); + } +} "); } }