Skip to content

Commit 9bbef6f

Browse files
committed
Rule 7.0.2: Address review feedback
1 parent 51938af commit 9bbef6f

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

cpp/misra/src/rules/RULE-7-0-2/NoImplicitBoolConversion.ql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ predicate isBitFieldOfSizeOne(Expr expr) {
4343

4444
predicate isPointerType(Type t) {
4545
t.getUnspecifiedType() instanceof PointerType or
46-
t.getUnspecifiedType() instanceof ArrayType or
4746
t.getUnspecifiedType() instanceof PointerToMemberType
4847
}
4948

@@ -59,12 +58,15 @@ where
5958
// Exception 2: Contextual conversion from pointer
6059
not (
6160
isPointerType(conv.getExpr().getType()) and
62-
isInContextualBoolContext(conv.getExpr())
61+
// Checks if the unconverted expression for this conversion is in a contextual bool context
62+
// This handles the cases where we have multiple stacked conversions, e.g. when converting
63+
// an array to a pointer, then the pointer to bool
64+
isInContextualBoolContext(conv.getUnconverted())
6365
) and
64-
// Exception 3: Bit-field of size 1
65-
not isBitFieldOfSizeOne(conv.getExpr()) and
66-
// Exception 4: While condition declaration
67-
not isInWhileConditionDeclaration(conv.getExpr()) and
66+
// Exception 3: Unconverted expression is a bit-field of size 1
67+
not isBitFieldOfSizeOne(conv.getUnconverted()) and
68+
// Exception 4: Unconverted expression is in a while condition declaration
69+
not isInWhileConditionDeclaration(conv.getUnconverted()) and
6870
reason = "Conversion from '" + conv.getExpr().getType().toString() + "' to 'bool'"
6971
)
7072
or

cpp/misra/test/rules/RULE-7-0-2/NoImplicitBoolConversion.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
| test.cpp:182:7:182:8 | (bool)... | Conversion from 'float' to 'bool'. |
2222
| test.cpp:184:7:184:8 | (bool)... | Conversion from 'double' to 'bool'. |
2323
| test.cpp:186:7:186:8 | (bool)... | Conversion from 'long double' to 'bool'. |
24-
| test.cpp:195:7:195:8 | (bool)... | Conversion from 'int32_t *' to 'bool'. |
25-
| test.cpp:197:7:197:8 | (bool)... | Conversion from 'int32_t *' to 'bool'. |
2624
| test.cpp:199:13:199:14 | (bool)... | Conversion from 'int32_t *' to 'bool'. |
2725
| test.cpp:211:13:211:14 | (bool)... | Conversion from '..:: *' to 'bool'. |
2826
| test.cpp:212:13:212:14 | (bool)... | Conversion from '..:: *' to 'bool'. |

cpp/misra/test/rules/RULE-7-0-2/test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ void test_floating_point_conversion() {
192192
void test_array_conversion() {
193193
std::int32_t l1[5] = {1, 2, 3, 4, 5};
194194

195-
if (l1) { // NON_COMPLIANT
195+
if (l1) { // COMPLIANT
196196
}
197-
if (g8) { // NON_COMPLIANT
197+
if (g8) { // COMPLIANT
198198
}
199199
bool l2 = l1; // NON_COMPLIANT
200200
bool l3 = (l1 != nullptr); // COMPLIANT

0 commit comments

Comments
 (0)