Skip to content

Commit

Permalink
Fix RCS1077 (#1428)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Mar 24, 2024
1 parent 8330609 commit 5698e08
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix analyzer [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1077) ([PR](https://github.com/dotnet/roslynator/pull/1428))

## [4.12.0] - 2024-03-19

### Added
Expand Down
Expand Up @@ -236,7 +236,7 @@ public static void AnalyzeFirstOrDefault(SyntaxNodeAnalysisContext context, in S
{
ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(invocationInfo.Expression, context.CancellationToken);

if (typeSymbol?.OriginalDefinition.HasMetadataName(MetadataNames.System_Collections_Generic_List_T) == true)
if (typeSymbol?.OriginalDefinition.EqualsOrInheritsFrom(MetadataNames.System_Collections_Generic_List_T) == true)
{
Report(context, invocationInfo.Name);
return;
Expand Down
32 changes: 32 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1077OptimizeLinqMethodCallTests.cs
Expand Up @@ -708,6 +708,38 @@ 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);
}
}
", @"
using System.Collections.Generic;
using System.Linq;
class C : List<object>
{
void M()
{
var items = new C();
var x = items.Find(_ => true);
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeLinqMethodCall)]
public async Task Test_CallFindInsteadOfFirstOrDefault_Array()
{
Expand Down

0 comments on commit 5698e08

Please sign in to comment.