1

The following code violates the MISRA C++ rule 0-1-4:

for (auto &a : b) {
    ... // The variable a is used only in the for condition.
}

Rule: A project shall not contain non-volatile POD variables having only one use. Variable 'a' is used only once, that is, during initialization.

What I tried:

for (const auto &a : b) {
    ... // The variable a is used only in the for condition.
}

But that was not the solution.

Does anyone have an idea how I can fix it?

KplnMoon
  • 151
  • 1
  • 10
  • Is `a` actually used within the `for` loop? – jjramsey May 11 '22 at 16:59
  • No. See comment in code. – KplnMoon May 11 '22 at 17:09
  • 1
    Perhaps request an exemption when a coding standard gets in the way? Tools should be working for you, not the other way around. – n. m. could be an AI May 11 '22 at 17:12
  • @n.1.8e9-where's-my-sharem. Do you think I should ignore this violation? – KplnMoon May 11 '22 at 17:24
  • If your project rules allow you to choose which violation to ignore, then I'd say feel free to ignore it and suppress the message. Though if `(void)a;` trick works, feel free to use it too. I assumed it would violate some other rule. – n. m. could be an AI May 11 '22 at 17:51
  • The solution below does not violate any other rule. Thanks anyway for your tip. Since this MISRA rule is from the "**required**" category, you can't ignore it (as I understand it). – KplnMoon May 11 '22 at 18:16
  • You can't use C++11 and MISRA C++ together, it's completely senseless. It's like having a corn flakes factory and placing one piece of nitroglycerine in each packet of corn flakes, then worry about if the nitroglycerine-placing machine is safe to use for the operator. Who cares, since you are, by design, counting on each of your products to explode in your customer's face? – Lundin May 12 '22 at 08:05

1 Answers1

2

Alas the current C++ grammar requires you to declare a variable when using the range-for form of the for loop:

for (auto& : b) {

is not allowed, despite it having potential applications (such as computing the number of elements in a container).

Writing

a;

or

(void)a;

in the loop body might work depending on the type. This would fool the static analyser and you can hope the expression gets compiled out.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks for the quick reply! :) That fixed it. But the new line violates another rule: "There shall be no dead code. Expression has no effect" – KplnMoon May 11 '22 at 17:11
  • @KplnMoon: I've added another trick. – Bathsheba May 11 '22 at 17:15
  • Wow! **(void)a;** does not cause any additional error and solves the problem. Thanks again! – KplnMoon May 11 '22 at 17:22
  • 1
    @KplnMoon that is a verbose **shut-up** to the compiler. – Red.Wave May 11 '22 at 18:08
  • @Red.Wave So you don't think it should be solved that way? – KplnMoon May 11 '22 at 18:18
  • @KplnMoon it's normally used as a patch. You have not used the object, yet it has a name. – Red.Wave May 11 '22 at 18:21
  • `#include ` for c++20: `std::ranges::for_each(b,[&](auto const &/*a*/){/*a has no name*/});` or `std::for_each(begin(b),end(b),[&](auto const&){/*a has no name again*/});` – Red.Wave May 11 '22 at 18:26
  • If there is a `break` then `find_if` works. `return true;` to `break` and `return false;` to `continue` with next element. These 2 library solutions are also imune to an alledged bug in range-based `for`. – Red.Wave May 11 '22 at 18:40