Skip to content

Commit 8fb8e8b

Browse files
committed
bugfix: improve CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount detection
1 parent 445c91b commit 8fb8e8b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
408408
{
409409
// TODO: add support for Enumerable.LongCount
410410
case nameof(Enumerable.Count) when IsEnumerableMethodWithoutArguments(invocationBeforeShould, metadata):
411-
if (assertion.TryGetSingleArgumentAs<IInvocationOperation>(out var assertionInvocation) && assertionInvocation.TargetMethod.Name is nameof(Enumerable.Count))
411+
if (assertion.TryGetFirstArgumentAs<IInvocationOperation>(out var assertionInvocation) && IsEnumerableMethodWithoutArguments(assertionInvocation, metadata))
412412
{
413413
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount));
414414
}

src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ public static bool TryGetSingleArgumentAs<TOperation>(this IInvocationOperation
199199
return false;
200200
}
201201

202+
public static bool TryGetFirstArgumentAs<TOperation>(this IInvocationOperation invocation, out TOperation operation)
203+
{
204+
if (invocation.Arguments.Length is >= 1 && invocation.Arguments[0].Value.UnwrapConversion() is TOperation op)
205+
{
206+
operation = op;
207+
return true;
208+
}
209+
210+
operation = default;
211+
return false;
212+
}
213+
202214
public static IOperation UnwrapConversion(this IOperation operation)
203215
{
204216
return operation switch

0 commit comments

Comments
 (0)