Skip to content

Commit 7100bda

Browse files
Fix #14251 FP constParameterReference for struct with optional struct member (#7948)
1 parent 7e09fa0 commit 7100bda

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/astutils.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,14 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti
26172617
return false;
26182618
}
26192619

2620+
static bool hasOverloadedMemberAccess(const Token* tok)
2621+
{
2622+
if (!Token::simpleMatch(tok, "."))
2623+
return false;
2624+
const Token* varTok = tok->astOperand2();
2625+
return !varTok || !varTok->variable() || !varTok->variable()->valueType() || varTok->variable()->valueType()->pointer == 0;
2626+
}
2627+
26202628
bool isVariableChanged(const Token *tok, int indirect, const Settings &settings, int depth)
26212629
{
26222630
if (!isMutableExpression(tok))
@@ -2631,7 +2639,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
26312639
(Token::simpleMatch(tok2->astParent(), ".") && !Token::Match(tok2->astParent()->astParent(), "[(,]")) ||
26322640
(tok2->astParent() && tok2->astParent()->isUnaryOp("&") && Token::simpleMatch(tok2->astParent()->astParent(), ".") && tok2->astParent()->astParent()->originalName()=="->") ||
26332641
(Token::simpleMatch(tok2->astParent(), "[") && tok2 == tok2->astParent()->astOperand1())) {
2634-
if (tok2->astParent() && (tok2->astParent()->isUnaryOp("*") || (astIsLHS(tok2) && tok2->astParent()->originalName() == "->")))
2642+
if (tok2->astParent() && (tok2->astParent()->isUnaryOp("*") || (astIsLHS(tok2) && tok2->astParent()->originalName() == "->" && !hasOverloadedMemberAccess(tok2))))
26352643
derefs++;
26362644
if (derefs > indirect)
26372645
break;

test/testother.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,6 +3974,13 @@ class TestOther : public TestFixture {
39743974
" return (void*)&s;\n"
39753975
"}\n"); // don't crash
39763976
ASSERT_EQUALS("", errout_str());
3977+
3978+
check("struct S { int i; };\n" // #14251
3979+
"struct T { std::optional<S> s; };\n"
3980+
"void f(T& t) {\n"
3981+
" t.s->i = 0;\n"
3982+
"}\n");
3983+
ASSERT_EQUALS("", errout_str());
39773984
}
39783985

39793986
void constParameterCallback() {

0 commit comments

Comments
 (0)