|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Composition; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CodeActions; |
| 7 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 8 | +using Microsoft.CodeAnalysis.CSharp; |
| 9 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using Microsoft.CodeAnalysis.Editing; |
| 11 | +using Microsoft.CodeAnalysis.Operations; |
| 12 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 13 | + |
| 14 | +namespace Meziantou.Analyzer.Rules; |
| 15 | + |
| 16 | +[ExportCodeFixProvider(LanguageNames.CSharp), Shared] |
| 17 | +public sealed class UsePatternMatchingForNullCheckFixer : CodeFixProvider |
| 18 | +{ |
| 19 | + public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(RuleIdentifiers.UsePatternMatchingForNullCheck, RuleIdentifiers.UsePatternMatchingForNullEquality); |
| 20 | + |
| 21 | + public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; |
| 22 | + |
| 23 | + public override async Task RegisterCodeFixesAsync(CodeFixContext context) |
| 24 | + { |
| 25 | + var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); |
| 26 | + var nodeToFix = root?.FindNode(context.Span, getInnermostNodeForTie: true); |
| 27 | + if (nodeToFix is not BinaryExpressionSyntax invocation) |
| 28 | + return; |
| 29 | + |
| 30 | + context.RegisterCodeFix( |
| 31 | + CodeAction.Create( |
| 32 | + "Use pattern matching", |
| 33 | + ct => Update(context.Document, invocation, ct), |
| 34 | + equivalenceKey: "Use pattern matching"), |
| 35 | + context.Diagnostics); |
| 36 | + } |
| 37 | + |
| 38 | + private static async Task<Document> Update(Document document, BinaryExpressionSyntax node, CancellationToken cancellationToken) |
| 39 | + { |
| 40 | + var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); |
| 41 | + if (editor.SemanticModel.GetOperation(node, cancellationToken) is not IBinaryOperation operation) |
| 42 | + return document; |
| 43 | + |
| 44 | + var valueSyntax = IsNull(operation.LeftOperand) ? operation.RightOperand.Syntax : operation.LeftOperand.Syntax; |
| 45 | + if (valueSyntax is not ExpressionSyntax expression) |
| 46 | + return document; |
| 47 | + |
| 48 | + PatternSyntax constantExpression = ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)); |
| 49 | + if (operation.OperatorKind is BinaryOperatorKind.NotEquals) |
| 50 | + { |
| 51 | + constantExpression = UnaryPattern(constantExpression); |
| 52 | + } |
| 53 | + |
| 54 | + var newSyntax = IsPatternExpression(expression, constantExpression); |
| 55 | + editor.ReplaceNode(node, newSyntax); |
| 56 | + return editor.GetChangedDocument(); |
| 57 | + } |
| 58 | + |
| 59 | + private static bool IsNull(IOperation operation) |
| 60 | + => operation.UnwrapConversionOperations() is ILiteralOperation { ConstantValue: { HasValue: true, Value: null } }; |
| 61 | +} |
0 commit comments