codeql-coding-standards icon indicating copy to clipboard operation
codeql-coding-standards copied to clipboard

`A4-7-1`: Guard using "." operator getting ignored

Open nbusser opened this issue 9 months ago • 0 comments

Affected rules

  • A4-7-1

Description

Changing a class/struct's member value with an arithmetic expression triggers a A4-7-1 warning, despite using an appropriate type guard (example function false_positive).
I have to workaround it by destructuring my member (example function true_negative).

It seems that the "." operator is badly supported.

Triggered warning is cpp/autosar/integer-expression-lead-to-data-loss.

Example

struct A {
  std::int32_t s32;
}

void false_positive(A a) {
  if (a.s32 > std::numeric_limits<std::int32_t>::min()) {
    // Is supposed to be compliant with A4-7-1, but CodeQL reports a warning
    --a.s32;
  }
}

void true_negative(A a) {
  auto s32 = a.s32;
  if (s32 > std::numeric_limits<std::int32_t>::min()) {
    // Compliant with A4-7-1
    --s32;
  }
  a.s32 = s32;
}

nbusser avatar Jun 01 '24 07:06 nbusser