diff --git a/javascript/ql/src/Expressions/ExprHasNoEffect.qll b/javascript/ql/src/Expressions/ExprHasNoEffect.qll index 15db9549f41..86790bb0da3 100644 --- a/javascript/ql/src/Expressions/ExprHasNoEffect.qll +++ b/javascript/ql/src/Expressions/ExprHasNoEffect.qll @@ -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() } diff --git a/javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/ExprHasNoEffect.expected b/javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/ExprHasNoEffect.expected index 108e0de6a9e..3189b68ea13 100644 --- a/javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/ExprHasNoEffect.expected +++ b/javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/ExprHasNoEffect.expected @@ -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. | diff --git a/javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/try.js b/javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/try.js new file mode 100644 index 00000000000..a665423bd81 --- /dev/null +++ b/javascript/ql/test/query-tests/Expressions/ExprHasNoEffect/try.js @@ -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; + } +}