Skip to main content

IAR Embedded Workbench for RISC-V 3.40

ATH-cmp-unsign-pos

In this section:
Synopsis

An unsigned value is compared to see whether it is greater than or equal to 0.

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

A comparison is performed on an unsigned value, to see whether it is greater than or equal to 0. This comparison always returns true, and is redundant.

Coding standards
CWE 571

Expression is Always True

Code examples

The following code example fails the check and will give a warning:

int foo(unsigned int x)
{
  if (x >= 0)  //checking an unsigned for negativity
    return 1;
  else
    return 0;
}

The following code example passes the check and will not give a warning about this issue:

int foo(unsigned int x)
{
  if (x > 0)  //OK - x might be 0
    return 1;
  else
    return 0;
}