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

Open
#600 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
devtools

Research direction

Start by locating the A4-7-1 implementation that emits cpp/autosar/integer-expression-lead-to-data-loss, then reproduce the false_positive and true_negative examples. Trace how the guard is recognized for a member accessed with the "." operator. Done means the guarded decrement in false_positive no longer warns while the existing true_negative behavior remains compliant.

Written by the indexing model from the issue text.

Description

Difficulty-High false positive/false negative Impact-Medium Standard-AUTOSAR user-report
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;
}
Dominant language
CodeQL
Stars
227
Forks
82
Avg merge
6d 7h
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from github/codeql-coding-standards

All issues in github/codeql-coding-standards

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.