Skip to content

Commit 269c636

Browse files
Fix #13877 FP constParameterReference when casting to derived class (#8041)
1 parent fe537a7 commit 269c636

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/astutils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,6 +2943,9 @@ static bool isExpressionChangedAt(const F& getExprTok,
29432943
auto expr = getExprTok();
29442944
if (!expr && !(tok->valueType() && tok->valueType()->pointer == 0 && tok->valueType()->reference == Reference::None))
29452945
aliased = true;
2946+
if (!aliased && expr && expr->varId() && tok->isCast() && tok->valueType() && tok->valueType()->reference != Reference::None &&
2947+
Token::Match(tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(), "%varid%", expr->varId()))
2948+
aliased = true;
29462949
if (!aliased)
29472950
aliased = isAliasOf(tok, expr, &i);
29482951
if (!aliased)

test/testother.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3982,6 +3982,19 @@ class TestOther : public TestFixture {
39823982
" t.s->i = 0;\n"
39833983
"}\n");
39843984
ASSERT_EQUALS("", errout_str());
3985+
3986+
check("struct B {};\n" // #13877
3987+
"struct D : B { int i; };\n"
3988+
"void f(B& b) {\n"
3989+
" static_cast<D&>(b).i = 0;\n"
3990+
"}\n"
3991+
"void g(B& b) {\n"
3992+
" std::cin >> static_cast<D&>(b).i;\n"
3993+
"}\n"
3994+
"int h(B& b) {\n"
3995+
" return static_cast<const D&>(b).i;\n"
3996+
"}\n");
3997+
ASSERT_EQUALS("[test.cpp:9:10]: (style) Parameter 'b' can be declared as reference to const [constParameterReference]\n", errout_str());
39853998
}
39863999

39874000
void constParameterCallback() {

0 commit comments

Comments
 (0)