Skip to content

Commit ef0096b

Browse files
committed
feat: improve TryGetFirstDescendent logic
1 parent 0c08231 commit ef0096b

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
312312
return;
313313
case "Be" when assertion.IsContainedInType(metadata.NumericAssertionsOfT2):
314314
{
315-
if (invocation.TryGetFirstDescendent<IInvocationOperation>(out var invocationBeforeShould))
315+
if (invocation.TryGetSingleArgumentAs<IInvocationOperation>(out var invocationBeforeShould))
316316
{
317317
switch (invocationBeforeShould.TargetMethod.Name)
318318
{
@@ -334,8 +334,8 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
334334

335335
}
336336
}
337-
var argument = invocation.Arguments[0].Value.UnwrapConversion();
338-
if (argument is IPropertyReferenceOperation propertyBeforeShould)
337+
338+
if (invocation.TryGetSingleArgumentAs<IPropertyReferenceOperation>(out var propertyBeforeShould))
339339
{
340340
switch (propertyBeforeShould.Property.Name)
341341
{

src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,18 @@ public static bool TryGetChainedInvocationAfterAndConstraint(this IInvocationOpe
192192
return false;
193193
}
194194

195+
public static bool TryGetSingleArgumentAs<TOperation>(this IInvocationOperation invocation, out TOperation operation)
196+
{
197+
if (invocation.Arguments.Length is 1 && invocation.Arguments[0].Value.UnwrapConversion() is TOperation op)
198+
{
199+
operation = op;
200+
return true;
201+
}
202+
203+
operation = default;
204+
return false;
205+
}
206+
195207
public static IOperation UnwrapConversion(this IOperation operation)
196208
{
197209
return operation switch

0 commit comments

Comments
 (0)