diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs index 812631d..1b86533 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs @@ -659,6 +659,7 @@ public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string asser [DataTestMethod] [AssertionDiagnostic("(actual.Count() + 1).Should().NotBe(unexpected.Count(){0});")] + [AssertionDiagnostic("actual.Count().Should().NotBe((unexpected.Count() + 1){0});")] [AssertionDiagnostic("actual.Count().ToString().Length.Should().NotBe(unexpected.Count(){0});")] [Implemented] public void CollectionShouldNotHaveSameCount_TestNotAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion)); diff --git a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs index add11ac..265cebf 100644 --- a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs +++ b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs @@ -408,7 +408,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs { // TODO: add support for Enumerable.LongCount case nameof(Enumerable.Count) when IsEnumerableMethodWithoutArguments(invocationBeforeShould, metadata): - if (assertion.Arguments[0].HasFirstDescendentInvocation(nameof(Enumerable.Count))) + if (assertion.TryGetFirstArgumentAs(out var assertionInvocation) && IsEnumerableMethodWithoutArguments(assertionInvocation, metadata)) { context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount)); } diff --git a/src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs b/src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs index 22a9c12..5f44106 100644 --- a/src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs +++ b/src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs @@ -60,11 +60,6 @@ public static TOperation GetFirstAncestor(this IOperation parent) wh return default; } - public static bool HasFirstDescendentInvocation(this IOperation parent, string invocationMethod) - { - return parent.TryGetFirstDescendent(out var invocation) && invocation.TargetMethod.Name == invocationMethod; - } - public static bool IsContainedInType(this IPropertyReferenceOperation property, SpecialType type) => property.Property.ContainingType.ConstructedFromType(type); public static bool IsContainedInType(this IPropertyReferenceOperation property, INamedTypeSymbol type) @@ -204,6 +199,18 @@ public static bool TryGetSingleArgumentAs(this IInvocationOperation return false; } + public static bool TryGetFirstArgumentAs(this IInvocationOperation invocation, out TOperation operation) + { + if (invocation.Arguments.Length is >= 1 && invocation.Arguments[0].Value.UnwrapConversion() is TOperation op) + { + operation = op; + return true; + } + + operation = default; + return false; + } + public static IOperation UnwrapConversion(this IOperation operation) { return operation switch