Rule S8949 is about using cancellation tokens provided by method arguments when possible. If we take the following snippet:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Threading;
using System.Threading.Tasks;
public static class S8949
{
/// <summary>Gets the diagnostics for the specified analyzers.</summary>
[Pure]
public static async Task<IReadOnlyCollection<Diagnostic>> GetDiagnosticsAsync(
Compilation compilation,
ImmutableArray<DiagnosticAnalyzer> analyzers,
IEnumerable<AdditionalText> texts,
CancellationToken cancellationToken = default)
{
var options = compilation.Options.WithSpecificDiagnosticOptions([]);
var analyzerOptions = new AnalyzerOptions([.. texts]);
var diagnostics = await compilation // FP, cancelation token is passed.
.WithOptions(options)
.WithAnalyzers(analyzers, analyzerOptions)
.GetAllDiagnosticsAsync(cancellationToken);
return diagnostics;
}
}
It reports:
Pass the âcancellationTokenâ to this method to allow cancellation of the operation, or use âCancellationToken.Noneâ to opt out explicitly.
it fails to detect that cancellationToken is actually provided, hence the FP. It is worth noticing that neither.WithOptions() and .WithAnalyzers() have an overload that has a CancellationToken argument, and that all 3 methods in the chain are extension methods. Iâm not sure if the last part is relevant, but it might be.
@lijun.chen Could you confirm this as an issue? I understand youâre busy, and that you can not give an ETA. It helps me to know that it could be confirmed and is somewhere on a backlog.
Apologies for the delay, and thanks for reporting this problem. I agree this is a false positive.
Actually, WithAnalyzers does have an overload that accepts a CancellationToken, but Roslyn marks it obsolete and recommends using the overload without a token.
That being said, I see two improvements we could make to this rule:
It should exclude obsolete overloads when looking for alternatives that accept a CancellationToken. This would prevent this false positive.
It should point the issue more precisely at the offending method it is reporting. Here, the issue is raised for WithAnalyzers, but the issue location currently covers compilation.WithOptions(options).WithAnalyzers, making it confusing.
Hope this helps.
I have created an internal ticket for these improvements.
Sorry for that. @lijun.chen Responded on my other FP, and my impulse to think, that they just mist this one was too big. Iâll try to behave better next time.
I missed that. Reason is that Visual Studio doesnât even show the obsolete option. Not in the tooltip, and not in the auto complete option. An extra reason not to advise it.