This commit is contained in:
parent
83b5cb5b2f
commit
331db40d3b
|
@ -1429,7 +1429,25 @@ void CheckOther::checkConstVariable()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (tok->isUnaryOp("&") && Token::Match(tok, "& %varid%", var->declarationId())) {
|
if (tok->isUnaryOp("&") && Token::Match(tok, "& %varid%", var->declarationId())) {
|
||||||
usedInAssignment = isExpressionChangedAt(tok->next(), tok, 0, false, mSettings, true);
|
const Token* opTok = tok->astParent();
|
||||||
|
int argn = -1;
|
||||||
|
if (opTok && (opTok->isUnaryOp("!") || opTok->isComparisonOp()))
|
||||||
|
continue;
|
||||||
|
if (opTok && (opTok->isAssignmentOp() || opTok->isCalculation())) {
|
||||||
|
if (opTok->isCalculation()) {
|
||||||
|
if (opTok->astOperand1() != tok)
|
||||||
|
opTok = opTok->astOperand1();
|
||||||
|
else
|
||||||
|
opTok = opTok->astOperand2();
|
||||||
|
}
|
||||||
|
if (opTok && opTok->valueType() && var->valueType() && opTok->valueType()->isConst(var->valueType()->pointer))
|
||||||
|
continue;
|
||||||
|
} else if (const Token* ftok = getTokenArgumentFunction(tok, argn)) {
|
||||||
|
bool inconclusive{};
|
||||||
|
if (var->valueType() && !isVariableChangedByFunctionCall(ftok, var->valueType()->pointer, var->declarationId(), mSettings, &inconclusive) && !inconclusive)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
usedInAssignment = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (astIsRangeBasedForDecl(tok) && Token::Match(tok->astParent()->astOperand2(), "%varid%", var->declarationId())) {
|
if (astIsRangeBasedForDecl(tok) && Token::Match(tok->astParent()->astOperand2(), "%varid%", var->declarationId())) {
|
||||||
|
|
|
@ -2794,31 +2794,34 @@ private:
|
||||||
"void a(T& x) {\n"
|
"void a(T& x) {\n"
|
||||||
" x.dostuff();\n"
|
" x.dostuff();\n"
|
||||||
" const U * y = dynamic_cast<const U *>(&x);\n"
|
" const U * y = dynamic_cast<const U *>(&x);\n"
|
||||||
|
" y->mutate();\n" // to avoid warnings that y can be const
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
|
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"
|
check("struct T : public U { void dostuff() const {}};\n"
|
||||||
"void a(T& x) {\n"
|
"void a(T& x) {\n"
|
||||||
" x.dostuff();\n"
|
" x.dostuff();\n"
|
||||||
" U const * y = dynamic_cast<U const *>(&x);\n"
|
" U const * y = dynamic_cast<U const *>(&x);\n"
|
||||||
|
" y->mutate();\n" // to avoid warnings that y can be const
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
|
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"
|
check("struct T : public U { void dostuff() const {}};\n"
|
||||||
"void a(T& x) {\n"
|
"void a(T& x) {\n"
|
||||||
" x.dostuff();\n"
|
" x.dostuff();\n"
|
||||||
" U * const y = dynamic_cast<U * const>(&x);\n"
|
" const U const * const * const * const y = dynamic_cast<const U const * const * const * const>(&x);\n"
|
||||||
" y->mutate();\n" // to avoid warnings that y can be const
|
" y->mutate();\n" // to avoid warnings that y can be const
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
check("struct T : public U { void dostuff() const {}};\n"
|
check("struct T : public U { void dostuff() const {}};\n"
|
||||||
"void a(T& x) {\n"
|
"void a(T& x) {\n"
|
||||||
" x.dostuff();\n"
|
" x.dostuff();\n"
|
||||||
" U const * const * * const y = dynamic_cast<U const * const * * const>(&x);\n"
|
" const U const * const * const * const y = dynamic_cast<const U const * const * const * const>(&x);\n"
|
||||||
|
" y->mutate();\n" // to avoid warnings that y can be const
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:2]: (style) Parameter 'x' can be declared as reference to const\n", errout.str());
|
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"
|
check("struct T : public U { void dostuff() const {}};\n"
|
||||||
"void a(T& x) {\n"
|
"void a(T& x) {\n"
|
||||||
" x.dostuff();\n"
|
" x.dostuff();\n"
|
||||||
" my::fancy<typename type const *> const * * const y = dynamic_cast<my::fancy<typename type const *> const * * const>(&x);\n"
|
" const U const * const * * const y = dynamic_cast<const U const * const * * const>(&x);\n"
|
||||||
" y->mutate();\n" // to avoid warnings that y can be const
|
" y->mutate();\n" // to avoid warnings that y can be const
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
@ -3371,6 +3374,15 @@ private:
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 's1' can be declared as reference to const\n"
|
ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 's1' can be declared as reference to const\n"
|
||||||
"[test.cpp:1]: (style) Parameter 's2' can be declared as reference to const\n",
|
"[test.cpp:1]: (style) Parameter 's2' can be declared as reference to const\n",
|
||||||
errout.str());
|
errout.str());
|
||||||
|
|
||||||
|
check("struct S {\n"
|
||||||
|
" void f(int& r) { p = &r; }\n"
|
||||||
|
" int* p;\n"
|
||||||
|
"};\n"
|
||||||
|
"void g(std::vector<int>& v1, std::vector<int*>& v2) {\n"
|
||||||
|
" std::transform(v1.begin(), v1.end(), v2.begin(), [](auto& x) { return &x; });\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void constParameterCallback() {
|
void constParameterCallback() {
|
||||||
|
@ -3760,7 +3772,7 @@ private:
|
||||||
check("void f(int& i) {\n"
|
check("void f(int& i) {\n"
|
||||||
" new (&i) int();\n"
|
" new (&i) int();\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
TODO_ASSERT_EQUALS("", "[test.cpp:1]: (style) Parameter 'i' can be declared as reference to const\n", errout.str()); // don't crash
|
ASSERT_EQUALS("", errout.str()); // don't crash
|
||||||
|
|
||||||
check("void f(int& i) {\n"
|
check("void f(int& i) {\n"
|
||||||
" int& r = i;\n"
|
" int& r = i;\n"
|
||||||
|
|
Loading…
Reference in New Issue