Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3114,6 +3114,8 @@ static const Token* findExpressionChangedImpl(const Token* expr,
if (vt->type == ValueType::ITERATOR)
++indirect;
}
if (indirect == 0 && tok2->astParent() && tok2->astParent()->isUnaryOp("*"))
++indirect;
for (int i = 0; i <= indirect; ++i) {
if (isExpressionChangedAt(tok, tok2, i, global, settings, depth))
return true;
Expand Down
10 changes: 7 additions & 3 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1769,9 +1769,13 @@ 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()) {
if (Token::Match(tok, "& %var% = %varid%", var->declarationId())) {
const Variable* refvar = tok->next()->variable();
if (refvar && !refvar->isConst() && refvar->nameToken() == tok->next()) {
if (Token::Match(tok, "%name% = %varid%", var->declarationId())) {
const Variable* refvar = tok->variable();
if (tok->strAt(-1) == "&" && refvar && !refvar->isConst() && refvar->nameToken() == tok) {
usedInAssignment = true;
break;
}
if (!tok->valueType() || tok->valueType()->type == ValueType::Type::RECORD) {
usedInAssignment = true;
break;
}
Expand Down
21 changes: 21 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4120,6 +4120,27 @@ class TestOther : public TestFixture {
" std::string _s;\n"
"};\n");
ASSERT_EQUALS("", errout_str());

check("void f(int& r) {\n" // #9761
" o1 = r;\n"
"}\n"
"boost::optional<int&> o2;\n"
"void g(int& r) {\n"
" o2 = r;\n"
"}\n"
"struct T {\n"
" int* p;\n"
" T& operator=(int& rhs) { p = &rhs; return *this; }\n"
"};\n"
"void h(T& t, int& r) {\n"
" t = r;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("void f(std::optional<int>& o) {\n"
" *o = 1;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void constParameterCallback() {
Expand Down
Loading