CPP-5803 S7035 Add exception for casts to bool

This commit is contained in:
tomasz-kaminski-sonarsource 2024-10-09 10:38:06 +02:00 committed by GitHub
parent d071c05987
commit d162735cf0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -65,7 +65,20 @@ This rule raises an issue when an enum is converted to an integral value without
=== Exceptions
Unscoped enums with no underlying type specified have an implementation-defined implicit underlying type. Calling `std::to_underlying` on them wouldn't make the code more portable so this rule doesn't raise on them.
The result of casting any integer type to `bool` does not depend on the specific integer type.
For the same reason, casting an enumeration to `bool` does not depend on the underlying type of enumerator, so this rule does not raise.
[source,cpp]
----
enum class Enum {A, B, C};
void foo(Enum e) {
auto i = static_cast<bool>(e); // Compliant by exception, the intent is obvious
...
}
----
Unscoped enums with no underlying type specified have an implementation-defined implicit underlying type.
Calling `std::to_underlying` on them would not make the code more portable so this rule does not raise on them.
[source,cpp]
----