Skip to content

Commit 4bebfde

Browse files
committed
Fix
1 parent 26e2547 commit 4bebfde

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

lib/checkother.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,9 +1769,13 @@ void CheckOther::checkConstVariable()
17691769
//Is it the right side of an initialization of a non-const reference
17701770
bool usedInAssignment = false;
17711771
for (const Token* tok = var->nameToken(); tok != scope->bodyEnd && tok != nullptr; tok = tok->next()) {
1772-
if (Token::Match(tok, "& %var% = %varid%", var->declarationId())) {
1773-
const Variable* refvar = tok->next()->variable();
1774-
if (refvar && !refvar->isConst() && refvar->nameToken() == tok->next()) {
1772+
if (Token::Match(tok, "%name% = %varid%", var->declarationId())) {
1773+
const Variable* refvar = tok->variable();
1774+
if (tok->strAt(-1) == "&" && refvar && !refvar->isConst() && refvar->nameToken() == tok) {
1775+
usedInAssignment = true;
1776+
break;
1777+
}
1778+
if (!tok->valueType() || tok->valueType()->type == ValueType::Type::RECORD) {
17751779
usedInAssignment = true;
17761780
break;
17771781
}

test/testother.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4121,6 +4121,22 @@ class TestOther : public TestFixture {
41214121
"};\n");
41224122
ASSERT_EQUALS("", errout_str());
41234123

4124+
check("void f(int& r) {\n" // #9761
4125+
" o1 = r;\n"
4126+
"}\n"
4127+
"boost::optional<int&> o2;\n"
4128+
"void g(int& r) {\n"
4129+
" o2 = r;\n"
4130+
"}\n"
4131+
"struct T {\n"
4132+
" int* p;\n"
4133+
" T& operator=(int& rhs) { p = &rhs; return *this; }\n"
4134+
"};\n"
4135+
"void h(T& t, int& r) {\n"
4136+
" t = r;\n"
4137+
"}\n");
4138+
ASSERT_EQUALS("", errout_str());
4139+
41244140
check("void f(std::optional<int>& o) {\n"
41254141
" *o = 1;\n"
41264142
"}\n");

0 commit comments

Comments
 (0)