Single use expression temporary
single_use_expression_temp
Description
Finds a local variable that only passes the value of an expression on to its single use
Extended Information
Reports DATA(v) =
when all of the following hold:
* v is a local variable of a METHOD, FORM or FUNCTION, its address is never taken. DATA(v) has no other
reference than its single read; DATA v TYPE t. without VALUE and not chained, or DATA(v) = VALUE t( ). with
nothing in the parentheses, has exactly one write besides CLEARs, v =
and exactly one read after it
* the read is one of
* the complete source of y = v.
* the receiver of a method call or attribute access: v->m( ), v->attr
* an actual parameter for an importing parameter: m( v ), m( p = v ), CALL METHOD m EXPORTING p = v,
NEW c( p = v ) or CREATE OBJECT o EXPORTING p = v
* APPEND v TO itab.
* an operand of a comparison with =, <>, <, >, <=, >= or their word forms, in IF, ELSEIF or xsdbool( )
* an embedding in a string template: |{ v }|
* the read is in the statement directly after the assignment, in the same block, ignoring declarations like
DATA and TYPES in between, unless the expression is
constant: a literal, a string template without embeddings, CONV t(
overflow, or a constant. Then the assignment must be executed before the read on every path: the read is in
the block of the assignment or a nested block
* the result type of the expression does not depend on the target: no arithmetic or bit operators, no unary
minus, no numeric functions like abs( ) or trunc( ) whose type follows the argument, no CONV or EXACT except
the constant CONV t( literal ), no # type inference at the top. String templates, && concatenations, literals, method calls, NEW, CAST,
constructor expressions with explicit type and table expressions are fine
* for DATA v TYPE t. and DATA(v) = VALUE t( )., the static type of the expression is identical to t. Into y = v. also a reference whose
static type is a subtype of t, unless y is an inline declaration, and an expression whose type depends on the
target, like arithmetic, if y has a type identical to t
* VALUE, CORRESPONDING, COND, SWITCH, REDUCE and FILTER can build their result directly in the target,
they are only moved into a new variable, or a local variable that they do not read and whose address
is never taken
* the order of evaluation does not change: if the expression is not constant, nothing that is evaluated before the
read in the same statement calls a method or function, constructs an object, accesses a table or an attribute
via a reference, does arithmetic, or reads data other than local variables, pass-by-value parameters and
constants; the read is not inside COND, SWITCH, VALUE or other constructor expressions except NEW, and not
after AND, OR or EQUIV, where it might not be evaluated. This does not apply to expressions that only read
local variables and cannot raise an exception
* a data object that other code can change is only moved if it keeps its value until the read, see
unnecessary_copy, a data object read by DATA(v) = . is left for unnecessary_copy, and a local
variable copied into y = v. for copy_chain
* a character constant in IF, ELSEIF, WHILE, CHECK, CASE or WHEN is left for constant_condition
Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
INCLUDE, test seams, enhancements or native SQL.
The quick fix deletes the declaration, the CLEARs and the assignment, and replaces the read of v with the
expression. Moving the expression of DATA v TYPE t. into other places than y = v. requires 7.40 SP02 or above,
ABAP Cloud or open-abap.