JavaScript: Fix false positive on Flow type annotations in ExprHasNoEffect

This commit is contained in:
Napalys Klicius
2025-06-03 15:26:22 +02:00
parent 46b5ded862
commit aac56e089a
3 changed files with 5 additions and 3 deletions

View File

@@ -174,7 +174,10 @@ predicate hasNoEffect(Expr e) {
not exists(fe.getName())
) and
// exclude block-level flow type annotations. For example: `(name: empty)`.
not e.(ParExpr).getExpression().getLastToken().getNextToken().getValue() = ":" and
not exists(ParExpr parent |
e.getParent() = parent and
e.getLastToken().getNextToken().getValue() = ":"
) and
// exclude the first statement of a try block
not e = any(TryStmt stmt).getBody().getStmt(0).(ExprStmt).getExpr() and
// exclude expressions that are alone in a file, and file doesn't contain a function.

View File

@@ -11,6 +11,5 @@
| tst.js:49:3:49:49 | new Syn ... o me?") | This expression has no effect. |
| tst.js:50:3:50:36 | new Err ... age(e)) | This expression has no effect. |
| tst.js:61:2:61:20 | o.trivialNonGetter1 | This expression has no effect. |
| tst.js:75:3:75:3 | o | This expression has no effect. |
| tst.js:77:24:77:24 | o | This expression has no effect. |
| uselessfn.js:1:2:1:26 | functio ... d.");\\n} | This expression has no effect. |

View File

@@ -72,7 +72,7 @@ function g() {
Object.defineProperty(o, "nonTrivialGetter2", unknownGetterDef());
o.nonTrivialGetter2;
(o: empty); // $SPURIOUS:Alert
(o: empty);
testSomeCondition() ? o : // $ Alert
doSomethingDangerous();