@@ -115,25 +115,21 @@ predicate whitelist(Expr e) {
115115}
116116
117117/**
118- * Holds if `e` is part of a conditional node `cond` that evaluates
119- * `e` and checks its value for truthiness.
118+ * Gets the `&&` expression that defines this guard node, if any.
120119 */
121- predicate isConditional ( ASTNode cond , Expr e ) {
122- e = cond .( IfStmt ) .getCondition ( ) or
123- e = cond .( ConditionalExpr ) .getCondition ( ) or
124- e = cond .( LogAndExpr ) .getLeftOperand ( ) or
125- e = cond .( LogOrExpr ) .getLeftOperand ( )
120+ LogAndExpr getLogAndExpr ( ConditionGuardNode guard ) {
121+ result .getLeftOperand ( ) .stripParens ( ) = guard .getTest ( )
126122}
127123
128- from ASTNode cond , DataFlow:: AnalyzedNode op , boolean cv , ASTNode sel , string msg
129- where isConditional ( cond , op .asExpr ( ) ) and
124+ from ConditionGuardNode guard , DataFlow:: AnalyzedNode op , boolean cv , ASTNode sel , string msg
125+ where guard . getTest ( ) = op .asExpr ( ) and
130126 cv = op .getTheBooleanValue ( ) and
131127 not whitelist ( op .asExpr ( ) ) and
132128
133129 // if `cond` is of the form `<non-trivial truthy expr> && <something>`,
134130 // we suggest replacing it with `<non-trivial truthy expr>, <something>`
135- if cond instanceof LogAndExpr and cv = true and not op .asExpr ( ) .isPure ( ) then
136- ( sel = cond and msg = "This logical 'and' expression can be replaced with a comma expression." )
131+ if exists ( getLogAndExpr ( guard ) ) and cv = true and not op .asExpr ( ) .isPure ( ) then
132+ ( sel = getLogAndExpr ( guard ) and msg = "This logical 'and' expression can be replaced with a comma expression." )
137133
138134 // otherwise we just report that `op` always evaluates to `cv`
139135 else (
0 commit comments