Skip to content

Commit

Permalink
Fix analyzer RCS1090 - Check ConfigureAwait argument value (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Oct 19, 2024
1 parent 6153c9e commit 28bd21d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fix analyzer [RCS1090](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1090) ([PR](https://github.com/dotnet/roslynator/pull/1566))

### Change

- Update analyzer [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1077) ([PR](https://github.com/dotnet/roslynator/pull/1653))
Expand Down
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(true);
}
}
");
}
}

0 comments on commit 28bd21d

Please sign in to comment.