Skip to content

Commit

Permalink
Fix #11469 FP mismatchingContainerExpression warning (#5912)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Jan 30, 2024
1 parent 485df98 commit 06c8363
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
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)
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

0 comments on commit 06c8363

Please sign in to comment.