-
Notifications
You must be signed in to change notification settings - Fork 480
CA1068: Allow caller attributes to come after cancellation token #4228
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
|
@Youssef1313 Have you been able to reproduce the issue? I couldn't... |
|
@Evangelink I haven't tried, this PR was just a quick edit from GitHub UI 😄 |
|
@Evangelink I read through the code again and I think I got it. The cancellation token should be optional to reproduce. |
|
@Youssef1313 I don't think so, some tests are raising an issue without having the parameter with a default value: [Fact]
public async Task DiagnosticOnExtensionMethodWhenCancellationTokenIsNotFirstParameter()
{
var test = @"
using System.Threading;
static class C1
{
public static void M1(this object p1, CancellationToken p2, object p3)
{
}
}";
var expected = VerifyCS.Diagnostic().WithLocation(5, 24).WithArguments("C1.M1(object, System.Threading.CancellationToken, object)");
await VerifyCS.VerifyAnalyzerAsync(test, expected);
} |
|
@Evangelink If optional parameter exists, the analyzer behavior is different based on whether the cancellation token is optional or not.
So: public Task SomeAsync(CancellationToken cancellationToken,
[CallerMemberName] string memberName = """",
[CallerFilePath] string sourceFilePath = """",
[CallerLineNumber] int sourceLineNumber = 0)
{
throw new NotImplementedException();
} The above currently doesn't report diagnostic, which is correct. public Task SomeAsync(CancellationToken cancellationToken = default,
[CallerMemberName] string memberName = """",
[CallerFilePath] string sourceFilePath = """",
[CallerLineNumber] int sourceLineNumber = 0)
{
throw new NotImplementedException();
} But the above code will report diagnostic, this is the case reported in #4227 I haven't confirmed this, but both tests are added in this draft #4229, so waiting for CI to confirm. Edit: @Evangelink The CI build in #4229 has completed and confirms that this analyzer reports a diagnostic when cancellation token is optional and is followed by caller information parameters. I'll close the draft PR once you give the build results a look. |
|
@mavasani This is preferred to be squashed when merging. |
...crosoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs
Outdated
Show resolved
Hide resolved
...crosoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs
Outdated
Show resolved
Hide resolved
...crosoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs
Outdated
Show resolved
Hide resolved
...ft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLastTests.cs
Outdated
Show resolved
Hide resolved
...ft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLastTests.cs
Show resolved
Hide resolved
...ft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLastTests.cs
Show resolved
Hide resolved
Co-authored-by: Amaury Levé <[email protected]>
Co-authored-by: Amaury Levé <[email protected]>
...ft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLastTests.cs
Show resolved
Hide resolved
...ft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLastTests.cs
Show resolved
Hide resolved
Codecov Report
@@ Coverage Diff @@
## master #4228 +/- ##
========================================
Coverage 95.80% 95.80%
========================================
Files 1170 1170
Lines 264435 264599 +164
Branches 15959 15966 +7
========================================
+ Hits 253333 253504 +171
+ Misses 9087 9081 -6
+ Partials 2015 2014 -1 |
...crosoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs
Outdated
Show resolved
Hide resolved
...crosoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs
Outdated
Show resolved
Hide resolved
Evangelink
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
...ft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLastTests.cs
Outdated
Show resolved
Hide resolved
...ft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLastTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Amaury Levé <[email protected]>
...crosoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs
Outdated
Show resolved
Hide resolved
…nGuidelines/CancellationTokenParametersMustComeLast.cs
...crosoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs
Outdated
Show resolved
Hide resolved
mavasani
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.
Change looks good, but needs moving well known type computation logic into CompilationStartAction.
…net#4228) * CA1068: Allow caller attributes to come after cancellation token * Add caller attributes tests for CA1068 * Ignore caller attribute only if optional * Move Parameter.IsOptional check to HasCallerInformationAttribute * Fix typo * typo * Use GetOrCreateTypeByMetadataName Co-authored-by: Amaury Levé <[email protected]> * Use AddIfNotNull * Apply suggestions from code review Co-authored-by: Amaury Levé <[email protected]> * Update CancellationTokenParametersMustComeLastTests.cs * Update CancellationTokenParametersMustComeLast.cs * Fix * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Amaury Levé <[email protected]> * Update src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/CancellationTokenParametersMustComeLast.cs * Update WellKnownTypeNames.cs * Use WellKnownTypeNames and move to compilation start Co-authored-by: Amaury Levé <[email protected]>
Fixes #4227