Skip to content

Commit 7419e53

Browse files
Fix #12740 FP redundantCopyLocalConst with pointer member (#6410)
1 parent 9fb2625 commit 7419e53

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/checkother.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2901,7 +2901,9 @@ void CheckOther::checkRedundantCopy()
29012901

29022902
const Token* dot = tok->astOperand1();
29032903
if (Token::simpleMatch(dot, ".")) {
2904-
if (dot->astOperand1() && isVariableChanged(dot->astOperand1()->variable(), *mSettings))
2904+
const Token* varTok = dot->astOperand1();
2905+
const int indirect = varTok->valueType() ? varTok->valueType()->pointer : 0;
2906+
if (isVariableChanged(tok, tok->scope()->bodyEnd, varTok->varId(), indirect, /*globalvar*/ false, *mSettings))
29052907
continue;
29062908
if (isTemporary(dot, &mSettings->library, /*unknown*/ true))
29072909
continue;

test/testother.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8975,6 +8975,45 @@ class TestOther : public TestFixture {
89758975
" if (a.x > a.y) {}\n"
89768976
"}\n");
89778977
ASSERT_EQUALS("", errout_str());
8978+
8979+
check("struct S {\n" // #12740
8980+
" const std::string & get() const { return m; }\n"
8981+
" void set(const std::string& v) { m = v; }\n"
8982+
" std::string m;\n"
8983+
"};\n"
8984+
"struct T {\n"
8985+
" void f();\n"
8986+
" S* s;\n"
8987+
"};\n"
8988+
"void T::f() {\n"
8989+
" const std::string o = s->get();\n"
8990+
" s->set(\"abc\");\n"
8991+
" s->set(o);\n"
8992+
"}\n");
8993+
ASSERT_EQUALS("", errout_str());
8994+
8995+
check("struct S {\n" // #12196
8996+
" std::string s;\n"
8997+
" const std::string& get() const { return s; }\n"
8998+
"};\n"
8999+
"struct T {\n"
9000+
" S* m;\n"
9001+
" void f();\n"
9002+
"};\n"
9003+
"struct U {\n"
9004+
" explicit U(S* p);\n"
9005+
" void g();\n"
9006+
" S* n;\n"
9007+
"};\n"
9008+
"void T::f() {\n"
9009+
" U u(m);\n"
9010+
" const std::string c = m->get();\n"
9011+
" u.g();\n"
9012+
" if (c == m->get()) {}\n"
9013+
"}\n");
9014+
TODO_ASSERT_EQUALS("",
9015+
"[test.cpp:16] -> [test.cpp:18]: (style) The comparison 'c == m->get()' is always true because 'c' and 'm->get()' represent the same value.\n",
9016+
errout_str());
89789017
}
89799018

89809019
void checkNegativeShift() {

0 commit comments

Comments
 (0)