-
Notifications
You must be signed in to change notification settings - Fork 237
New Rule S6966: Awaitable method should be used #9101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
...ers/its/expected/CSharpLatest/S6966-NetCore31.MVC.ConfigurableRules-netcoreapp3.1.Views.json
Show resolved
Hide resolved
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore6-net6.0.json
Show resolved
Hide resolved
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore7-net7.0.json
Show resolved
Hide resolved
analyzers/its/expected/ManuallyAddedNoncompliantIssues.CS/S6966-AspNetCore8-net8.0.json
Show resolved
Hide resolved
analyzers/its/expected/akka.net/S6966-Akka.Cluster.Sharding-netstandard2.0.json
Show resolved
Hide resolved
|
|
||
| namespace Microsoft.CodeAnalysis.Shared.Extensions; | ||
|
|
||
| [GeneratedCode("Copied from Roslyn", "ca66296efa86bd8078508fe7b38b91b415364f78")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to myself:
Needs to be changed to [ExcludeFromCodeCoverage]
https://sonarsource.slack.com/archives/C06S7H06Z4Y/p1713354144963039?thread_ts=1713264344.421699&cid=C06S7H06Z4Y
| #if NET | ||
|
|
||
| [TestMethod] | ||
| public void UseAwaitableMethod_CS_Test() => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This snippet is for testing only and will be removed before merging
zsolt-kolbay-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Left a bunch of polishing comments, but nothing major.
|
|
||
| ReturnMethod(); // Noncompliant | ||
| _ = ReturnMethod(); // Noncompliant | ||
| this.ReturnMethod().ReturnMethod().ReturnMethod(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the following test case:
await ReturnMethod().ReturnMethodAsync(); // Noncompliant
// ^^^^^^^^^^^^^^| public static C operator -(C c) => default(C); | ||
| public static C operator -(C c1, C c2) => default(C); | ||
| public static C operator !(C c) => default(C); | ||
| public static C operator ~(C c) => default(C); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ~ operator is not used.
| using System; | ||
|
|
||
| var ms = new MemoryStream(); | ||
| ms.Dispose(); // Noncompliant {{Await DisposeAsync instead.}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rule can be extended to also raise a warning a using statement without the await keyword in an async context (or maybe as a new rule?).
using var ms1 = new MemoryStream(); // Noncompliant
await using var ms2 = new MemoryStream(); // CompliantWDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 That requires a different implementation. Please create a Rule Idea issue.
| return wellKnownExtensionMethodContainer; | ||
| } | ||
|
|
||
| private static IEnumerable<IMethodSymbol> GetMethodSymbolsInScope(string methodName, WellKnownExtensionMethodContainer wellKnownExtensionMethodContainer, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this and the following method below FindAwaitableAlternatives.
|
Other well known extension methods to look at: |
| if (awaitableRoot is { Parent: AwaitExpressionSyntax }) | ||
| { | ||
| return ImmutableArray<ISymbol>.Empty; // Invocation result is already awaited. | ||
| } | ||
| if (invocationExpression.EnclosingScope() is { } scope && !IsAsyncCodeBlock(scope)) | ||
| { | ||
| return ImmutableArray<ISymbol>.Empty; // Not in an async scope | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two conditions can be merged into a single if statement.
zsolt-kolbay-sonarsource
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Co-authored-by: Zsolt Kolbay <[email protected]>
ab1fc63 to
3d79d9d
Compare
|
|
This reverts commit 02fcc9e.
|
Peach validation:
PerformanceThe rule is about 3 times as expensive as other rules, like |
|
Peach validation: 430 issues found. All are TP with the following exceptions:
Most of the violations originate from (in that order)
We also have found issues in razor files ( |




Fixes #9096