Cast via generic reference

 home quick fix  example  edit

cast_via_generic_reference

Description

Finds a down cast from a local reference variable that only holds a copy of another reference

Extended Information

Reports DATA o TYPE REF TO object. o = r. ... x ?= o. when all of the following hold:

* o is a local variable of a METHOD, FORM or FUNCTION, declared with DATA without VALUE and not chained,
typed REF TO object, or REF TO a class or interface that is the static type of r or a supertype of it
* o has exactly one write, o = r., exactly one read, the complete source of x ?= o., and its address is never
taken. x is not an inline declaration
* r is a plain object reference: a variable, or a chain of attributes and components without calls,
offsets, table expressions, field symbols or ->*
* r keeps its value from o = r. until the read, as in unnecessary_copy: for a local r, no write to it and its
address is never taken; for other r, in addition no method, function or form call and no write except to
local variables, by value parameters and RETURNING in between
* o = r. is executed before x ?= o. on every path: the read is in the same block or a nested block, and both
are in the same loop
* x ?= r. is valid: x or r is typed REF TO object or an interface, or the class of x and the class of r
are in the same inheritance line

The object is the same, so the cast succeeds or raises cx_sy_move_cast_error the same way.
try_cast_empty_catch can then replace a TRY around the cast.
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 and o = r., and casts r directly. No release requirement.

Default Configuration

Hover to see descriptions, Ctrl+Space for suggestions

Examples