From b81de5494e70bd0b768bc77e4f126ad398bfee52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 7 Oct 2013 18:01:08 +0200 Subject: [PATCH] CheckBool: Fixed false positive for calculation using bool result in rhs --- lib/checkbool.cpp | 21 +++++++++++++++++---- test/testbool.cpp | 6 ++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index cae0ec492..79bbef269 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -431,10 +431,23 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt() if (Token::Match(opTok, "<|>")) op = opTok->str()[0]; } else if (Token::Match(tok->previous(), "(|&&|%oror% %num% %comp% !")) { - numTok = tok; - opTok = tok->next(); - if (Token::Match(opTok, "<|>")) - op = opTok->str()[0]=='>'?'<':'>'; + const Token *rhs = tok->tokAt(3); + while (rhs) { + if (rhs->str() == "!") { + if (Token::simpleMatch(rhs, "! (")) + rhs = rhs->next()->link(); + rhs = rhs->next(); + } else if (rhs->isName() || rhs->isNumber()) + rhs = rhs->next(); + else + break; + } + if (Token::Match(rhs, "&&|%oror%|)")) { + numTok = tok; + opTok = tok->next(); + if (Token::Match(opTok, "<|>")) + op = opTok->str()[0]=='>'?'<':'>'; + } } // boolean result in lhs compared with <|<=|>|>= diff --git a/test/testbool.cpp b/test/testbool.cpp index 03bc0aa68..4febc3b96 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -386,6 +386,12 @@ private: check("void f(int a, int b, int c) { if (a != !b || c) {} }"); ASSERT_EQUALS("",errout.str()); + + check("void f(int a, int b, int c) { if (1 < !!a + !!b + !!c) {} }"); + ASSERT_EQUALS("",errout.str()); + + check("void f(int a, int b, int c) { if (1 < !(a+b)) {} }"); + TODO_ASSERT_EQUALS("error","",errout.str()); } void comparisonOfBoolExpressionWithInt3() {