File tree Expand file tree Collapse file tree
test/query-tests/Statements/UseOfReturnlessFunction Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1212import javascript
1313import Declarations.UnusedVariable
1414import Expressions.ExprHasNoEffect
15+ import Statements.UselessConditional
1516
1617predicate returnsVoid ( Function f ) {
1718 exists ( f .getBody ( ) .( Stmt ) ) and
@@ -57,9 +58,8 @@ predicate benignContext(Expr e) {
5758 // It is ok (or to be flagged by another query?) to await a non-async function.
5859 exists ( AwaitExpr await | await .getOperand ( ) = e and benignContext ( await ) )
5960 or
60-
6161 // Avoid double reporting with js/trivial-conditional
62- exists ( IfStmt ifStmt | ifStmt . getCondition ( ) = e )
62+ exists ( ASTNode cond | isExplicitConditional ( cond , e ) )
6363 or
6464 // Avoid double reporting with js/comparison-between-incompatible-types
6565 exists ( Comparison binOp | binOp .getAnOperand ( ) = e )
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import javascript
1616import semmle.javascript.RestrictedLocations
1717import semmle.javascript.dataflow.Refinements
1818import semmle.javascript.DefensiveProgramming
19+ import UselessConditional
1920
2021/**
2122 * Gets the unique definition of `v`.
@@ -123,21 +124,7 @@ predicate whitelist(Expr e) {
123124 isConstantBooleanReturnValue ( e )
124125}
125126
126- /**
127- * Holds if `e` is part of a conditional node `cond` that evaluates
128- * `e` and checks its value for truthiness, and the return value of `e`
129- * is not used for anything other than this truthiness check.
130- */
131- predicate isExplicitConditional ( ASTNode cond , Expr e ) {
132- e = cond .( IfStmt ) .getCondition ( )
133- or
134- e = cond .( LoopStmt ) .getTest ( )
135- or
136- e = cond .( ConditionalExpr ) .getCondition ( )
137- or
138- isExplicitConditional ( _, cond ) and
139- e = cond .( Expr ) .getUnderlyingValue ( ) .( LogicalBinaryExpr ) .getAnOperand ( )
140- }
127+
141128
142129/**
143130 * Holds if `e` is part of a conditional node `cond` that evaluates
Original file line number Diff line number Diff line change 1+ import javascript
2+
3+ /**
4+ * Holds if `e` is part of a conditional node `cond` that evaluates
5+ * `e` and checks its value for truthiness, and the return value of `e`
6+ * is not used for anything other than this truthiness check.
7+ */
8+ predicate isExplicitConditional ( ASTNode cond , Expr e ) {
9+ e = cond .( IfStmt ) .getCondition ( )
10+ or
11+ e = cond .( LoopStmt ) .getTest ( )
12+ or
13+ e = cond .( ConditionalExpr ) .getCondition ( )
14+ or
15+ isExplicitConditional ( _, cond ) and
16+ e = cond .( Expr ) .getUnderlyingValue ( ) .( LogicalBinaryExpr ) .getAnOperand ( )
17+ }
Original file line number Diff line number Diff line change 2929
3030 var d = 42 + ( 42 , onlySideEffects ( ) ) ; // NOT OK!
3131 console . log ( d ) ;
32+
33+ if ( onlySideEffects ( ) ) {
34+ // nothing.
35+ }
36+
37+ for ( i = 0 ; onlySideEffects ( ) ; i ++ ) {
38+ // nothing.
39+ }
3240} ) ( ) ;
You can’t perform that action at this time.
0 commit comments