From 63acd9bb3eb8ed442672f20e77290ed9e4f297d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 3 Apr 2011 22:12:22 +0200 Subject: [PATCH] Clarify calculation: Added warnings for << and >> --- lib/checkother.cpp | 16 ++++++++-------- lib/checkother.h | 4 ++-- test/testother.cpp | 5 +++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3822a06d2..a18ae900a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -86,10 +86,10 @@ void CheckOther::clarifyCalculation() continue; // calculation - if (!Token::Match(cond, "[+-*/]")) + if (!Token::Match(cond, "[+-*/]") && !Token::Match(cond, "<<|>>")) continue; - const char op = cond->str()[0]; + const std::string &op = cond->str(); cond = cond->previous(); // skip previous multiplications.. @@ -106,28 +106,28 @@ void CheckOther::clarifyCalculation() } else if (cond->isName() || cond->isNumber()) { - if (Token::Match(cond->previous(),"return|+|-|,|(")) + if (Token::Match(cond->previous(),("return|+|-|,|(|"+op).c_str())) clarifyCalculationError(cond, op); } } } } -void CheckOther::clarifyCalculationError(const Token *tok, char op) +void CheckOther::clarifyCalculationError(const Token *tok, const std::string &op) { // suspicious calculation - const std::string calc(std::string("'a") + op + "b?c:d'"); + const std::string calc("'a" + op + "b?c:d'"); // recommended calculation #1 - const std::string s1(std::string("'(a") + op + "b)?c:d'"); + const std::string s1("'(a" + op + "b)?c:d'"); // recommended calculation #2 - const std::string s2(std::string("'a") + op + "(b?c:d)'"); + const std::string s2("'a" + op + "(b?c:d)'"); reportError(tok, Severity::style, "clarifyCalculation", - std::string("Clarify calculation precedence for ") + op + " and ?\n" + + "Clarify calculation precedence for " + op + " and ?\n" "Suspicious calculation. Please use parentheses to clarify the code. " "The code " + calc + " should be written as either " + s1 + " or " + s2 + "."); } diff --git a/lib/checkother.h b/lib/checkother.h index ad2b8c5ee..8f82ba07f 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -97,7 +97,7 @@ public: /** @brief Clarify calculation for ".. a * b ? .." */ void clarifyCalculation(); - void clarifyCalculationError(const Token *tok, char op); + void clarifyCalculationError(const Token *tok, const std::string &op); /** @brief Suspicious condition (assignment+comparison) */ void clarifyCondition(); @@ -269,7 +269,7 @@ public: c.unassignedVariableError(0, "varname"); c.catchExceptionByValueError(0); c.memsetZeroBytesError(0, "varname"); - c.clarifyCalculationError(0, '+'); + c.clarifyCalculationError(0, "+"); c.clarifyConditionError(0); c.incorrectStringCompareError(0, "substr", "\"Hello World\"", "12"); c.incrementBooleanError(0); diff --git a/test/testother.cpp b/test/testother.cpp index eff97d0ff..addc62bbe 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2291,6 +2291,11 @@ private: " printf(\"%i\", 1 + 1 ? 1 : 2);\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for + and ?\n", errout.str()); + + check("void f() {\n" + " std::cout << x << 1 ? 2 : 3;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) Clarify calculation precedence for << and ?\n", errout.str()); } // clarify conditions with = and comparison