Skip to content
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

Conversation

ryzngard
Copy link
Contributor

@ryzngard ryzngard commented Feb 12, 2021

Fixes dotnet/runtime#33806 and dotnet/runtime#33807

Definition of Done Checklist

  • Analyzer and Code Fix implemented for C# and VB
  • Run against dotnet/runtime
  • Run against dotnet/roslyn-analyzers
  • Run against dotnet/roslyn
  • Document for review: severity, default, categorization, numbering, titles, messages, and descriptions.

Comment on lines +20 to +24
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.DoNotUseWhenAllWithSingleTaskTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)),
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.DoNotUseWhenAllWithSingleTaskTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)),
Copy link
Member

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?

Copy link
Contributor Author

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.


return editor.GetChangedDocument();
},
equivalenceKey: title),
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 203 to 215
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)|};
Copy link
Member

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.

Copy link
Contributor Author

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)

Base automatically changed from master to main March 5, 2021 02:20
@ryzngard ryzngard force-pushed the feature/task_when_wait_all_analyzer branch 2 times, most recently from ff872d0 to 7a88664 Compare April 8, 2021 19:36
@ryzngard ryzngard marked this pull request as ready for review April 14, 2021 21:23
@ryzngard ryzngard requested a review from a team as a code owner April 14, 2021 21:23
Copy link
Contributor

@mavasani mavasani left a 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>
Copy link
Contributor

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?

Copy link
Contributor Author

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" ?

Copy link
Contributor

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.

Comment on lines +81 to +90
return nameMatches &&
targetMethod.IsStatic &&
SymbolEqualityComparer.Default.Equals(targetMethod.ContainingType, taskType) &&
parameters.Length == 1 &&
parameters[0].IsParams;
}
Copy link
Contributor

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.

Copy link
Contributor

@mavasani mavasani left a 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.

Copy link
Contributor

@mavasani mavasani left a 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.

@ryzngard ryzngard force-pushed the feature/task_when_wait_all_analyzer branch from e2a9359 to 604a1f0 Compare April 16, 2021 22:58
@ryzngard ryzngard changed the base branch from main to release/6.0.1xx-preview4 April 16, 2021 23:00
@ryzngard ryzngard requested a review from mavasani April 21, 2021 20:13
@codecov
Copy link

codecov bot commented Apr 21, 2021

Codecov Report

Merging #4841 (dd00427) into release/6.0.1xx-preview4 (138ab76) will increase coverage by 0.00%.
The diff coverage is 96.88%.

@@                    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     

Copy link
Contributor

@mavasani mavasani left a 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!

ryzngard and others added 3 commits April 22, 2021 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not call Task.WhenAll with a single argument
3 participants