Skip to content

Commit

Permalink
Partial fix for #12697 FN ignoredReturnValue with "pure" and "const" …
Browse files Browse the repository at this point in the history
…function attributes (#6380)
  • Loading branch information
chrchr-github authored May 22, 2024
1 parent 4a6eef8 commit ccfbc0c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/checkfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ void CheckFunctions::checkIgnoredReturnValue()
if ((!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) &&
tok->next()->astOperand1()) {
const Library::UseRetValType retvalTy = mSettings->library.getUseRetValType(tok);
const bool warn = (tok->function() && tok->function()->isAttributeNodiscard()) || // avoid duplicate warnings for resource-allocating functions
const bool warn = (tok->function() && (tok->function()->isAttributeNodiscard() || tok->function()->isAttributePure() || tok->function()->isAttributeConst())) ||
// avoid duplicate warnings for resource-allocating functions
(retvalTy == Library::UseRetValType::DEFAULT && mSettings->library.getAllocFuncInfo(tok) == nullptr);
if (mSettings->severity.isEnabled(Severity::warning) && warn)
ignoredReturnValueError(tok, tok->next()->astOperand1()->expressionString());
Expand Down
10 changes: 10 additions & 0 deletions test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,16 @@ class TestFunctions : public TestFixture {
" delete *v.begin();\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("int __attribute__((pure)) p_foo(int);\n" // #12697
"int __attribute__((const)) c_foo(int);\n"
"void f() {\n"
" p_foo(0);\n"
" c_foo(0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Return value of function p_foo() is not used.\n"
"[test.cpp:5]: (warning) Return value of function c_foo() is not used.\n",
errout_str());
}

void checkIgnoredErrorCode() {
Expand Down

0 comments on commit ccfbc0c

Please sign in to comment.