From 036545d1d5dce315403a97129fb9aedc0a7c18d2 Mon Sep 17 00:00:00 2001 From: Devansh Varshney Date: Sun, 29 Mar 2026 15:10:58 +0530 Subject: [PATCH 1/2] misra: avoid false positives for true/false after #14607 --- addons/misra.py | 49 ++++++++++++++++++------------ addons/test/misra/misra-test-c11.c | 2 ++ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index 45332d58681..d531ea8a68d 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -2460,27 +2460,38 @@ def get_category(essential_type): if essential_type.split(' ')[0] in ('unsigned', 'signed'): return essential_type.split(' ')[0] return None + for tok in cfg.tokenlist: - if tok.isAssignmentOp: - lhs = getEssentialType(tok.astOperand1) - rhs = getEssentialType(tok.astOperand2) - #print(lhs) - #print(rhs) - if lhs is None or rhs is None: + if not tok.isAssignmentOp: + continue + + lhs = getEssentialType(tok.astOperand1) + rhs = getEssentialType(tok.astOperand2) + if lhs is None or rhs is None: + continue + + find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion + + rhs_tok = tok.astOperand2 + rhs_macro_name = rhs_tok.macroName if rhs_tok else None + rhs_spelling = rhs_macro_name if rhs_macro_name in ('true', 'false') else rhs_tok.str + + rhs_is_source_bool_literal = rhs_spelling in ('true', 'false') + rhs_is_source_int_literal_0_1 = rhs_spelling in ('0', '1') + + if lhs == 'bool': + if rhs_is_source_bool_literal: continue - lhs_category = get_category(lhs) - 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) - 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) + if find_std == 'c89' and rhs_is_source_int_literal_0_1: + continue + + lhs_category = get_category(lhs) + 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): + self.reportError(tok, 10, 3) def misra_10_4(self, data): op = {'+', '-', '*', '/', '%', '&', '|', '^', '+=', '-=', ':'} diff --git a/addons/test/misra/misra-test-c11.c b/addons/test/misra/misra-test-c11.c index 77679d71547..aa00b2f0b42 100644 --- a/addons/test/misra/misra-test-c11.c +++ b/addons/test/misra/misra-test-c11.c @@ -29,6 +29,8 @@ static void misra_10_3_c11(void) { bool b = false; bool b0 = 0; // 10.3 bool b1 = 1; // 10.3 + bool bf = false; // no-warning + bool bt = true; // no-warning b = 0; // 10.3 b = 1; // 10.3 b = false; // no-warning From a7340d176e1fdf261aefdc240f18c2fb569a413d Mon Sep 17 00:00:00 2001 From: Devansh Varshney Date: Sun, 29 Mar 2026 15:48:55 +0530 Subject: [PATCH 2/2] misra: remove trailing whitespace --- addons/misra.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index d531ea8a68d..43b92b51fb0 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -2460,36 +2460,36 @@ def get_category(essential_type): if essential_type.split(' ')[0] in ('unsigned', 'signed'): return essential_type.split(' ')[0] return None - + for tok in cfg.tokenlist: if not tok.isAssignmentOp: continue - + lhs = getEssentialType(tok.astOperand1) rhs = getEssentialType(tok.astOperand2) if lhs is None or rhs is None: continue - + find_std = cfg.standards.c if cfg.standards and cfg.standards.c else self.stdversion - + rhs_tok = tok.astOperand2 rhs_macro_name = rhs_tok.macroName if rhs_tok else None rhs_spelling = rhs_macro_name if rhs_macro_name in ('true', 'false') else rhs_tok.str - + rhs_is_source_bool_literal = rhs_spelling in ('true', 'false') rhs_is_source_int_literal_0_1 = rhs_spelling in ('0', '1') - + if lhs == 'bool': if rhs_is_source_bool_literal: continue if find_std == 'c89' and rhs_is_source_int_literal_0_1: continue - + lhs_category = get_category(lhs) 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): self.reportError(tok, 10, 3)