@@ -137,46 +137,34 @@ private static Expression lookup(Expression exp, Map<String, Expression> map, Se
137137 * @param exp
138138 * @param name
139139 * @param value
140- * @param canExcludeDefinition
140+ * @param topLevel
141141 *
142142 * @return true if used, false otherwise
143143 */
144- private static boolean hasUsage (Expression exp , String name , Expression value , boolean canExcludeDefinition ) {
145- // exclude own definitions
146- if (canExcludeDefinition && exp instanceof BinaryExpression binary && "==" .equals (binary .getOperator ())) {
144+ private static boolean hasUsage (Expression exp , String name , Expression value , boolean topLevel ) {
145+ if (topLevel && exp instanceof BinaryExpression binary && "&&" .equals (binary .getOperator ())) {
146+ return hasUsage (binary .getFirstOperand (), name , value , true )
147+ || hasUsage (binary .getSecondOperand (), name , value , true );
148+ }
149+
150+ if (topLevel && exp instanceof BinaryExpression binary && "==" .equals (binary .getOperator ())) {
147151 Expression left = binary .getFirstOperand ();
148152 Expression right = binary .getSecondOperand ();
149- if (left instanceof Var v && v .getName ().equals (name ) && right .equals (value )
150- && (isConstant (right ) || (!(right instanceof Var ) && canSubstitute (v , right ))))
151- return false ;
152- if (left instanceof FunctionInvocation && left .toString ().equals (name ) && right .equals (value )
153- && (isConstant (right ) || (!(right instanceof Var ) && !containsExpression (right , left ))))
154- return false ;
155- if (right instanceof Var v && v .getName ().equals (name ) && left .equals (value ) && isConstant (left ))
156- return false ;
157- if (right instanceof FunctionInvocation && right .toString ().equals (name ) && left .equals (value )
158- && isConstant (left ))
153+ boolean leftDefinition = name .equals (substitutionKey (left )) && right .equals (value )
154+ && (isConstant (right )
155+ || (left instanceof Var v && !(right instanceof Var ) && canSubstitute (v , right ))
156+ || (left instanceof FunctionInvocation && !(right instanceof Var )
157+ && !containsExpression (right , left )));
158+ boolean rightDefinition = name .equals (substitutionKey (right )) && left .equals (value ) && isConstant (left );
159+ if (leftDefinition || rightDefinition )
159160 return false ;
160161 }
161162
162- // usage found
163- if (exp instanceof Var var && var .getName ().equals (name )) {
164- return true ;
165- }
166- if (exp instanceof FunctionInvocation && exp .toString ().equals (name )) {
163+ if (name .equals (substitutionKey (exp )))
167164 return true ;
168- }
169-
170- // recurse children
171- if (exp .hasChildren ()) {
172- boolean childCanExcludeDefinition = exp instanceof BinaryExpression binary
173- && "&&" .equals (binary .getOperator ());
174- for (Expression child : exp .getChildren ())
175- if (hasUsage (child , name , value , childCanExcludeDefinition ))
176- return true ;
177- }
178-
179- // usage not found
165+ for (Expression child : exp .getChildren ())
166+ if (hasUsage (child , name , value , false ))
167+ return true ;
180168 return false ;
181169 }
182170
0 commit comments