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

CA1853: Address issues found in PR review #6767 #6791

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@

namespace Microsoft.NetCore.CSharp.Analyzers.Performance
{
/// <summary>
/// CA1853: <inheritdoc cref="NetCore.Analyzers.MicrosoftNetCoreAnalyzersResources.DoNotGuardDictionaryRemoveByContainsKeyTitle"/>
/// CA1868: <inheritdoc cref="NetCore.Analyzers.MicrosoftNetCoreAnalyzersResources.DoNotGuardSetAddOrRemoveByContainsTitle"/>
/// </summary>
[ExportCodeFixProvider(LanguageNames.CSharp), Shared]
public sealed class CSharpDoNotGuardSetAddOrRemoveByContainsFixer : DoNotGuardSetAddOrRemoveByContainsFixer
public sealed class CSharpDoNotGuardCallFixer : DoNotGuardCallFixer
{
protected override bool SyntaxSupportedByFixer(SyntaxNode conditionalSyntax, SyntaxNode childStatementSyntax)
{
Expand All @@ -23,9 +27,9 @@ protected override bool SyntaxSupportedByFixer(SyntaxNode conditionalSyntax, Syn

if (conditionalSyntax is IfStatementSyntax ifStatementSyntax)
{
var addOrRemoveInElse = childStatementSyntax.Parent is ElseClauseSyntax || childStatementSyntax.Parent?.Parent is ElseClauseSyntax;
var guardedCallInElse = childStatementSyntax.Parent is ElseClauseSyntax || childStatementSyntax.Parent?.Parent is ElseClauseSyntax;

return addOrRemoveInElse
return guardedCallInElse
? ifStatementSyntax.Else?.Statement.ChildNodes().Count() == 1
: ifStatementSyntax.Statement.ChildNodes().Count() == 1;
}
Expand All @@ -40,11 +44,11 @@ protected override Document ReplaceConditionWithChild(Document document, SyntaxN
if (conditionalOperationNode is IfStatementSyntax { Else: not null } ifStatementSyntax)
{
var expression = GetNegatedExpression(document, childOperationNode);
var addOrRemoveInElse = childOperationNode.Parent is ElseClauseSyntax || childOperationNode.Parent?.Parent is ElseClauseSyntax;
var guardedCallInElse = childOperationNode.Parent is ElseClauseSyntax || childOperationNode.Parent?.Parent is ElseClauseSyntax;

SyntaxNode newConditionalOperationNode = ifStatementSyntax
.WithCondition((ExpressionSyntax)expression)
.WithStatement(addOrRemoveInElse ? ifStatementSyntax.Statement : ifStatementSyntax.Else.Statement)
.WithStatement(guardedCallInElse ? ifStatementSyntax.Statement : ifStatementSyntax.Else.Statement)
.WithElse(null)
.WithAdditionalAnnotations(Formatter.Annotation).WithTriviaFrom(conditionalOperationNode);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@

namespace Microsoft.NetCore.Analyzers.Performance
{
public abstract class DoNotGuardSetAddOrRemoveByContainsFixer : CodeFixProvider
/// <summary>
/// CA1853: <inheritdoc cref="MicrosoftNetCoreAnalyzersResources.DoNotGuardDictionaryRemoveByContainsKeyTitle"/>
/// CA1868: <inheritdoc cref="MicrosoftNetCoreAnalyzersResources.DoNotGuardSetAddOrRemoveByContainsTitle"/>
/// </summary>
public abstract class DoNotGuardCallFixer : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
ImmutableArray.Create(DoNotGuardSetAddOrRemoveByContains.RuleId);
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(
DoNotGuardCallAnalyzer.DoNotGuardDictionaryRemoveByContainsKeyRuleId,
DoNotGuardCallAnalyzer.DoNotGuardSetAddOrRemoveByContainsRuleId);

public sealed override FixAllProvider GetFixAllProvider()
{
Expand Down Expand Up @@ -44,9 +49,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
return;
}

var codeAction = CodeAction.Create(MicrosoftNetCoreAnalyzersResources.RemoveRedundantGuardCallCodeFixTitle,
var codeAction = CodeAction.Create(
MicrosoftNetCoreAnalyzersResources.RemoveRedundantGuardCallCodeFixTitle,
ct => Task.FromResult(ReplaceConditionWithChild(context.Document, root, conditionalSyntax, childStatementSyntax)),
nameof(MicrosoftNetCoreAnalyzersResources.DoNotGuardSetAddOrRemoveByContainsTitle));
diagnostic.Descriptor.Id);

context.RegisterCodeFix(codeAction, diagnostic);
}
Expand Down