Skip to content

Commit ccfbc0c

Browse files
Partial fix for #12697 FN ignoredReturnValue with "pure" and "const" function attributes (#6380)
1 parent 4a6eef8 commit ccfbc0c

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/checkfunctions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ void CheckFunctions::checkIgnoredReturnValue()
278278
if ((!tok->function() || !Token::Match(tok->function()->retDef, "void %name%")) &&
279279
tok->next()->astOperand1()) {
280280
const Library::UseRetValType retvalTy = mSettings->library.getUseRetValType(tok);
281-
const bool warn = (tok->function() && tok->function()->isAttributeNodiscard()) || // avoid duplicate warnings for resource-allocating functions
281+
const bool warn = (tok->function() && (tok->function()->isAttributeNodiscard() || tok->function()->isAttributePure() || tok->function()->isAttributeConst())) ||
282+
// avoid duplicate warnings for resource-allocating functions
282283
(retvalTy == Library::UseRetValType::DEFAULT && mSettings->library.getAllocFuncInfo(tok) == nullptr);
283284
if (mSettings->severity.isEnabled(Severity::warning) && warn)
284285
ignoredReturnValueError(tok, tok->next()->astOperand1()->expressionString());

test/testfunctions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,16 @@ class TestFunctions : public TestFixture {
14341434
" delete *v.begin();\n"
14351435
"}\n");
14361436
ASSERT_EQUALS("", errout_str());
1437+
1438+
check("int __attribute__((pure)) p_foo(int);\n" // #12697
1439+
"int __attribute__((const)) c_foo(int);\n"
1440+
"void f() {\n"
1441+
" p_foo(0);\n"
1442+
" c_foo(0);\n"
1443+
"}\n");
1444+
ASSERT_EQUALS("[test.cpp:4]: (warning) Return value of function p_foo() is not used.\n"
1445+
"[test.cpp:5]: (warning) Return value of function c_foo() is not used.\n",
1446+
errout_str());
14371447
}
14381448

14391449
void checkIgnoredErrorCode() {

0 commit comments

Comments
 (0)