Skip to content

Commit 28bd21d

Browse files
authored
Fix analyzer RCS1090 - Check ConfigureAwait argument value (#1566)
1 parent 6153c9e commit 28bd21d

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fix analyzer [RCS1090](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1090) ([PR](https://github.com/dotnet/roslynator/pull/1566))
13+
1014
### Change
1115

1216
- Update analyzer [RCS1077](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1077) ([PR](https://github.com/dotnet/roslynator/pull/1653))

src/Analyzers/CSharp/Analysis/ConfigureAwaitAnalyzer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System.Collections.Immutable;
44
using System.Linq;
5+
using System.Threading;
56
using Microsoft.CodeAnalysis;
67
using Microsoft.CodeAnalysis.CSharp;
78
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -87,7 +88,7 @@ private static void RemoveCallToConfigureAwait(SyntaxNodeAnalysisContext context
8788
// ^^^^^^^^^^^^^^^^^^^^^^
8889
SimpleMemberInvocationExpressionInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationExpressionInfo(expression);
8990

90-
if (!IsConfigureAwait(invocationInfo))
91+
if (!IsConfigureAwaitFalse(invocationInfo, context.SemanticModel, context.CancellationToken))
9192
return;
9293

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

137+
private static bool IsConfigureAwaitFalse(SimpleMemberInvocationExpressionInfo invocationInfo, SemanticModel semanticModel, CancellationToken cancellationToken)
138+
{
139+
return IsConfigureAwait(invocationInfo)
140+
&& semanticModel.GetConstantValue(invocationInfo.Arguments[0].Expression, cancellationToken).Value is bool boolValue
141+
&& !boolValue;
142+
}
143+
136144
private static bool IsConfigureAwaitable(ITypeSymbol typeSymbol, SemanticModel semanticModel, int position)
137145
{
138146
return semanticModel.LookupSymbols(position, typeSymbol, "ConfigureAwait", includeReducedExtensionMethods: true)

src/Tests/Analyzers.Tests/RCS1090RemoveCallToConfigureAwaitTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,23 @@ public Awaitable ConfigureAwait(bool continueOnCapturedContext)
553553
return default(Awaitable);
554554
}
555555
}
556+
");
557+
}
558+
559+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ConfigureAwait)]
560+
public async Task TestNoDiagnostic_ConfigureAwaitTrue()
561+
{
562+
await VerifyNoDiagnosticAsync(@"
563+
using System.Threading.Tasks;
564+
565+
class C
566+
{
567+
async Task M()
568+
{
569+
Task task = default;
570+
await task.ConfigureAwait(true);
571+
}
572+
}
556573
");
557574
}
558575
}

0 commit comments

Comments
 (0)