Skip to content

Commit

Permalink
Fix analyzer RCS1077 (#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored May 8, 2024
1 parent 69b5962 commit 5aa6521
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
22 changes: 22 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,28 @@ void M()
Func<string, IEnumerable<string>> 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<string>();
Func<string, bool> predicate = null;
list.FirstOrDefault(predicate);
}
}
");
}
}

0 comments on commit 5aa6521

Please sign in to comment.