cpp:S3630 shouldn't be reported if you're not using C++20

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

Hey there.

What version of what product(s) are you using?

It’s in the tags. Latest version of the VSCode SonarLint extension - v3.21.0

Thanks.

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.
image

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 std::bit_cast.

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 std::bit_cast

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.

2 Likes

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.

1 Like

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

Hello,

We recently improved the description of this rule. I hope you’ll like the new wording and examples better.

1 Like