Skip to content

Commit 1066c7c

Browse files
authored
CA1853: Address issues found in PR review #6767 (#6791)
* Rename do not guard set analyzer and fixer This is done in a separate commit, so that the actual changes can be seen in the next commit. This is because git would not recognize the rename if the changes were added as is. Note that this commit by itself is broken. * Combine analyzers for guarded calls This combines the analyzers CA1853 and CA1868 to avoid code duplication, make CA1853 support the same range of cases as CA1868 and fix #6781.
1 parent 9dba21f commit 1066c7c

File tree

12 files changed

+2647
-1197
lines changed

12 files changed

+2647
-1197
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
namespace Microsoft.NetCore.CSharp.Analyzers.Performance
1313
{
14+
/// <summary>
15+
/// CA1853: <inheritdoc cref="NetCore.Analyzers.MicrosoftNetCoreAnalyzersResources.DoNotGuardDictionaryRemoveByContainsKeyTitle"/>
16+
/// CA1868: <inheritdoc cref="NetCore.Analyzers.MicrosoftNetCoreAnalyzersResources.DoNotGuardSetAddOrRemoveByContainsTitle"/>
17+
/// </summary>
1418
[ExportCodeFixProvider(LanguageNames.CSharp), Shared]
15-
public sealed class CSharpDoNotGuardSetAddOrRemoveByContainsFixer : DoNotGuardSetAddOrRemoveByContainsFixer
19+
public sealed class CSharpDoNotGuardCallFixer : DoNotGuardCallFixer
1620
{
1721
protected override bool SyntaxSupportedByFixer(SyntaxNode conditionalSyntax, SyntaxNode childStatementSyntax)
1822
{
@@ -23,9 +27,9 @@ protected override bool SyntaxSupportedByFixer(SyntaxNode conditionalSyntax, Syn
2327

2428
if (conditionalSyntax is IfStatementSyntax ifStatementSyntax)
2529
{
26-
var addOrRemoveInElse = childStatementSyntax.Parent is ElseClauseSyntax || childStatementSyntax.Parent?.Parent is ElseClauseSyntax;
30+
var guardedCallInElse = childStatementSyntax.Parent is ElseClauseSyntax || childStatementSyntax.Parent?.Parent is ElseClauseSyntax;
2731

28-
return addOrRemoveInElse
32+
return guardedCallInElse
2933
? ifStatementSyntax.Else?.Statement.ChildNodes().Count() == 1
3034
: ifStatementSyntax.Statement.ChildNodes().Count() == 1;
3135
}
@@ -40,11 +44,11 @@ protected override Document ReplaceConditionWithChild(Document document, SyntaxN
4044
if (conditionalOperationNode is IfStatementSyntax { Else: not null } ifStatementSyntax)
4145
{
4246
var expression = GetNegatedExpression(document, childOperationNode);
43-
var addOrRemoveInElse = childOperationNode.Parent is ElseClauseSyntax || childOperationNode.Parent?.Parent is ElseClauseSyntax;
47+
var guardedCallInElse = childOperationNode.Parent is ElseClauseSyntax || childOperationNode.Parent?.Parent is ElseClauseSyntax;
4448

4549
SyntaxNode newConditionalOperationNode = ifStatementSyntax
4650
.WithCondition((ExpressionSyntax)expression)
47-
.WithStatement(addOrRemoveInElse ? ifStatementSyntax.Statement : ifStatementSyntax.Else.Statement)
51+
.WithStatement(guardedCallInElse ? ifStatementSyntax.Statement : ifStatementSyntax.Else.Statement)
4852
.WithElse(null)
4953
.WithAdditionalAnnotations(Formatter.Annotation).WithTriviaFrom(conditionalOperationNode);
5054

src/NetAnalyzers/CSharp/Microsoft.NetCore.Analyzers/Performance/CSharpDoNotGuardDictionaryRemoveByContainsKeyFixer.cs

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@
99

1010
namespace Microsoft.NetCore.Analyzers.Performance
1111
{
12-
public abstract class DoNotGuardSetAddOrRemoveByContainsFixer : CodeFixProvider
12+
/// <summary>
13+
/// CA1853: <inheritdoc cref="MicrosoftNetCoreAnalyzersResources.DoNotGuardDictionaryRemoveByContainsKeyTitle"/>
14+
/// CA1868: <inheritdoc cref="MicrosoftNetCoreAnalyzersResources.DoNotGuardSetAddOrRemoveByContainsTitle"/>
15+
/// </summary>
16+
public abstract class DoNotGuardCallFixer : CodeFixProvider
1317
{
14-
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
15-
ImmutableArray.Create(DoNotGuardSetAddOrRemoveByContains.RuleId);
18+
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(
19+
DoNotGuardCallAnalyzer.DoNotGuardDictionaryRemoveByContainsKeyRuleId,
20+
DoNotGuardCallAnalyzer.DoNotGuardSetAddOrRemoveByContainsRuleId);
1621

1722
public sealed override FixAllProvider GetFixAllProvider()
1823
{
@@ -44,9 +49,10 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
4449
return;
4550
}
4651

47-
var codeAction = CodeAction.Create(MicrosoftNetCoreAnalyzersResources.RemoveRedundantGuardCallCodeFixTitle,
52+
var codeAction = CodeAction.Create(
53+
MicrosoftNetCoreAnalyzersResources.RemoveRedundantGuardCallCodeFixTitle,
4854
ct => Task.FromResult(ReplaceConditionWithChild(context.Document, root, conditionalSyntax, childStatementSyntax)),
49-
nameof(MicrosoftNetCoreAnalyzersResources.DoNotGuardSetAddOrRemoveByContainsTitle));
55+
diagnostic.Descriptor.Id);
5056

5157
context.RegisterCodeFix(codeAction, diagnostic);
5258
}

0 commit comments

Comments
 (0)