JS: ignore useless-expr in first stmt in try block

This commit is contained in:
Asger Feldthaus
2020-01-14 11:17:54 +00:00
parent f7278d36e1
commit 6d9306366c
3 changed files with 31 additions and 1 deletions

View File

@@ -156,5 +156,7 @@ 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() = ":"
not e.(ParExpr).getExpression().getLastToken().getNextToken().getValue() = ":" and
// exclude the first statement of a try block
not e = any(TryStmt stmt).getBody().getStmt(0).(ExprStmt).getExpr()
}

View File

@@ -1,3 +1,4 @@
| try.js:22:9:22:26 | x.ordinaryProperty | This expression has no effect. |
| tst2.js:3:4:3:4 | 0 | This expression has no effect. |
| tst.js:3:1:3:2 | 23 | This expression has no effect. |
| tst.js:5:1:5:2 | 23 | This expression has no effect. |

View File

@@ -0,0 +1,27 @@
function try1(x) {
try {
x.ordinaryProperty; // OK - try/catch indicates intent to throw exception
} catch (e) {
return false;
}
return true;
}
function try2(x) {
try {
x.ordinaryProperty; // OK - try/catch indicates intent to throw exception
return x;
} catch (e) {
return false;
}
}
function try3(x) {
try {
x.ordinaryProperty()
x.ordinaryProperty // NOT OK
return x;
} catch (e) {
return false;
}
}