Reduce _DoubleClampVisitor false positives (#128539)

I'm getting a few false positives in https://github.com/flutter/flutter/pull/128522/checks from the `num.clamp` checker since I introduced a class with a `clamp` method.
This commit is contained in:
LongCatIsLooong 2023-06-08 15:42:35 -07:00 committed by GitHub
parent 0b8fe01738
commit d4733a3fd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -239,7 +239,15 @@ class _DoubleClampVisitor extends RecursiveAstVisitor<CompilationUnit> {
@override
CompilationUnit? visitMethodInvocation(MethodInvocation node) {
if (node.methodName.name == 'clamp') {
final NodeList<Expression> arguments = node.argumentList.arguments;
// This may produce false positives when `node.target` is not a subtype of
// num. The static type of `node.target` isn't guaranteed to be resolved at
// this time. Check whether the argument list consists of 2 positional args
// to reduce false positives.
final bool isNumClampInvocation = node.methodName.name == 'clamp'
&& arguments.length == 2
&& !arguments.any((Expression exp) => exp is NamedExpression);
if (isNumClampInvocation) {
final _Line line = _getLine(parseResult, node.function.offset);
if (!line.content.contains('// ignore_clamp_double_lint')) {
clamps.add(line);