diff --git a/addons/misra.py b/addons/misra.py index cea5596d47d..45332d58681 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -2472,7 +2472,14 @@ def get_category(essential_type): rhs_category = get_category(rhs) if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed','unsigned'): self.reportError(tok, 10, 3) - if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and (lhs != "bool" or tok.astOperand2.str not in ('0','1')): + find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion + allow_bool_literal_0_1 = ( + find_std == "c89" and + lhs == "bool" and + tok.astOperand2 and + tok.astOperand2.str in ('0', '1') + ) + if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs) and not allow_bool_literal_0_1: self.reportError(tok, 10, 3) def misra_10_4(self, data): diff --git a/addons/test/misra/misra-test-c11.c b/addons/test/misra/misra-test-c11.c index 031bc361e5f..77679d71547 100644 --- a/addons/test/misra/misra-test-c11.c +++ b/addons/test/misra/misra-test-c11.c @@ -2,6 +2,7 @@ // ~/cppcheck/cppcheck --dump misra/misra-test-c11.c --std=c11 // ~/cppcheck/cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test-c11.c --std=c11 --platform=unix64 && python3 ../misra.py -verify misra/misra-test-c11.c.dump +#include #include typedef unsigned int UINT_TYPEDEF; @@ -23,3 +24,13 @@ static void misra6_1_fn(void) { struct_with_bitfields s; s.h = 61; } + +static void misra_10_3_c11(void) { + bool b = false; + bool b0 = 0; // 10.3 + bool b1 = 1; // 10.3 + b = 0; // 10.3 + b = 1; // 10.3 + b = false; // no-warning + b = true; // no-warning +} diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 743637117a5..6b6f52342aa 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -726,6 +726,10 @@ static void misra_10_3(uint32_t u32a, uint32_t u32b) { const char c = '0'; // no-warning bool b = true; // no-warning uint32_t u = UINT32_C(10); // no-warning + bool b0 = 0; // no-warning + bool b1 = 1; // no-warning + b = 0; // no-warning + b = 1; // no-warning } static void misra_10_4(u32 x, s32 y) { diff --git a/releasenotes.txt b/releasenotes.txt index f80ee8dc42d..6a071c2c2ce 100644 --- a/releasenotes.txt +++ b/releasenotes.txt @@ -5,7 +5,7 @@ Major bug fixes & crashes: - New checks: -- +- MISRA C 2012 rule 10.3 now warns on assigning integer literals 0 and 1 to bool in C99 and later while preserving the existing C89 behavior. C/C++ support: -