Skip to content

Commit eed1daf

Browse files
Fix #9761 FP constParameter on parameter assigned to boost::optional<T&> (#8548)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 7c7ed58 commit eed1daf

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

lib/astutils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,8 @@ static const Token* findExpressionChangedImpl(const Token* expr,
31143114
if (vt->type == ValueType::ITERATOR)
31153115
++indirect;
31163116
}
3117+
if (indirect == 0 && tok2->astParent() && tok2->astParent()->isUnaryOp("*"))
3118+
++indirect;
31173119
for (int i = 0; i <= indirect; ++i) {
31183120
if (isExpressionChangedAt(tok, tok2, i, global, settings, depth))
31193121
return true;

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,6 +4120,27 @@ class TestOther : public TestFixture {
41204120
" std::string _s;\n"
41214121
"};\n");
41224122
ASSERT_EQUALS("", errout_str());
4123+
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+
4140+
check("void f(std::optional<int>& o) {\n"
4141+
" *o = 1;\n"
4142+
"}\n");
4143+
ASSERT_EQUALS("", errout_str());
41234144
}
41244145

41254146
void constParameterCallback() {

0 commit comments

Comments
 (0)