Fixed #9459 (MISRA Rule 2.2: Find dead code in arithmetic operators)

This commit is contained in:
Daniel Marjamäki 2021-11-17 17:44:54 +01:00
parent f701a9361d
commit 593683ab69
2 changed files with 28 additions and 0 deletions

View File

@ -1433,6 +1433,23 @@ class MisraChecker:
'wcrtomb_s', 'mbsrtowcs_s', 'wcsrtombs_s'):
self.reportError(token, 1, 4)
def misra_2_2(self, cfg):
for token in cfg.tokenlist:
if (token.str in '+-') and token.astOperand2:
if simpleMatch(token.astOperand1, '0'):
self.reportError(token.astOperand1, 2, 2)
elif simpleMatch(token.astOperand2, '0'):
self.reportError(token.astOperand2, 2, 2)
if token.str == '*' and token.astOperand2:
if simpleMatch(token.astOperand2, '0'):
self.reportError(token.astOperand1, 2, 2)
elif simpleMatch(token.astOperand1, '0'):
self.reportError(token.astOperand2, 2, 2)
elif simpleMatch(token.astOperand1, '1'):
self.reportError(token.astOperand1, 2, 2)
elif simpleMatch(token.astOperand2, '1'):
self.reportError(token.astOperand2, 2, 2)
def misra_2_3(self, dumpfile, typedefInfo):
self._save_ctu_summary_typedefs(dumpfile, typedefInfo)
@ -4227,6 +4244,7 @@ class MisraChecker:
self.printStatus('Checking %s, config %s...' % (dumpfile, cfg.name))
self.executeCheck(104, self.misra_1_4, cfg)
self.executeCheck(202, self.misra_2_2, cfg)
self.executeCheck(203, self.misra_2_3, dumpfile, cfg.typedefInfo)
self.executeCheck(204, self.misra_2_4, dumpfile, cfg)
self.executeCheck(205, self.misra_2_5, dumpfile, cfg)

View File

@ -65,6 +65,16 @@ static _Noreturn void misra_1_4_func(void) // 1.4
printf_s("hello"); // 1.4
}
static void misra_2_2(int x) {
int a;
a = x + 0; // 2.2
a = 0 + x; // 2.2
a = x * 0; // 2.2
a = 0 * x; // 2.2
a = x * 1; // 2.2
a = 1 * x; // 2.2
(void)a;
}
/* // */ // 3.1
/* /* */ // 3.1