diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 6c57116aa..8c22553e8 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1395,8 +1395,7 @@ void CheckOther::checkConstVariable() { //Is it the right side of an initialization of a non-const reference bool usedInAssignment = false; - for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) - { + for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) { if (!Token::Match(tok, "& %var% = %varid%", var->declarationId())) continue; const Variable* refvar = tok->next()->variable(); @@ -1410,19 +1409,17 @@ void CheckOther::checkConstVariable() } // Skip if we ever cast this variable to a pointer/reference to a non-const type { - bool castToNonConst = [&] + bool castToNonConst = [&] { + for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) { - for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) - { - if (tok->isCast()) - { - bool isConst = 0 != (tok->valueType()->constness & (1 << tok->valueType()->pointer)); - if (!isConst) - return true; - } + if (tok->isCast()) { + bool isConst = 0 != (tok->valueType()->constness & (1 << tok->valueType()->pointer)); + if (!isConst) + return true; } - return false; - }(); + } + return false; + }(); if (castToNonConst) continue; } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 42cd0e2fb..2315bf126 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5387,7 +5387,7 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) vt.reference = Reference::None; setValueType(parent, vt); } else if (mIsCpp && ((Token::Match(parent->tokAt(-3), "%var% ; %var% =") && parent->strAt(-3) == parent->strAt(-1)) || - Token::Match(parent->tokAt(-1), "%var% ="))) { + Token::Match(parent->tokAt(-1), "%var% ="))) { Token *var1Tok = parent->strAt(-2) == ";" ? parent->tokAt(-3) : parent->tokAt(-1); Token *autoTok = nullptr; if (Token::Match(var1Tok->tokAt(-2), ";|{|}|(|const auto")) diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 937601bed..4d13d9293 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1144,8 +1144,7 @@ private: void findFunctionInBase(const std::string & name, nonneg int args, std::vector & matches) const; }; -enum class Reference -{ +enum class Reference { None, LValue, RValue diff --git a/test/testother.cpp b/test/testother.cpp index 3769f49d8..5a2f6bf14 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -2059,52 +2059,52 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const T& z = x;\n" //Make sure we find all assignments - " T& y = x\n" - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const T& z = x;\n" //Make sure we find all assignments + " T& y = x\n" + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U& y = x\n" - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U& y = x\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U& y = x\n" - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U& y = x\n" + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " my::type& y = x\n" //we don't know if y is const or not - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " my::type& y = x\n" //we don't know if y is const or not + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U& y = static_cast(x)\n" - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U& y = static_cast(x)\n" + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U& y = static_cast(x)\n" - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U& y = static_cast(x)\n" + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U& y = dynamic_cast(x)\n" - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U& y = dynamic_cast(x)\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check( "struct T : public U { void dostuff() const {}};\n" @@ -2115,10 +2115,10 @@ private: ); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U& y = dynamic_cast(x)\n" - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U& y = dynamic_cast(x)\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" "void a(T& x) {\n" @@ -2128,108 +2128,108 @@ private: "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U& y = dynamic_cast(x)\n" - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U& y = dynamic_cast(x)\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U& y = dynamic_cast(x)\n" - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U& y = dynamic_cast(x)\n" + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U* y = dynamic_cast(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U* y = dynamic_cast(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U * y = dynamic_cast(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U * y = dynamic_cast(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); TODO_ASSERT_EQUALS("can be const", errout.str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U const * y = dynamic_cast(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U const * y = dynamic_cast(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); TODO_ASSERT_EQUALS("can be const", errout.str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U * const y = dynamic_cast(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U * const y = dynamic_cast(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U const * const * const * const y = dynamic_cast(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U const * const * const * const y = dynamic_cast(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); TODO_ASSERT_EQUALS("can be const", errout.str(), ""); //Currently taking the address is treated as a non-const operation when it should depend on what we do with it check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U const * const * * const y = dynamic_cast(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U const * const * * const y = dynamic_cast(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " my::fancy const * * const y = dynamic_cast const * * const>(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " my::fancy const * * const y = dynamic_cast const * * const>(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " my::fancy const * const * const y = dynamic_cast const * const * const>(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " my::fancy const * const * const y = dynamic_cast const * const * const>(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U& y = (const U&)(x)\n" - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U& y = (const U&)(x)\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U& y = (U&)(x)\n" - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U& y = (U&)(x)\n" + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " const U& y = (typename const U&)(x)\n" - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " const U& y = (typename const U&)(x)\n" + "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared with const\n", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U& y = (typename U&)(x)\n" - " y.mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U& y = (typename U&)(x)\n" + " y.mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); check("struct T : public U { void dostuff() const {}};\n" - "void a(T& x) {\n" - " x.dostuff();\n" - " U* y = (U*)(&x)\n" - " y->mutate();\n" //to avoid warnings that y can be const - "}"); + "void a(T& x) {\n" + " x.dostuff();\n" + " U* y = (U*)(&x)\n" + " y->mutate();\n" //to avoid warnings that y can be const + "}"); ASSERT_EQUALS("", errout.str()); diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index c5f5c0894..75ae03f3e 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -967,8 +967,7 @@ private: ASSERT(p->valueType()->pointer == 1); } - void VariableValueTypeReferences() - { + void VariableValueTypeReferences() { { GET_SYMBOL_DB("void foo(int x) {}\n"); const Variable* const p = db->getVariableFromVarId(1);