|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Composition; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CodeActions; |
| 5 | +using Microsoft.CodeAnalysis.CodeFixes; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | +using Microsoft.CodeAnalysis.Editing; |
| 9 | +using Microsoft.CodeAnalysis.Operations; |
| 10 | +using Microsoft.CodeAnalysis.Simplification; |
| 11 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 12 | + |
| 13 | +namespace Meziantou.Analyzer.Rules; |
| 14 | + |
| 15 | +[ExportCodeFixProvider(LanguageNames.CSharp), Shared] |
| 16 | +public sealed class UsePatternMatchingInsteadOfHasvalueFixer : CodeFixProvider |
| 17 | +{ |
| 18 | + public override ImmutableArray<string> FixableDiagnosticIds => |
| 19 | + ImmutableArray.Create(RuleIdentifiers.UsePatternMatchingInsteadOfHasvalue); |
| 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 MemberAccessExpressionSyntax memberAccess) |
| 28 | + return; |
| 29 | + |
| 30 | + context.RegisterCodeFix( |
| 31 | + CodeAction.Create( |
| 32 | + "Use pattern matching", |
| 33 | + ct => Update(context.Document, memberAccess, ct), |
| 34 | + equivalenceKey: "Use pattern matching"), |
| 35 | + context.Diagnostics); |
| 36 | + } |
| 37 | + |
| 38 | + private static async Task<Document> Update(Document document, MemberAccessExpressionSyntax node, CancellationToken cancellationToken) |
| 39 | + { |
| 40 | + var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); |
| 41 | + if (editor.SemanticModel.GetOperation(node, cancellationToken) is not IPropertyReferenceOperation operation) |
| 42 | + return document; |
| 43 | + |
| 44 | + var (nodeToReplace, negate) = GetNodeToReplace(operation); |
| 45 | + var target = operation.Instance?.Syntax as ExpressionSyntax; |
| 46 | + var newNode = MakeIsNotNull(target ?? node, negate); |
| 47 | + editor.ReplaceNode(nodeToReplace, newNode); |
| 48 | + return editor.GetChangedDocument(); |
| 49 | + } |
| 50 | + |
| 51 | + private static (SyntaxNode Node, bool Negate) GetNodeToReplace(IOperation operation) |
| 52 | + { |
| 53 | + if (operation.Parent is IUnaryOperation unaryOperation && unaryOperation.OperatorKind == UnaryOperatorKind.Not) |
| 54 | + return (operation.Parent.Syntax, true); |
| 55 | + |
| 56 | + if (operation.Parent is IBinaryOperation binaryOperation && |
| 57 | + (binaryOperation.OperatorKind is BinaryOperatorKind.Equals or BinaryOperatorKind.NotEquals)) |
| 58 | + { |
| 59 | + if (binaryOperation.RightOperand.ConstantValue is { HasValue: true, Value: bool rightValue }) |
| 60 | + { |
| 61 | + var negate = (!rightValue && binaryOperation.OperatorKind is BinaryOperatorKind.Equals) || |
| 62 | + (rightValue && binaryOperation.OperatorKind is BinaryOperatorKind.NotEquals); |
| 63 | + return (operation.Parent.Syntax, negate); |
| 64 | + } |
| 65 | + |
| 66 | + if (binaryOperation.LeftOperand.ConstantValue is { HasValue: true, Value: bool leftValue }) |
| 67 | + { |
| 68 | + var negate = (!leftValue && binaryOperation.OperatorKind is BinaryOperatorKind.Equals) || |
| 69 | + (leftValue && binaryOperation.OperatorKind is BinaryOperatorKind.NotEquals); |
| 70 | + return (operation.Parent.Syntax, negate); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (operation.Parent is IIsPatternOperation { Pattern: IConstantPatternOperation { Value: ILiteralOperation { ConstantValue: { Value: bool value } } } }) |
| 75 | + { |
| 76 | + if (value) |
| 77 | + { |
| 78 | + return (operation.Parent.Syntax, false); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + return (operation.Parent.Syntax, true); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return (operation.Syntax, false); |
| 87 | + } |
| 88 | + |
| 89 | + private static IsPatternExpressionSyntax MakeIsNotNull(ExpressionSyntax instance, bool negate) |
| 90 | + { |
| 91 | + PatternSyntax constantExpression = ConstantPattern(LiteralExpression(SyntaxKind.NullLiteralExpression)); |
| 92 | + if (!negate) |
| 93 | + { |
| 94 | + constantExpression = UnaryPattern(constantExpression); |
| 95 | + } |
| 96 | + |
| 97 | + return IsPatternExpression(ParenthesizedExpression(instance).WithAdditionalAnnotations(Simplifier.Annotation), constantExpression); |
| 98 | + } |
| 99 | +} |
0 commit comments