Skip to content

Commit 593683a

Browse files
committed
Fixed #9459 (MISRA Rule 2.2: Find dead code in arithmetic operators)
1 parent f701a93 commit 593683a

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

addons/misra.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,23 @@ def misra_1_4(self, cfg):
14331433
'wcrtomb_s', 'mbsrtowcs_s', 'wcsrtombs_s'):
14341434
self.reportError(token, 1, 4)
14351435

1436+
def misra_2_2(self, cfg):
1437+
for token in cfg.tokenlist:
1438+
if (token.str in '+-') and token.astOperand2:
1439+
if simpleMatch(token.astOperand1, '0'):
1440+
self.reportError(token.astOperand1, 2, 2)
1441+
elif simpleMatch(token.astOperand2, '0'):
1442+
self.reportError(token.astOperand2, 2, 2)
1443+
if token.str == '*' and token.astOperand2:
1444+
if simpleMatch(token.astOperand2, '0'):
1445+
self.reportError(token.astOperand1, 2, 2)
1446+
elif simpleMatch(token.astOperand1, '0'):
1447+
self.reportError(token.astOperand2, 2, 2)
1448+
elif simpleMatch(token.astOperand1, '1'):
1449+
self.reportError(token.astOperand1, 2, 2)
1450+
elif simpleMatch(token.astOperand2, '1'):
1451+
self.reportError(token.astOperand2, 2, 2)
1452+
14361453
def misra_2_3(self, dumpfile, typedefInfo):
14371454
self._save_ctu_summary_typedefs(dumpfile, typedefInfo)
14381455

@@ -4227,6 +4244,7 @@ def fillVerifyExpected(verify_expected, tok):
42274244
self.printStatus('Checking %s, config %s...' % (dumpfile, cfg.name))
42284245

42294246
self.executeCheck(104, self.misra_1_4, cfg)
4247+
self.executeCheck(202, self.misra_2_2, cfg)
42304248
self.executeCheck(203, self.misra_2_3, dumpfile, cfg.typedefInfo)
42314249
self.executeCheck(204, self.misra_2_4, dumpfile, cfg)
42324250
self.executeCheck(205, self.misra_2_5, dumpfile, cfg)

addons/test/misra/misra-test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ static _Noreturn void misra_1_4_func(void) // 1.4
6565
printf_s("hello"); // 1.4
6666
}
6767

68+
static void misra_2_2(int x) {
69+
int a;
70+
a = x + 0; // 2.2
71+
a = 0 + x; // 2.2
72+
a = x * 0; // 2.2
73+
a = 0 * x; // 2.2
74+
a = x * 1; // 2.2
75+
a = 1 * x; // 2.2
76+
(void)a;
77+
}
6878

6979
/* // */ // 3.1
7080
/* /* */ // 3.1

0 commit comments

Comments
 (0)