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

Cleanup DoNotUseWhenAllOrWaitAllWithSingleArgument #5060

Merged
merged 2 commits into from
Apr 23, 2021
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,28 @@ public sealed override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction(compilationContext =>
context.RegisterCompilationStartAction(context =>
ryzngard marked this conversation as resolved.
Show resolved Hide resolved
{
if (!compilationContext.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask, out var taskType) ||
!compilationContext.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask1, out var genericTaskType))
if (!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask, out var taskType) ||
!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemThreadingTasksTask1, out var genericTaskType))
{
return;
}

compilationContext.RegisterOperationAction(operationContext =>
context.RegisterOperationAction(context =>
{
var invocation = (IInvocationOperation)operationContext.Operation;
var invocation = (IInvocationOperation)context.Operation;
if (IsWhenOrWaitAllMethod(invocation.TargetMethod, taskType) &&
IsSingleTaskArgument(invocation, taskType, genericTaskType))
{
switch (invocation.TargetMethod.Name)
{
case nameof(Task.WhenAll):
operationContext.ReportDiagnostic(invocation.CreateDiagnostic(WhenAllRule));
context.ReportDiagnostic(invocation.CreateDiagnostic(WhenAllRule));
break;

case nameof(Task.WaitAll):
operationContext.ReportDiagnostic(invocation.CreateDiagnostic(WaitAllRule));
context.ReportDiagnostic(invocation.CreateDiagnostic(WaitAllRule));
break;

default:
Expand Down Expand Up @@ -112,7 +112,8 @@ private static bool IsSingleTaskArgument(IInvocationOperation invocation, INamed
return false;
}

return namedTypeSymbol.Equals(taskType) || namedTypeSymbol.ConstructedFrom.Equals(genericTaskType);
return namedTypeSymbol.Equals(taskType, SymbolEqualityComparer.Default) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought symbol.Equals(otherSymbol) implicitly used the SymbolEqualityComparer.Default if available?

Copy link
Member Author

Choose a reason for hiding this comment

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

@ryzngard It does. But there is an analyzer (RS1024) to enforce using SymbolEqualityComparer explicitly to make the intent clear.

Currently the analyzer is disabled due to few bugs and will be re-enabled once they are fixed.

namedTypeSymbol.ConstructedFrom.Equals(genericTaskType, SymbolEqualityComparer.Default);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
Expand All @@ -12,7 +13,8 @@

namespace Microsoft.NetCore.Analyzers.Tasks
{
public abstract class DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer : CodeFixProvider
[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic), Shared]
Copy link
Contributor

Choose a reason for hiding this comment

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

oops 😅 I really thought I had this change in already. Thanks for fixing!

public sealed class DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(
DoNotUseWhenAllOrWaitAllWithSingleArgument.WaitAllRule.Id,
Expand Down Expand Up @@ -51,7 +53,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

return editor.GetChangedDocument();
},
equivalenceKey: title),
equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.DoNotUseWaitAllWithSingleTaskFix)),
context.Diagnostics);
}
else if (!IsValueStored(operation) && operation.TargetMethod.Name == nameof(Task.WhenAll))
Expand All @@ -69,7 +71,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)

return editor.GetChangedDocument();
},
equivalenceKey: title),
equivalenceKey: nameof(MicrosoftNetCoreAnalyzersResources.DoNotUseWhenAllWithSingleTaskFix)),
context.Diagnostics);
}
}
Expand All @@ -94,7 +96,9 @@ private static bool IsValueStored(IInvocationOperation operation)
return false;
}

protected abstract SyntaxNode GetSingleArgumentSyntax(IInvocationOperation operation);
private static SyntaxNode GetSingleArgumentSyntax(IInvocationOperation operation)
=> ((IArrayCreationOperation)operation.Arguments[0].Value).Initializer.ElementValues[0].Syntax;

public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer;

// Needed for Telemetry (https://github.com/dotnet/roslyn-analyzers/issues/192)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.NetCore.Analyzers.Tasks.DoNotUseWhenAllOrWaitAllWithSingleArgument,
Microsoft.NetCore.CSharp.Analyzers.Tasks.CSharpDoNotUseWhenAllOrWaitAllWithSingleArgumentFixer>;
Microsoft.NetCore.Analyzers.Tasks.DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer>;
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier<
Microsoft.NetCore.Analyzers.Tasks.DoNotUseWhenAllOrWaitAllWithSingleArgument,
Microsoft.NetCore.VisualBasic.Analyzers.Tasks.VisualBasicDoNotUseWhenAllOrWaitAllWithSingleArgumentFixer>;
Microsoft.NetCore.Analyzers.Tasks.DoNotUseWhenAllOrWaitAllWithSingleArgumentFixer>;

namespace Microsoft.NetCore.Analyzers.Tasks.UnitTests
{
Expand Down

This file was deleted.