From d162735cf0da4cbae3815e84d9569149b3a7008d Mon Sep 17 00:00:00 2001 From: tomasz-kaminski-sonarsource <79814193+tomasz-kaminski-sonarsource@users.noreply.github.com> Date: Wed, 9 Oct 2024 10:38:06 +0200 Subject: [PATCH] CPP-5803 S7035 Add exception for casts to bool --- rules/S7035/cfamily/rule.adoc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/rules/S7035/cfamily/rule.adoc b/rules/S7035/cfamily/rule.adoc index 745465e8b5..dd7124e772 100644 --- a/rules/S7035/cfamily/rule.adoc +++ b/rules/S7035/cfamily/rule.adoc @@ -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(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] ----