From 856932a1b921764959af35e5f93a4d13909f79f8 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Thu, 23 Aug 2018 10:50:25 -0700 Subject: [PATCH] Fix conflict between fixers that was preventing one from showing up. --- ...rCastAndEqualityOperatorCodeFixProvider.cs | 16 +++++++++++----- ...IsNullForReferenceEqualsCodeFixProvider.cs | 19 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorCodeFixProvider.cs b/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorCodeFixProvider.cs index 8d11e4c7aee45..b85e225992f16 100644 --- a/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorCodeFixProvider.cs +++ b/src/Features/CSharp/Portable/UseIsNullCheck/CSharpUseIsNullCheckForCastAndEqualityOperatorCodeFixProvider.cs @@ -25,14 +25,20 @@ internal class CSharpUseIsNullCheckForCastAndEqualityOperatorCodeFixProvider : S public override ImmutableArray FixableDiagnosticIds => ImmutableArray.Create(IDEDiagnosticIds.UseIsNullCheckDiagnosticId); + private static bool IsSupportedDiagnostic(Diagnostic diagnostic) + => diagnostic.Properties[UseIsNullConstants.Kind] == UseIsNullConstants.CastAndEqualityKey; + public override Task RegisterCodeFixesAsync(CodeFixContext context) { var diagnostic = context.Diagnostics.First(); + if (IsSupportedDiagnostic(diagnostic)) + { + context.RegisterCodeFix( + new MyCodeAction(CSharpFeaturesResources.Use_is_null_check, + c => this.FixAsync(context.Document, diagnostic, c)), + context.Diagnostics); + } - context.RegisterCodeFix( - new MyCodeAction(CSharpFeaturesResources.Use_is_null_check, - c => this.FixAsync(context.Document, diagnostic, c)), - context.Diagnostics); return Task.CompletedTask; } @@ -42,7 +48,7 @@ protected override Task FixAllAsync( { foreach (var diagnostic in diagnostics) { - if (diagnostic.Properties[UseIsNullConstants.Kind] != UseIsNullConstants.CastAndEqualityKey) + if (!IsSupportedDiagnostic(diagnostic)) { continue; } diff --git a/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullForReferenceEqualsCodeFixProvider.cs b/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullForReferenceEqualsCodeFixProvider.cs index 6bb6868557322..1b009ebb122e3 100644 --- a/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullForReferenceEqualsCodeFixProvider.cs +++ b/src/Features/Core/Portable/UseIsNullCheck/AbstractUseIsNullForReferenceEqualsCodeFixProvider.cs @@ -28,15 +28,22 @@ public override ImmutableArray FixableDiagnosticIds protected abstract SyntaxNode CreateNullCheck(SyntaxNode argument, bool isUnconstrainedGeneric); protected abstract SyntaxNode CreateNotNullCheck(SyntaxNode notExpression, SyntaxNode argument, bool isUnconstrainedGeneric); + private static bool IsSupportedDiagnostic(Diagnostic diagnostic) + => diagnostic.Properties[UseIsNullConstants.Kind] == UseIsNullConstants.ReferenceEqualsKey; + public override Task RegisterCodeFixesAsync(CodeFixContext context) { var diagnostic = context.Diagnostics.First(); - var negated = diagnostic.Properties.ContainsKey(Negated); - var title = negated ? GetIsNotNullTitle() : GetIsNullTitle(); + if (IsSupportedDiagnostic(diagnostic)) + { + var negated = diagnostic.Properties.ContainsKey(Negated); + var title = negated ? GetIsNotNullTitle() : GetIsNullTitle(); + + context.RegisterCodeFix( + new MyCodeAction(title, c => this.FixAsync(context.Document, diagnostic, c)), + context.Diagnostics); + } - context.RegisterCodeFix( - new MyCodeAction(title, c => this.FixAsync(context.Document, diagnostic, c)), - context.Diagnostics); return Task.CompletedTask; } @@ -51,7 +58,7 @@ protected override Task FixAllAsync( // not there once their parent has been replaced. foreach (var diagnostic in diagnostics.OrderByDescending(d => d.Location.SourceSpan.Start)) { - if (diagnostic.Properties[UseIsNullConstants.Kind] != UseIsNullConstants.ReferenceEqualsKey) + if (!IsSupportedDiagnostic(diagnostic)) { continue; }