Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update analyzer RCS1077 - Remove suggestion to change FirstOrDefault to Find #1563

Merged
merged 4 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Change

- Update analyzer [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1077) ([PR](https://github.com/dotnet/roslynator/pull/1653))
- Do not suggest to change `list.FirstOrDefault(predicate)` to `list.Find(predicate)`.
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).

## [4.12.8] - 2024-10-11

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,6 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
ct => CallCastInsteadOfSelectAsync(document, invocation, ct),
GetEquivalenceKey(diagnostic, "CallCastInsteadOfSelect"));

context.RegisterCodeFix(codeAction, diagnostic);
return;
}
case "FirstOrDefault":
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

CodeAction codeAction = CodeAction.Create(
"Call 'Find' instead of 'FirstOrDefault'",
ct => CallFindInsteadOfFirstOrDefaultAsync(document, invocationInfo, ct),
GetEquivalenceKey(diagnostic, "CallFindInsteadOfFirstOrDefault"));

context.RegisterCodeFix(codeAction, diagnostic);
return;
}
Expand Down Expand Up @@ -436,16 +424,6 @@ private static Task<Document> CallCastInsteadOfSelectAsync(
return document.ReplaceNodeAsync(invocationExpression, newInvocationExpression, cancellationToken);
}

private static Task<Document> CallFindInsteadOfFirstOrDefaultAsync(
Document document,
in SimpleMemberInvocationExpressionInfo invocationInfo,
CancellationToken cancellationToken)
{
IdentifierNameSyntax newName = IdentifierName("Find").WithTriviaFrom(invocationInfo.Name);

return document.ReplaceNodeAsync(invocationInfo.Name, newName, cancellationToken);
}

public static Task<Document> UseCountOrLengthPropertyInsteadOfCountMethodAsync(
Document document,
InvocationExpressionSyntax invocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,6 @@ public static void AnalyzeFirstOrDefault(SyntaxNodeAnalysisContext context, in S
&& SymbolUtility.IsPredicateFunc(parameters[1].Type, methodSymbol.TypeArguments[0])
&& invocationInfo.Arguments[0].Expression is LambdaExpressionSyntax)
{
ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(invocationInfo.Expression, context.CancellationToken);

if (typeSymbol?.OriginalDefinition.EqualsOrInheritsFrom(MetadataNames.System_Collections_Generic_List_T) == true)
{
Report(context, invocationInfo.Name);
return;
}

success = true;
}
}
Expand Down
34 changes: 4 additions & 30 deletions src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -679,20 +679,7 @@ void M()
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
public async Task Test_CallFindInsteadOfFirstOrDefault_List()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Collections.Generic;
using System.Linq;

class C
{
void M()
{
var items = new List<object>();

var x = items.[|FirstOrDefault|](_ => true);
}
}
", @"
await VerifyNoDiagnosticAsync(@"
using System.Collections.Generic;
using System.Linq;

Expand All @@ -702,7 +689,7 @@ void M()
{
var items = new List<object>();

var x = items.Find(_ => true);
var x = items.FirstOrDefault(_ => true);
}
}
");
Expand All @@ -711,20 +698,7 @@ void M()
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
public async Task Test_CallFindInsteadOfFirstOrDefault_DerivedFromList()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Collections.Generic;
using System.Linq;

class C : List<object>
{
void M()
{
var items = new C();

var x = items.[|FirstOrDefault|](_ => true);
}
}
", @"
await VerifyNoDiagnosticAsync(@"
using System.Collections.Generic;
using System.Linq;

Expand All @@ -734,7 +708,7 @@ void M()
{
var items = new C();

var x = items.Find(_ => true);
var x = items.FirstOrDefault(_ => true);
}
}
");
Expand Down