Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Oct 19, 2024
1 parent 6153c9e commit b1bf37b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Analyzers/CSharp/Analysis/ConfigureAwaitAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -87,7 +88,7 @@ private static void RemoveCallToConfigureAwait(SyntaxNodeAnalysisContext context
// ^^^^^^^^^^^^^^^^^^^^^^
SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(expression);

if (!IsConfigureAwait(invocationInfo))
if (!IsConfigureAwaitFalse(invocationInfo, context.SemanticModel, context.CancellationToken))
return;

ITypeSymbol awaitedType = context.SemanticModel.GetTypeSymbol(expression, context.CancellationToken);
Expand Down Expand Up @@ -133,6 +134,13 @@ private static bool IsConfigureAwait(SimpleMemberInvocationExpressionInfo invoca
&& invocationInfo.Arguments.Count == 1;
}

private static bool IsConfigureAwaitFalse(SimpleMemberInvocationExpressionInfo invocationInfo, SemanticModel semanticModel, CancellationToken cancellationToken)
{
return IsConfigureAwait(invocationInfo)
&& semanticModel.GetConstantValue(invocationInfo.Arguments[0].Expression, cancellationToken).Value is bool boolValue
&& !boolValue;
}

private static bool IsConfigureAwaitable(ITypeSymbol typeSymbol, SemanticModel semanticModel, int position)
{
return semanticModel.LookupSymbols(position, typeSymbol, "ConfigureAwait", includeReducedExtensionMethods: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,23 @@ public Awaitable ConfigureAwait(bool continueOnCapturedContext)
return default(Awaitable);
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ConfigureAwait)]
public async Task TestNoDiagnostic_ConfigureAwaitTrue()
{
await VerifyNoDiagnosticAsync(@"
using System.Threading.Tasks;
class C
{
async Task M()
{
Task task = default;
await task.ConfigureAwait();
}
}
");
}
}

0 comments on commit b1bf37b

Please sign in to comment.