Rules / unsigned-vs-zero
Comparing UINT or WORD with zero: IF x < 0 never runs
An unsigned variable is never negative, so x < 0 is always false and x >= 0 is always true. It usually means the wrong type was chosen: the negative guard was written on purpose and the type cancelled it.
Severity: Warning
Rule id: unsigned-vs-zero
Unsigned compared against zero
What the rule checks
An unsigned variable (UINT, UDINT, WORD, BYTE and the like) is compared against zero in a way whose answer is known in advance: x < 0 is never true, x >= 0 is never false. The compiler does not complain, the branch compiles and never runs. Usually this is not a style issue but the trace of a wrongly chosen type: the guard against a negative value was written deliberately and the type silently cancelled it, and where a clamp to zero was expected, the value wraps around instead (0 − 1 on a UINT gives 65535). The x >= 0 form is particularly nasty inside an alarm condition: it always holds, so the alarm either stays on permanently or masks the real one.
What it does to the plant
Instead of clamping at zero the value wraps: 0 - 1 on a UINT gives 65535 and the output goes to maximum.
Example
PROGRAM ScaleOutVAR uiCode : UINT; iDelta : INT;END_VARuiCode := uiCode + INT_TO_UINT(iDelta);IF uiCode < 0 THEN uiCode := 0;END_IF
PROGRAM ScaleOutVAR iCode : INT; iDelta : INT;END_VARiCode := iCode + iDelta;IF iCode < 0 THEN iCode := 0;END_IF
Both listings are run through the linter by an automated test: the rule fires on the first and stays silent on the second.
How to fix
- If a negative value is possible, switch the type to a signed one (INT, DINT or REAL). The guard will then do what it was meant to do.
- If a negative value is impossible, drop the dead branch: it gives a false sense of protection to whoever reads the code after you.
- Check the arithmetic above it: on an unsigned type
0 - 1yields the top of the range, which is exactly what a clamp to zero fails to catch.
Upload a PLCopen XML or CODESYS .export file: the report is free and the code is not stored.
The same linter without uploading a file: command line and GitHub Action.
Related rules
2026-11-26