diff --git a/addons/misra.py b/addons/misra.py index 4ad437fdd..b0e9bb8ca 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -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) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 573563c51..6c2bf1b7b 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -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