Detects implicit conversions from smaller data types to larger data types
Justification: The compiler permits any assignments of different types when the value
range of the source type is completely contained within the value range of the target
type. However, the compiler will build a conversion into the code as late as possible.
For an assignment of type lint := dint * dint
, the compiler performs the implicit conversion only after multiplication: lint := TO_LINT(dint * dint)
. An overflow is therefore truncated. To prevent this, you can already convert the
elements: lint := TO_LINT(dint) * TO_LINT(dint)
. Therefore, it may be useful to report locations where the compiler implements implicit
conversions in order to check whether these are exactly what is intended. Furthermore,
explicit conversions can be used to improve portability to other systems when those
systems have more restrictive type checks.
Importance: Low
Example
PROGRAM PLC_PRG VAR byTemp : BYTE; usiTemp : USINT; uiTemp: UINT; iTemp : INT; udiTemp: UDINT; diTemp : DINT; uliTemp : ULINT; liTemp : LINT; lwTemp : LWORD; lrTemp : LREAL; END_VAR liTemp := iTemp; // SA0130 uliTemp := usiTemp; // SA0130 lwTemp := udiTemp; // SA0130 lrTemp := byTemp; // SA0130 diTemp := uiTemp; // SA0130 byTemp.5 := FALSE; // OK (BIT_BOOL conversion) --> SA0130: Implicit widening conversion from type 'INT' to type 'LINT' --> SA0130: Implicit widening conversion from type 'USINT' to type 'ULINT' --> SA0130: Implicit widening conversion from type 'UDINT' to type 'LWORD' --> SA0130: Implicit widening conversion from type 'BYTE' to type 'LREAL' --> SA0130: Implicit widening conversion from type 'UINT' to type 'DINT'