From d4733a3fd4b113ba2427d554601fb9baaf46c992 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Thu, 8 Jun 2023 15:42:35 -0700 Subject: [PATCH] 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. --- dev/bots/analyze.dart | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/dev/bots/analyze.dart b/dev/bots/analyze.dart index 9f9bc8b316..19056c54f3 100644 --- a/dev/bots/analyze.dart +++ b/dev/bots/analyze.dart @@ -239,7 +239,15 @@ class _DoubleClampVisitor extends RecursiveAstVisitor { @override CompilationUnit? visitMethodInvocation(MethodInvocation node) { - if (node.methodName.name == 'clamp') { + final NodeList 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);