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

Ignore obsolete methods for CA2016 #6822

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ public override void Initialize(AnalysisContext context)

private void AnalyzeCompilationStart(CompilationStartAnalysisContext context)
{
if (!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingCancellationToken, out INamedTypeSymbol? cancellationTokenType))
var typeProvider = WellKnownTypeProvider.GetOrCreate(context.Compilation);
if (!typeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingCancellationToken, out INamedTypeSymbol? cancellationTokenType)
|| !typeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemObsoleteAttribute, out INamedTypeSymbol? obsoleteAttribute))
{
return;
}

// We don't care if these symbols are not defined in our compilation. They are used to special case the Task<T> <-> ValueTask<T> logic
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask1, out INamedTypeSymbol? genericTask);
context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksValueTask1, out INamedTypeSymbol? genericValueTask);
typeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask1, out INamedTypeSymbol? genericTask);
typeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksValueTask1, out INamedTypeSymbol? genericValueTask);

context.RegisterOperationAction(context =>
{
Expand All @@ -93,6 +95,7 @@ private void AnalyzeCompilationStart(CompilationStartAnalysisContext context)
cancellationTokenType,
genericTask,
genericValueTask,
obsoleteAttribute,
out int shouldFix,
out string? cancellationTokenArgumentName,
out string? invocationTokenParameterName))
Expand Down Expand Up @@ -125,6 +128,7 @@ private bool ShouldDiagnose(
INamedTypeSymbol cancellationTokenType,
INamedTypeSymbol? genericTask,
INamedTypeSymbol? genericValueTask,
INamedTypeSymbol obsoleteAttribute,
out int shouldFix, [NotNullWhen(returnValue: true)] out string? ancestorTokenParameterName, out string? invocationTokenParameterName)
{
shouldFix = 1;
Expand Down Expand Up @@ -152,7 +156,7 @@ private bool ShouldDiagnose(
}
}
// or an overload that takes a ct at the end
else if (MethodHasCancellationTokenOverload(compilation, method, cancellationTokenType, genericTask, genericValueTask, out overload))
else if (MethodHasCancellationTokenOverload(compilation, method, cancellationTokenType, genericTask, genericValueTask, obsoleteAttribute, out overload))
{
if (ArgumentsImplicitOrNamed(cancellationTokenType, invocation.Arguments))
{
Expand Down Expand Up @@ -334,13 +338,14 @@ private static bool MethodHasCancellationTokenOverload(
ITypeSymbol cancellationTokenType,
INamedTypeSymbol? genericTask,
INamedTypeSymbol? genericValueTask,
INamedTypeSymbol obsoleteAttribute,
[NotNullWhen(returnValue: true)] out IMethodSymbol? overload)
{
overload = method.ContainingType
.GetMembers(method.Name)
.OfType<IMethodSymbol>()
.FirstOrDefault(methodToCompare =>
HasSameParametersPlusCancellationToken(compilation, cancellationTokenType, genericTask, genericValueTask, method, methodToCompare));
.GetMembers(method.Name)
.OfType<IMethodSymbol>()
.FirstOrDefault(methodToCompare => !methodToCompare.HasAnyAttribute(obsoleteAttribute)
&& HasSameParametersPlusCancellationToken(compilation, cancellationTokenType, genericTask, genericValueTask, method, methodToCompare));

return overload != null;

Expand Down
1 change: 0 additions & 1 deletion src/NetAnalyzers/RulesMissingDocumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ CA1863 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-
CA1865 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1865> | Use char overload |
CA1866 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1866> | Use char overload |
CA1867 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1867> | Use char overload |
CA1868 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1868> | Unnecessary call to 'Contains(item)' |
CA2021 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2021> | Do not call Enumerable.Cast\<T> or Enumerable.OfType\<T> with incompatible types |
CA2261 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2261> | Do not use ConfigureAwaitOptions.SuppressThrowing with Task\<TResult> |
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,28 @@ static void M1(string s, CancellationToken cancellationToken, __arglist)
await VerifyCS.VerifyCodeFixAsync(source, source);
}

[Fact]
[WorkItem(6819, "https://github.com/dotnet/roslyn-analyzers/issues/6819")]
public Task ObsoleteOverload()
{
return VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Threading;

class Test
{
public void Main(CancellationToken token)
{
Run();
}

public void Run() {}

[Obsolete]
public void Run(CancellationToken token) {}
}");
}

#endregion

#region Diagnostics with no fix = C#
Expand Down