-
Notifications
You must be signed in to change notification settings - Fork 463
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
Add WhenAll and WaitAll analyzer for single task argument #4841
Add WhenAll and WaitAll analyzer for single task argument #4841
Conversation
...CSharp/Microsoft.NetCore.Analyzers/Tasks/CSharpDoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.DoNotUseWhenAllWithSingleTaskTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), | ||
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.DoNotUseWhenAllWithSingleTaskTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), |
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.
Was it intentional to pass the title as the message too?
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.
In this case they are the same, so I didn't add a separate message.
...alyzers/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Outdated
Show resolved
Hide resolved
...alyzers/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Outdated
Show resolved
Hide resolved
|
||
return editor.GetChangedDocument(); | ||
}, | ||
equivalenceKey: title), |
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.
IIRC, there was a comment from @sharwell somewhere that equivalence key shouldn't be localized and use nameof(Resources.FixTitle) instead. I don't know the reason though, but just pointing this out.
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.
...rs/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer.cs
Outdated
Show resolved
Hide resolved
var fixedSource = @" | ||
using System.Threading.Tasks; | ||
|
||
class C | ||
{ | ||
async Task M() | ||
{ | ||
var t1 = CreateTask(); | ||
var objectTask1 = CreateObjectTask(); | ||
|
||
await t1; | ||
await CreateTask(); | ||
var t1WhenAll = {|CA2250:Task.WhenAll(t1)|}; |
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 fixed code seems incorrect.
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.
var returnedTask = Task.WhenAll(t1)
was recommended to remain unfixed as designed in this comment dotnet/runtime#33806 (comment)
ff872d0
to
7a88664
Compare
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.
New .NET6 analyzers need to target release/6.0.1xx-preview4
branch
<data name="DoNotUseWaitAllWithSingleTaskDescription" xml:space="preserve"> | ||
<value>Using 'WaitAll' with a single task should be avoided in favor of directly awaiting that task or returning it as is.</value> |
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.
Can we also include the why
part in the description?
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.
Is it enough to say something like
"Using 'WaitAll' may result in performance loss, await or return the task 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.
That sounds fine to me.
...alyzers/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Outdated
Show resolved
Hide resolved
...alyzers/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Outdated
Show resolved
Hide resolved
...alyzers/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Show resolved
Hide resolved
...alyzers/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Outdated
Show resolved
Hide resolved
return nameMatches && | ||
targetMethod.IsStatic && | ||
SymbolEqualityComparer.Default.Equals(targetMethod.ContainingType, taskType) && | ||
parameters.Length == 1 && | ||
parameters[0].IsParams; | ||
} |
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.
Not critical, but I think a bit more performant implementation would be to compute and store the method symbols for Task.WhenAll
and Task.WaitAll
upfront in the compilation start action and then this method would just need to perform symbol equality checks.
...rs/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer.cs
Show resolved
Hide resolved
...rs/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer.cs
Outdated
Show resolved
Hide resolved
...rs/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer.cs
Outdated
Show resolved
Hide resolved
...itTests/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgumentTests.cs
Outdated
Show resolved
Hide resolved
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.
Also please make sure to run msbuild /v:m /t:pack
at the root of the repo for auto-updating the documentation files in the repo and push the changes to the PR.
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.
Overall, this looks great to me. Please address the suggestions and this should be good to merge.
Co-authored-by: Youssef Victor <31348972+Youssef1313@users.noreply.github.com>
e2a9359
to
604a1f0
Compare
… feature/task_when_wait_all_analyzer
Codecov Report
@@ Coverage Diff @@
## release/6.0.1xx-preview4 #4841 +/- ##
==========================================================
Coverage 95.55% 95.55%
==========================================================
Files 1200 1205 +5
Lines 273798 274343 +545
Branches 16611 16632 +21
==========================================================
+ Hits 261624 262146 +522
- Misses 9978 9989 +11
- Partials 2196 2208 +12 |
...alyzers/Core/Microsoft.NetCore.Analyzers/Tasks/DoNotUseWhenAllOrWaitAllWithSingleArgument.cs
Outdated
Show resolved
Hide resolved
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. Feel free to merge when ready. Thanks!
…seWhenAllOrWaitAllWithSingleArgument.cs Co-authored-by: Manish Vasani <mavasani@microsoft.com>
Fixes dotnet/runtime#33806 and dotnet/runtime#33807
Definition of Done Checklist