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

Apply CA2016: Forward CancellationToken to methods that can take it #37607

Merged
merged 13 commits into from
Jul 15, 2020

Conversation

carlossanlop
Copy link
Member

This PR applies Roslyn analyzer CA2016: It identified methods that had a CancellationToken parameter, and then looked inside its method body for any method invocations that could accept a token as an argument, but weren't taking it yet.

The Roslyn analyzer/fixer is up for review here: dotnet/roslyn-analyzers#3641
Original issue here: #33774

@mavasani there were a couple of cases were the rule behaved strange:

A) In MockConnection.cs, the ReadAsync method is located in an extension class. The rule wasn't supposed to look for extension method overloads, but it did anyway, and offered a fix. Applying the fix causes the object to be substituted with the extension class, and the code now does not compile. Here's the unit test that verifies we don't offer a diagnostic when the overload is in an extension class, and it is passing. Do you know what's causing this unexpected diagnostic to be triggered?

B) In HttpContentJsonExtensions.cs, the rule triggered a diagnostic and offered a fix, but applying it would cause a build break because the method overload that takes a token is not available in the same .NET Standard version, so I decided to wrap those invocations with a pragma directive to prevent getting the rule triggered. If this is something I can detect on the analyzer side, let me know.

@carlossanlop carlossanlop added this to the 5.0 milestone Jun 8, 2020
@carlossanlop carlossanlop self-assigned this Jun 8, 2020
@carlossanlop carlossanlop changed the title Forward CancellationToken to methods that can take it Apply CA2016: Forward CancellationToken to methods that can take it Jun 8, 2020
Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

LGTM

Can you also change:

<Rule Id="CA2016" Action="Info" /> <!-- Forward the 'CancellationToken' parameter to methods that take one -->

to be Warning instead of Info? That will help to ensure we haven't missed any and don't miss any more moving forward.

@carlossanlop
Copy link
Member Author

The latest bugs in this analyzer have been merged with this PR. I merged it yesterday.
How long does it take for the runtime repo to start consuming the latest bits from roslyn-analyzers? I ask so I can rebase this PR.

@mavasani
Copy link

mavasani commented Jul 6, 2020

@carlossanlop I believe you can just bump up

<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="3.3.0-beta1.20327.1" PrivateAssets="all" />
with latest package from https://dev.azure.com/dnceng/public/_packaging?_a=package&feed=dotnet5&package=Microsoft.CodeAnalysis.NetAnalyzers&protocolType=NuGet&version=3.3.0-beta1.20355.1

@carlossanlop
Copy link
Member Author

Thanks for the sign-off, @jeffhandley. Can I get one more @stephentoub @mavasani @AArnott ?

@carlossanlop carlossanlop merged commit 7f34be5 into dotnet:master Jul 15, 2020
@carlossanlop carlossanlop deleted the CA2016 branch July 15, 2020 20:11
@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.