Skip to content

Commit 1887938

Browse files
authored
Merge pull request #2627 from asger-semmle/js-useless-expression-trycatch
Approved by esbena
2 parents 48301e1 + 7141f15 commit 1887938

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

change-notes/1.24/analysis-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
| Duplicate parameter names (`js/duplicate-parameter-name`) | Fewer results | This query now recognizes additional parameters that reasonably can have duplicated names. |
3131
| Incomplete string escaping or encoding (`js/incomplete-sanitization`) | Fewer false positive results | This query now recognizes additional cases where a single replacement is likely to be intentional. |
3232
| Unbound event handler receiver (`js/unbound-event-handler-receiver`) | Fewer false positive results | This query now recognizes additional ways event handler receivers can be bound. |
33-
| Expression has no effect (`js/useless-expression`) | Fewer false positive results | The query now recognizes block-level flow type annotations. |
33+
| Expression has no effect (`js/useless-expression`) | Fewer false positive results | The query now recognizes block-level flow type annotations and ignores the first statement of a try block. |
3434
| Use of call stack introspection in strict mode (`js/strict-mode-call-stack-introspection`) | Fewer false positive results | The query no longer flags expression statements. |
3535

3636
## Changes to libraries

javascript/ql/src/Expressions/ExprHasNoEffect.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,7 @@ predicate hasNoEffect(Expr e) {
156156
not exists(fe.getName())
157157
) and
158158
// exclude block-level flow type annotations. For example: `(name: empty)`.
159-
not e.(ParExpr).getExpression().getLastToken().getNextToken().getValue() = ":"
159+
not e.(ParExpr).getExpression().getLastToken().getNextToken().getValue() = ":" and
160+
// exclude the first statement of a try block
161+
not e = any(TryStmt stmt).getBody().getStmt(0).(ExprStmt).getExpr()
160162
}

javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/ExprHasNoEffect.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| try.js:22:9:22:26 | x.ordinaryProperty | This expression has no effect. |
12
| tst2.js:3:4:3:4 | 0 | This expression has no effect. |
23
| tst.js:3:1:3:2 | 23 | This expression has no effect. |
34
| tst.js:5:1:5:2 | 23 | This expression has no effect. |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function try1(x) {
2+
try {
3+
x.ordinaryProperty; // OK - try/catch indicates intent to throw exception
4+
} catch (e) {
5+
return false;
6+
}
7+
return true;
8+
}
9+
10+
function try2(x) {
11+
try {
12+
x.ordinaryProperty; // OK - try/catch indicates intent to throw exception
13+
return x;
14+
} catch (e) {
15+
return false;
16+
}
17+
}
18+
19+
function try3(x) {
20+
try {
21+
x.ordinaryProperty()
22+
x.ordinaryProperty // NOT OK
23+
return x;
24+
} catch (e) {
25+
return false;
26+
}
27+
}

0 commit comments

Comments
 (0)