Skip to content

Commit

Permalink
Do not report MA0134 when the ancestor operation is a discard operati…
Browse files Browse the repository at this point in the history
…on (#514)
  • Loading branch information
meziantou authored Apr 28, 2023
1 parent a616828 commit 32e47b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ private static void AnalyzeOperation(OperationAnalysisContext context, Awaitable
var operation = (IInvocationOperation)context.Operation;

var parent = operation.Parent;
if (parent is null or IBlockOperation or IExpressionStatementOperation or IConditionalAccessOperation)

// unwrap all IConditionalAccessOperation
while (parent is IConditionalAccessOperation conditionalAccess)
{
parent = conditionalAccess.Parent;
}

if (parent is null or IBlockOperation or IExpressionStatementOperation)
{
var semanticModel = operation.SemanticModel!;
var position = operation.Syntax.GetLocation().SourceSpan.End;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,6 @@ void A()
.ValidateAsync();
}


[Fact]
public async Task Report_ConditionalInvoke()
{
Expand All @@ -347,4 +346,23 @@ void A(Test instance)
""")
.ValidateAsync();
}

[Fact]
public async Task DoNotReport_Discard_ConditionalInvoke()
{
await CreateProjectBuilder()
.WithSourceCode("""
using System.Threading.Tasks;
class Test
{
Task ReturnTask() => throw null;
void A(Test instance)
{
_ = instance?.ReturnTask();
}
}
""")
.ValidateAsync();
}
}

0 comments on commit 32e47b4

Please sign in to comment.