Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12740 FP redundantCopyLocalConst with pointer member #6410

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2878,7 +2878,9 @@ void CheckOther::checkRedundantCopy()

const Token* dot = tok->astOperand1();
if (Token::simpleMatch(dot, ".")) {
if (dot->astOperand1() && isVariableChanged(dot->astOperand1()->variable(), *mSettings))
const Token* varTok = dot->astOperand1();
const int indirect = varTok->valueType() ? varTok->valueType()->pointer : 0;
if (isVariableChanged(tok, tok->scope()->bodyEnd, varTok->varId(), indirect, /*globalvar*/ false, *mSettings))
continue;
if (isTemporary(dot, &mSettings->library, /*unknown*/ true))
continue;
Expand Down
39 changes: 39 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8969,6 +8969,45 @@ class TestOther : public TestFixture {
" if (a.x > a.y) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S {\n" // #12740
" const std::string & get() const { return m; }\n"
" void set(const std::string& v) { m = v; }\n"
" std::string m;\n"
"};\n"
"struct T {\n"
" void f();\n"
" S* s;\n"
"};\n"
"void T::f() {\n"
" const std::string o = s->get();\n"
" s->set(\"abc\");\n"
" s->set(o);\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S {\n" // #12196
" std::string s;\n"
" const std::string& get() const { return s; }\n"
"};\n"
"struct T {\n"
" S* m;\n"
" void f();\n"
"};\n"
"struct U {\n"
" explicit U(S* p);\n"
" void g();\n"
" S* n;\n"
"};\n"
"void T::f() {\n"
" U u(m);\n"
" const std::string c = m->get();\n"
" u.g();\n"
" if (c == m->get()) {}\n"
"}\n");
TODO_ASSERT_EQUALS("",
"[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",
errout_str());
}

void checkNegativeShift() {
Expand Down
Loading