Skip to content

Commit 778b344

Browse files
committed
Rule 7.0.6: Ignore compound expressions
1 parent 0ed1689 commit 778b344

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

cpp/misra/src/rules/RULE-7-0-6/NumericAssignmentTypeMismatch.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class NumericType extends Type {
6969
}
7070

7171
predicate isAssignment(Expr source, NumericType targetType, string context) {
72-
// Assignment operator
73-
exists(Assignment assign |
72+
// Assignment operator (but not compound assignment)
73+
exists(AssignExpr assign |
7474
assign.getRValue() = source and
7575
context = "assignment"
7676
|
@@ -191,7 +191,7 @@ predicate hasConstructorException(FunctionCall call) {
191191
*/
192192
class IdExpression extends VariableAccess {
193193
IdExpression() {
194-
// Not a member variable
194+
// Not a member variable access (no dot or arrow)
195195
(
196196
not exists(this.getQualifier())
197197
or

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,25 @@ void test_references() {
359359
std::uint64_t l4 = 1000;
360360
std::uint64_t &l5 = l4;
361361
f14(l5); // NON_COMPLIANT - narrowing conversion through reference
362+
}
363+
364+
// Test compound assignments - rule does not apply to compound assignments
365+
void test_compound_assignments() {
366+
std::uint8_t l1 = 10;
367+
std::uint16_t l2 = 100;
368+
std::int8_t l3 = 5;
369+
float l4 = 1.5f;
370+
371+
l1 += l2; // COMPLIANT - compound assignment, rule does not apply
372+
l1 -= l3; // COMPLIANT - compound assignment, rule does not apply
373+
l2 *= l1; // COMPLIANT - compound assignment, rule does not apply
374+
l2 /= l3; // COMPLIANT - compound assignment, rule does not apply
375+
l1 %= l3; // COMPLIANT - compound assignment, rule does not apply
376+
l2 &= l1; // COMPLIANT - compound assignment, rule does not apply
377+
l2 |= l3; // COMPLIANT - compound assignment, rule does not apply
378+
l2 ^= l1; // COMPLIANT - compound assignment, rule does not apply
379+
l2 <<= 2; // COMPLIANT - compound assignment, rule does not apply
380+
l2 >>= 1; // COMPLIANT - compound assignment, rule does not apply
381+
l4 += l1; // COMPLIANT - compound assignment, rule does not apply
382+
l4 -= s32; // COMPLIANT - compound assignment, rule does not apply
362383
}

0 commit comments

Comments
 (0)