|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/analysis_rule/analysis_rule.dart'; |
| 6 | +import 'package:analyzer/analysis_rule/rule_context.dart'; |
| 7 | +import 'package:analyzer/analysis_rule/rule_visitor_registry.dart'; |
| 8 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 9 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 10 | +import 'package:analyzer/dart/element/element.dart'; |
| 11 | +import 'package:analyzer/dart/element/type.dart'; |
| 12 | +import 'package:analyzer/error/error.dart'; |
| 13 | + |
| 14 | +/// Verify that we use clampDouble instead of double.clamp for performance |
| 15 | +/// reasons. |
| 16 | +/// |
| 17 | +/// See also: |
| 18 | +/// * https://github.com/flutter/flutter/pull/103559 |
| 19 | +/// * https://github.com/flutter/flutter/issues/103917 |
| 20 | +class NoDoubleClamp extends AnalysisRule { |
| 21 | + NoDoubleClamp() |
| 22 | + : super( |
| 23 | + name: code.name, |
| 24 | + description: |
| 25 | + 'Verify that we use clampDouble instead of double.clamp for performance reasons.', |
| 26 | + ); |
| 27 | + |
| 28 | + static const LintCode code = LintCode( |
| 29 | + 'no_double_clamp', |
| 30 | + 'Avoid double.clamp for performance reasons.', |
| 31 | + correctionMessage: 'Use clampDouble instead.', |
| 32 | + severity: DiagnosticSeverity.ERROR, |
| 33 | + ); |
| 34 | + |
| 35 | + @override |
| 36 | + DiagnosticCode get diagnosticCode => code; |
| 37 | + |
| 38 | + @override |
| 39 | + void registerNodeProcessors(RuleVisitorRegistry registry, RuleContext context) { |
| 40 | + final visitor = _Visitor(this, context); |
| 41 | + registry.addSimpleIdentifier(this, visitor); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class _Visitor extends SimpleAstVisitor<void> { |
| 46 | + _Visitor(this.rule, this.context); |
| 47 | + |
| 48 | + final AnalysisRule rule; |
| 49 | + final RuleContext context; |
| 50 | + |
| 51 | + @override |
| 52 | + void visitSimpleIdentifier(SimpleIdentifier node) { |
| 53 | + if (node.name != 'clamp' || node.element is! MethodElement) { |
| 54 | + return; |
| 55 | + } |
| 56 | + final bool isAllowed = switch (node.parent) { |
| 57 | + // PropertyAccess and PrefixedIdentifier match num.clamp in tear-off form. |
| 58 | + // Always prefer doubleClamp over tear-offs: even when all 3 operands are |
| 59 | + // int literals, the return type doesn't get promoted to int: |
| 60 | + // final x = 1.clamp(0, 2); // The inferred return type is int, where as: |
| 61 | + // final f = 1.clamp; |
| 62 | + // final y = f(0, 2) // The inferred return type is num. |
| 63 | + PropertyAccess( |
| 64 | + target: Expression( |
| 65 | + staticType: DartType(isDartCoreDouble: true) || |
| 66 | + DartType(isDartCoreNum: true) || |
| 67 | + DartType(isDartCoreInt: true), |
| 68 | + ), |
| 69 | + ) || |
| 70 | + PrefixedIdentifier( |
| 71 | + prefix: Expression( |
| 72 | + staticType: DartType(isDartCoreDouble: true) || |
| 73 | + DartType(isDartCoreNum: true) || |
| 74 | + DartType(isDartCoreInt: true), |
| 75 | + ), |
| 76 | + ) => false, |
| 77 | + |
| 78 | + // Expressions like `final int x = 1.clamp(0, 2);` should be allowed. |
| 79 | + MethodInvocation( |
| 80 | + target: Expression(staticType: DartType(isDartCoreInt: true)), |
| 81 | + argumentList: ArgumentList( |
| 82 | + arguments: [ |
| 83 | + Expression(staticType: DartType(isDartCoreInt: true)), |
| 84 | + Expression(staticType: DartType(isDartCoreInt: true)), |
| 85 | + ], |
| 86 | + ), |
| 87 | + ) => |
| 88 | + true, |
| 89 | + |
| 90 | + // Otherwise, disallow num.clamp() invocations. |
| 91 | + MethodInvocation( |
| 92 | + target: Expression( |
| 93 | + staticType: DartType(isDartCoreDouble: true) || |
| 94 | + DartType(isDartCoreNum: true) || |
| 95 | + DartType(isDartCoreInt: true), |
| 96 | + ), |
| 97 | + ) => |
| 98 | + false, |
| 99 | + |
| 100 | + _ => true, |
| 101 | + }; |
| 102 | + if (!isAllowed) { |
| 103 | + rule.reportAtNode(node); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments