forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveRedundantEqualityCodeFixProvider.cs
72 lines (59 loc) · 3.26 KB
/
RemoveRedundantEqualityCodeFixProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.LanguageService;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.RemoveRedundantEquality;
[ExportCodeFixProvider(LanguageNames.CSharp, LanguageNames.VisualBasic, Name = PredefinedCodeFixProviderNames.RemoveRedundantEquality), Shared]
[method: ImportingConstructor]
[method: SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")]
internal sealed class RemoveRedundantEqualityCodeFixProvider() : ForkingSyntaxEditorBasedCodeFixProvider<SyntaxNode>
{
public override ImmutableArray<string> FixableDiagnosticIds => [IDEDiagnosticIds.RemoveRedundantEqualityDiagnosticId];
protected override (string title, string equivalenceKey) GetTitleAndEquivalenceKey(CodeFixContext context)
=> (AnalyzersResources.Remove_redundant_equality, nameof(AnalyzersResources.Remove_redundant_equality));
protected override async Task FixAsync(
Document document,
SyntaxEditor editor,
CodeActionOptionsProvider fallbackOptions,
SyntaxNode node,
ImmutableDictionary<string, string?> properties,
CancellationToken cancellationToken)
{
var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var generator = editor.Generator;
var generatorInternal = document.GetRequiredLanguageService<SyntaxGeneratorInternal>();
var syntaxFacts = document.GetRequiredLanguageService<ISyntaxFactsService>();
var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);
editor.ReplaceNode(node, WithElasticTrailingTrivia(RewriteNode()));
return;
SyntaxNode RewriteNode()
{
// This should happen only in error cases.
if (!syntaxFacts.IsBinaryExpression(node))
return node;
syntaxFacts.GetPartsOfBinaryExpression(node, out var left, out var right);
var rewritten =
properties[RedundantEqualityConstants.RedundantSide] == RedundantEqualityConstants.Right ? left :
properties[RedundantEqualityConstants.RedundantSide] == RedundantEqualityConstants.Left ? right : node;
if (properties.ContainsKey(RedundantEqualityConstants.Negate))
rewritten = generator.Negate(generatorInternal, rewritten, semanticModel, cancellationToken);
return rewritten;
}
static SyntaxNode WithElasticTrailingTrivia(SyntaxNode node)
{
return node.WithTrailingTrivia(node.GetTrailingTrivia().Select(SyntaxTriviaExtensions.AsElastic));
}
}
}