Skip to content

Commit ea6971e

Browse files
authored
Update analyzer RCS1077 - Remove suggestion to change FirstOrDefault to Find (#1563)
1 parent c37e24e commit ea6971e

File tree

4 files changed

+10
-60
lines changed

4 files changed

+10
-60
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Change
11+
12+
- Update analyzer [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1077) ([PR](https://github.com/dotnet/roslynator/pull/1653))
13+
- Do not suggest to change `list.FirstOrDefault(predicate)` to `list.Find(predicate)`.
14+
Performance gain is negligible and actually `FirstOrDefault` can be even faster on .NET 9 (see related [issue](https://github.com/dotnet/roslynator/pull/1531) for more details).
15+
1016
## [4.12.8] - 2024-10-11
1117

1218
### Fixed

src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeLinqMethodCallCodeFixProvider.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,6 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
175175
ct => CallCastInsteadOfSelectAsync(document, invocation, ct),
176176
GetEquivalenceKey(diagnostic, "CallCastInsteadOfSelect"));
177177

178-
context.RegisterCodeFix(codeAction, diagnostic);
179-
return;
180-
}
181-
case "FirstOrDefault":
182-
{
183-
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
184-
185-
CodeAction codeAction = CodeAction.Create(
186-
"Call 'Find' instead of 'FirstOrDefault'",
187-
ct => CallFindInsteadOfFirstOrDefaultAsync(document, invocationInfo, ct),
188-
GetEquivalenceKey(diagnostic, "CallFindInsteadOfFirstOrDefault"));
189-
190178
context.RegisterCodeFix(codeAction, diagnostic);
191179
return;
192180
}
@@ -436,16 +424,6 @@ private static Task<Document> CallCastInsteadOfSelectAsync(
436424
return document.ReplaceNodeAsync(invocationExpression, newInvocationExpression, cancellationToken);
437425
}
438426

439-
private static Task<Document> CallFindInsteadOfFirstOrDefaultAsync(
440-
Document document,
441-
in SimpleMemberInvocationExpressionInfo invocationInfo,
442-
CancellationToken cancellationToken)
443-
{
444-
IdentifierNameSyntax newName = IdentifierName("Find").WithTriviaFrom(invocationInfo.Name);
445-
446-
return document.ReplaceNodeAsync(invocationInfo.Name, newName, cancellationToken);
447-
}
448-
449427
public static Task<Document> UseCountOrLengthPropertyInsteadOfCountMethodAsync(
450428
Document document,
451429
InvocationExpressionSyntax invocation,

src/Analyzers/CSharp/Analysis/OptimizeLinqMethodCallAnalysis.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,6 @@ public static void AnalyzeFirstOrDefault(SyntaxNodeAnalysisContext context, in S
235235
&& SymbolUtility.IsPredicateFunc(parameters[1].Type, methodSymbol.TypeArguments[0])
236236
&& invocationInfo.Arguments[0].Expression is LambdaExpressionSyntax)
237237
{
238-
ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(invocationInfo.Expression, context.CancellationToken);
239-
240-
if (typeSymbol?.OriginalDefinition.EqualsOrInheritsFrom(MetadataNames.System_Collections_Generic_List_T) == true)
241-
{
242-
Report(context, invocationInfo.Name);
243-
return;
244-
}
245-
246238
success = true;
247239
}
248240
}

src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -679,20 +679,7 @@ void M()
679679
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
680680
public async Task Test_CallFindInsteadOfFirstOrDefault_List()
681681
{
682-
await VerifyDiagnosticAndFixAsync(@"
683-
using System.Collections.Generic;
684-
using System.Linq;
685-
686-
class C
687-
{
688-
void M()
689-
{
690-
var items = new List<object>();
691-
692-
var x = items.[|FirstOrDefault|](_ => true);
693-
}
694-
}
695-
", @"
682+
await VerifyNoDiagnosticAsync(@"
696683
using System.Collections.Generic;
697684
using System.Linq;
698685
@@ -702,7 +689,7 @@ void M()
702689
{
703690
var items = new List<object>();
704691
705-
var x = items.Find(_ => true);
692+
var x = items.FirstOrDefault(_ => true);
706693
}
707694
}
708695
");
@@ -711,20 +698,7 @@ void M()
711698
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
712699
public async Task Test_CallFindInsteadOfFirstOrDefault_DerivedFromList()
713700
{
714-
await VerifyDiagnosticAndFixAsync(@"
715-
using System.Collections.Generic;
716-
using System.Linq;
717-
718-
class C : List<object>
719-
{
720-
void M()
721-
{
722-
var items = new C();
723-
724-
var x = items.[|FirstOrDefault|](_ => true);
725-
}
726-
}
727-
", @"
701+
await VerifyNoDiagnosticAsync(@"
728702
using System.Collections.Generic;
729703
using System.Linq;
730704
@@ -734,7 +708,7 @@ void M()
734708
{
735709
var items = new C();
736710
737-
var x = items.Find(_ => true);
711+
var x = items.FirstOrDefault(_ => true);
738712
}
739713
}
740714
");

0 commit comments

Comments
 (0)