Constant condition
constant_condition
Description
Finds IF and ELSEIF conditions on local variables whose value is known, and conditions that compare a field with two different values and are always true or always false
Extended Information
Reports two cases, for local variables of a METHOD, FORM or FUNCTION.
1. DATA(c) = <constant>. where the constant is a character literal like 'X', or a character constant like
abap_true, abap_false or space, and the only other reference to c is the condition of an IF or ELSEIF:
c = <constant>, c <> <constant>, EQ, NE, c IS INITIAL, c IS NOT INITIAL, optionally with NOT.
The declaration must be executed before the condition on every path: the IF is in the block of the
declaration or a nested block. The quick fix deletes the declaration.
2. DATA v TYPE ... without VALUE, or DATA(v) = VALUE t( ). with an explicit type and nothing in the parentheses,
where v is never changed, except by CLEAR v., and its address is never taken, in a condition v IS [NOT] INITIAL
or v IS [NOT] BOUND. The declaration is kept.
The quick fix removes the IF branch that is never taken, or keeps the branch that is always taken and
removes the rest, also in ELSEIF chains: a first IF branch that is never taken turns the next ELSEIF into IF,
an ELSEIF branch that is always taken turns into ELSE. Nothing is reported if a removed branch declares
data that is used outside of it.
Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
INCLUDE, test seams, enhancements or native SQL. No release requirement.
Option exclusiveValues: reports x <> a OR x <> b, which is always true, and x = a AND x = b, which is always
false, in any condition of a METHOD, FORM or FUNCTION, also with dynamic code. Also with NE and EQ, with NOT,
with the operands swapped, and with more conditions joined by the same operator. x is a data object of type c,
generic c or string, without calls, offsets or table expressions. a and b are character literals or character
constants, also components of constant structures of a class or interface, with different values. Usually one
of the operators or one of the values is wrong, there is no quick fix.
Not reported: conditions that mix AND and OR or contain a call, values that are not known, like constants of
classes outside the registry, other types of x, which can convert different values to the same one, ASSERT,
and SQL conditions, where a NULL value makes both comparisons unknown.