|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/analysis/features.dart'; |
| 6 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 7 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 8 | +import 'package:analyzer/dart/element/element2.dart'; |
| 9 | +// ignore: implementation_imports |
| 10 | +import 'package:analyzer/src/dart/element/element.dart'; |
| 11 | + |
| 12 | +import '../analyzer.dart'; |
| 13 | +import '../extensions.dart'; |
| 14 | + |
| 15 | +const _desc = |
| 16 | + r'If-elements testing for null can be replaced with null-aware elements.'; |
| 17 | + |
| 18 | +class UseNullAwareElements extends LintRule { |
| 19 | + UseNullAwareElements() |
| 20 | + : super(name: LintNames.use_null_aware_elements, description: _desc); |
| 21 | + |
| 22 | + @override |
| 23 | + LintCode get lintCode => LinterLintCode.use_null_aware_elements; |
| 24 | + |
| 25 | + @override |
| 26 | + void registerNodeProcessors( |
| 27 | + NodeLintRegistry registry, |
| 28 | + LinterContext context, |
| 29 | + ) { |
| 30 | + if (!context.isEnabled(Feature.null_aware_elements)) return; |
| 31 | + var visitor = _Visitor(this); |
| 32 | + registry.addIfElement(this, visitor); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class _Visitor extends SimpleAstVisitor<void> { |
| 37 | + final LintRule rule; |
| 38 | + |
| 39 | + _Visitor(this.rule); |
| 40 | + |
| 41 | + @override |
| 42 | + void visitIfElement(IfElement node) { |
| 43 | + if (node case IfElement(:var thenElement, elseKeyword: null)) { |
| 44 | + Element2? nullCheckTarget; |
| 45 | + if (node.expression case BinaryExpression( |
| 46 | + :var operator, |
| 47 | + :var leftOperand, |
| 48 | + :var rightOperand, |
| 49 | + ) when operator.isOperator && operator.lexeme == '!=') { |
| 50 | + // Case of non-pattern null checks of the form `if (x != null) x`. |
| 51 | + if (leftOperand is NullLiteral) { |
| 52 | + // Cases of the form `if (null != x) x`. |
| 53 | + nullCheckTarget = rightOperand.canonicalElement; |
| 54 | + } else if (rightOperand is NullLiteral) { |
| 55 | + // Cases of the form `if (x != null) x`. |
| 56 | + nullCheckTarget = leftOperand.canonicalElement; |
| 57 | + } |
| 58 | + } else if (node.caseClause?.guardedPattern.pattern case NullCheckPattern( |
| 59 | + pattern: DeclaredVariablePattern(:var declaredElement2), |
| 60 | + )) { |
| 61 | + // Case of pattern null checks of the form `if (x case var y?) y`. |
| 62 | + nullCheckTarget = declaredElement2; |
| 63 | + } |
| 64 | + |
| 65 | + if (nullCheckTarget is PromotableElementImpl2) { |
| 66 | + if (thenElement is SimpleIdentifier && |
| 67 | + nullCheckTarget == thenElement.canonicalElement) { |
| 68 | + // List and set elements, such as the following: |
| 69 | + // |
| 70 | + // [if (x != null) x] |
| 71 | + // {if (x != null) x} |
| 72 | + rule.reportLintForToken(node.ifKeyword); |
| 73 | + } else if (thenElement case MapLiteralEntry(:var key, :var value)) { |
| 74 | + if (key is SimpleIdentifier && |
| 75 | + nullCheckTarget == key.canonicalElement) { |
| 76 | + // Map keys, such as the following: |
| 77 | + // |
| 78 | + // {if (x != null) x: value} |
| 79 | + rule.reportLintForToken(node.ifKeyword); |
| 80 | + } else if (value is SimpleIdentifier && |
| 81 | + nullCheckTarget == value.canonicalElement) { |
| 82 | + // Map keys, such as the following: |
| 83 | + // |
| 84 | + // {if (x != null) key: x} |
| 85 | + rule.reportLintForToken(node.ifKeyword); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments