diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs index d9ee998b..941d5676 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/SanityTests.cs @@ -448,5 +448,62 @@ public class MyCollectionType { }"; }) ); } + + [TestMethod] + [Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/290")] + public void ShouldNotReportIssue290() + { + const string source = @" +using FluentAssertions; +using FluentAssertions.Extensions; +using System; +using System.Collections.Generic; +using System.Linq; +public class TestClass +{ + public static void Main() + { + IEnumerable expectedOrderedNames = new[] { new Item(""Alpha""), new Item(""Bravo""), new Item(""Charlie"") }; + IEnumerable actual = GetSortedItems(); + + actual.Select(x => x.Item).Should().Equal(expectedOrderedNames); + } + + static IEnumerable GetSortedItems() + { + yield return new Parent(""Bravo""); + yield return new Parent(""Charlie""); + yield return new Parent(""Alpha""); + } +} + +public class Item +{ + public string Name { get; set; } + public Guid Id { get; set; } + + public Item(string name) + { + Name = name; + Id = Guid.NewGuid(); + } +} + +public class Parent +{ + public Item Item { get; set; } + + public Parent(string name) + { + Item = new Item(name); + } +}"; + + DiagnosticVerifier.VerifyDiagnostic(new DiagnosticVerifierArguments() + .WithSources(source) + .WithAllAnalyzers() + .WithPackageReferences(PackageReference.FluentAssertions_6_12_0) + ); + } } } \ No newline at end of file diff --git a/src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs b/src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs index fe36a6d3..4389524d 100644 --- a/src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs +++ b/src/FluentAssertions.Analyzers/Tips/DiagnosticMetadata.cs @@ -47,7 +47,7 @@ private DiagnosticMetadata(string message, string helpLink, [CallerMemberName] s public static DiagnosticMetadata CollectionShouldHaveElementAt_SkipFirstShouldBe { get; } = new("Use .Should().HaveElementAt()", GetHelpLink("Collections-26")); public static DiagnosticMetadata CollectionShouldBeInAscendingOrder_OrderByShouldEqual { get; } = new("Use .Should().BeInAscendingOrder()", GetHelpLink("Collections-27")); public static DiagnosticMetadata CollectionShouldBeInDescendingOrder_OrderByDescendingShouldEqual { get; } = new("Use .Should().BeInDescendingOrder()", GetHelpLink("Collections-28")); - public static DiagnosticMetadata CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect { get; } = new("Use .Should().BeEquivalentTo()", GetHelpLink("Collections-29")); + public static DiagnosticMetadata CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect { get; } = new("Use .Should().Equal()", GetHelpLink("Collections-29")); public static DiagnosticMetadata CollectionShouldNotIntersectWith_IntersectShouldBeEmpty { get; } = new("Use .Should().NotIntersectWith()", GetHelpLink("Collections-30")); public static DiagnosticMetadata CollectionShouldIntersectWith_IntersectShouldNotBeEmpty { get; } = new("Use .Should().IntersectWith()", GetHelpLink("Collections-31")); public static DiagnosticMetadata CollectionShouldNotContainNulls_SelectShouldNotContainNulls { get; } = new("Use .Should().NotContainNulls()", GetHelpLink("Collections-32")); diff --git a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsOperationAnalyzer.cs b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsOperationAnalyzer.cs index 2bf2b612..d83500b8 100644 --- a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsOperationAnalyzer.cs +++ b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsOperationAnalyzer.cs @@ -179,7 +179,8 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs case nameof(Enumerable.OrderByDescending) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata) && invocationBeforeShould.Arguments[0].IsSameArgumentReference(assertion.Arguments[0]): context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldBeInDescendingOrder_OrderByDescendingShouldEqual)); return; - case nameof(Enumerable.Select) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata): + case nameof(Enumerable.Select) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata) + && assertion.Arguments[0].Value is IInvocationOperation { TargetMethod.Name: nameof(Enumerable.Select), Arguments.Length: 2 } expectedInvocation && expectedInvocation.Arguments[1].IsLambda(): context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldEqualOtherCollectionByComparer_SelectShouldEqualOtherCollectionSelect)); return; }