Skip to content
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
10 changes: 9 additions & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,15 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow
if (Token::simpleMatch(tok->astOperand1(), "typeid"))
return false;
if (tok->valueType()) {
return tok->valueType()->reference == Reference::None;
if (tok->valueType()->pointer > 0) {
const Token* const parent = tok->astParent();
if (Token::simpleMatch(parent, "&"))
return true;
if (Token::simpleMatch(parent, "return") && parent->valueType()->reference != Reference::None &&
parent->valueType()->container && parent->valueType()->container->stdStringLike)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to treat any pointer as a temporary value.
This line is for const std::string& f() { return "abc"; }

return true;
}
return tok->valueType()->reference == Reference::None && tok->valueType()->pointer == 0;
}
const Token* ftok = nullptr;
if (Token::simpleMatch(tok->previous(), ">") && tok->previous()->link())
Expand Down
7 changes: 2 additions & 5 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2031,17 +2031,14 @@ class TestStl : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout.str());

check("struct S {\n"
check("struct S {\n" // #11469
" const std::vector<int>* vec() const { return &v; }\n"
" const std::vector<int> v;\n"
"};\n"
"void f(const S& a, const S& b) {\n"
" if (a.vec()->begin() - a.vec()->end() != b.vec()->begin() - b.vec()->end()) {}\n"
"}\n");
TODO_ASSERT_EQUALS("",
"[test.cpp:6]: (warning) Iterators to containers from different expressions 'a.vec()' and 'a.vec()' are used together.\n"
"[test.cpp:6]: (warning) Iterators to containers from different expressions 'b.vec()' and 'b.vec()' are used together.\n",
errout.str());
ASSERT_EQUALS("", errout.str());
}

void iteratorSameExpression() {
Expand Down