File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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");
You can’t perform that action at this time.
0 commit comments