Skip to content

JS: Improve useless-expression query to avoid duplicate alerts on compound expressions #19579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions javascript/ql/lib/Expressions/ExprHasNoEffect.qll
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ predicate hasNoEffect(Expr e) {
e.getParent() = parent and
e.getLastToken().getNextToken().getValue() = ":"
) and
// exclude expressions that are part of a conditional expression
not exists(ConditionalExpr cond | e.getParent() = cond |
e instanceof NullLiteral or
e instanceof GlobalVarAccess or
e.(NumberLiteral).getIntValue() = 0 or
e.(UnaryExpr).getOperator() = "void"
) 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@
| 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:77:24:77:24 | o | This expression has no effect. |
| tst.js:83:43:83:46 | null | This expression has no effect. |
| tst.js:84:42:84:45 | null | This expression has no effect. |
| uselessfn.js:1:2:1:26 | functio ... d.");\\n} | This expression has no effect. |
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ function g() {
consume(testSomeCondition() ? o :
doSomethingDangerous());

("release" === isRelease() ? warning() : null); // $ Alert
"release" === isRelease() ? warning() : null; // $ Alert
("release" === isRelease() ? warning() : null);
"release" === isRelease() ? warning() : null;
"release" === isRelease() ? warning() : 0;
"release" === isRelease() ? warning() : undefined;
};