The description of cpp:S3630 says:
Since C++20, a std::bit_cast should be used instead of reinterpret_cast
But we’re not using C++20. The settings.json for VSCode explicilty sets C++17 and our compiler doesn’t support C++20.
I see this was raised against SonarQube before: S3630 is raised on C++17 project - #3 by Abbas
What version of what product(s) are you using?
It’s in the tags. Latest version of the VSCode SonarLint extension - v3.21.0
Are you sure that the issue message itself is telling you that you should use
std::bit_cast – or is it just information in the rule description?
A code sample / screenshot where the issue is being raised would help to clarify.
It’s information in the rule description, but it’s the only way of fixing the warning is by using a C++20 feature.
Hello @Tom_Isaacson ,
As Abbas mentions on the other thread,
reinterpret_cast should not be used regardless of the standard. It is a dangerous cast, and can end on Undefined Behavior.
Note that SonarLint is just saying “Replace reinterpret_cast with a safer cast”. The rule description is generic and what it tries to say is if you are using C++20 you can use
If you are using C++17 and you are trying to reinterpret one type as another, you should use
std::memcpy. This does not introduce UB and the compilers can be pretty clever at identifying the pattern, so there is no overhead.
P.S The day you move to C++20, S6181, which is C++20 specific, will remind you to switch to
By the way, thank you for the feedback! I have noted that the wording of the rule can be confusing and lacks examples for C++17 or prior. We will take this into account the next time we re-iterate the rule description.
I’d recommend adding the comment about memcpy() to the wording of the rule. If it only suggests a C++20 feature as a fix then it’s not helpful.
Completely agreed. I have added that to the notes.