Problem resolving rule MISRA C++ 2008, 5-0-10

Problem resolving rule MISRA C++ 2008, 5-0-10

SonarQube Version: 7.9.1.27448
C++11

I would like to activate the following Misra Rule on my Code:

Misra Rule MISRA C++ 2008, 5-0-10 :
If the bitwise operators ~ and << are applied to an operand with an underlying type of unsigned char or unsigned short, the result shall be immediately cast to the underlying type of the operand.

My Code:

void someFunction(const uint8_t bitmask)
{
   uint8_t result	= 0;
   uint8_t invert = (~bitmask)
   result &= invert;
}

Now Sonar want that I “Add an explicit cast to the result of the “~” operator.” to my code.

Unfortunate i get the error:

As you can see I tried already to cast the ~ Operation, but it didn’t work.
I tried different paranthesis and different casts but Sonar always throw this error.
Can somebody help me, please?

Hello @Sugus,

First, I would like to emphasize that the rule S853 that we implemented is not equivalent to the MISRA rule. It was developed to address the same kind of issue (this is why it is described as based-on-misra), but differs in what exactly is detected.

Now, for your problem, after some tests, I discovered that is is caused by the const qualification of your parameter. On my machine, the following code does not trigger the issue:

void someFunction(const uint8_t bitmask)
{
  uint8_t result = 0;
  uint8_t invert = static_cast<const uint8_t>(~bitmask);
  result &= invert;
}

You can add this const to your code, but I believe we should not have this behaviour, and created an issue to improve the situation. You can follow its progress here.

Thank you!

Thank you! This solution worked and I will keep an eye on the jira task.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.