0

For example, I have this snippet:

const int array_type = model.accessors[accessor_index].type;
Assert(array_type == TypeCode<T>(), "");

And I get this error:

Src/Engine/Animation/GltfLib.cpp:103:26: style: Variable 'array_type' is assigned a value that is never used. [unreadVariable]
    const int array_type = model.accessors[accessor_index].type;

Assert is a macro that was based on the regular assert but uses some internal logging mechanisms on top of throwing the error.

Is there a way to make cppcheck notice that the variable is actually used?

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • 3
    If that `Assert` macro is based on the standard library's `assert` macro, this code can, indeed, not use `array_type`. That happens when `assert` is disabled. – Pete Becker May 15 '21 at 19:03
  • So to make cppcheck happy I would need to make the code available only when the Assert is enabled? – Makogan May 15 '21 at 19:14
  • 2
    For the standard library’s `assert` macro, using `model.accessors[accessor_index].type == TypeCode()` and dropping `array_type` would probably get rid of the warning. – Pete Becker May 15 '21 at 19:35

1 Answers1

1

The warning is telling you that: when Assert is disabled, you are creating a variable that is never used.

I would suggest moving the statement into the assert statement; however, you can also use the processor to remove the code when debugging is disabled.

Assert(model.accessors[accessor_index].type == TypeCode<T>(), "");

If, on the other hand, you do need that line of code (perhaps because std::map::operator[] is non-const) then you should use at in the assert line and add a comment.

unDeadHerbs
  • 1,306
  • 1
  • 11
  • 19