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

Analyzer + Fixer: Forward cancellation token to method invocations #3641

Merged
merged 20 commits into from
Jun 22, 2020

Conversation

carlossanlop
Copy link
Member

@carlossanlop carlossanlop commented May 18, 2020

CA2016: Forward CancellationToken to methods.

Fixes dotnet/runtime#33774

Summary

The rule should try to identify places where a CancellationToken (ct) should have been passed but wasn't. For example, in a method that takes a ct, a method is called that has an overload that takes a ct but a shorter overload that doesn't take a ct was used instead.

Conditions for positive cases

  • The node to analyze is an awaited invocation, or is the origin invocation of a ConfigureAwait.
  • The parent method signature has a ct parameter.
  • The invocation is not receiving a ct argument.
  • The invocation method either:
    • Only has one method version, but the signature has one optional ct.
    • Has a method overload with the exact same arguments in the same order, plus one ct parameter at the end.
  • A normal method, a lambda or a nested method receives a ct, and there's an awaited invocation inside it.

Conditions for negative cases

  • The parent method signature does not have a ct parameter.
  • The node to analyze is not an awaited invocation.
  • The awaited invocation is already receiving a ct argument.
  • The invocation method does not have an overload with the exact same arguments that also receives a ct.
  • The user is using the overload that takes the ct, but is already passing a value. Can be default or default(CancellationToken) in C#, Nothing in VB, or CancellationToken.None, or the token from a CancellationTokenSource, or something else.
  • If the overload that receives a ct receives more than one ct, do not suggest it.

Pending to address

  • Case where CancellationToken is renamed among the usings directives (is it called an alias?).
  • Trivia
  • Ensure we have unit tests verifying false negatives with the cases not handled below (potential future improvements).
  • Appropriate strings to message, title and description.

Potential future improvements

  • Finding an overload with one ct parameter, but it is not located in the last position. For example, some ContinueWith overloads have the ct in the middle.
  • Passing a named ct in a different position than the last.
  • Detecting extension methods as suggested in this comment.

@carlossanlop carlossanlop requested a review from a team as a code owner May 18, 2020 22:41
@carlossanlop carlossanlop self-assigned this May 18, 2020
@carlossanlop carlossanlop requested review from marklio and AArnott May 18, 2020 22:42
@AArnott
Copy link
Contributor

AArnott commented May 18, 2020

Why the focus on whether awaits exist? This rule should apply to synchronous methods just as much as async ones. If you search dotnet/runtime#33774 for the term "async" you'll find a few discussion items on this question where @stephentoub agreed with this assertion.

@carlossanlop
Copy link
Member Author

I would like to read what people think about the "Potential future improvements" section in the main description - is it ok to get them addressed later, or do you think I should address them now?

@carlossanlop carlossanlop requested a review from mavasani June 18, 2020 18:08
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, looks great to me! Thanks for the extensive test suite! I have some suggestions around certain conditions that seem always false in the analyzer and other minor nit comments.

I am really looking forward to enabling this analyzer in all our repos!! Thanks.

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.

Thanks!

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.

Forward CancellationToken to methods that can take it
6 participants