Skip to content

Commit

Permalink
Partial fix for #12529 FN unreadVariable/unusedVariable with library …
Browse files Browse the repository at this point in the history
…type (#6170)
  • Loading branch information
chrchr-github committed Mar 21, 2024
1 parent 38f6651 commit 69037c9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
5 changes: 4 additions & 1 deletion lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,9 @@ void CheckUnusedVar::checkFunctionVariableUsage()
if (!isAssignment && !isInitialization && !isIncrementOrDecrement)
continue;

bool isTrivialInit = false;
if (isInitialization && Token::Match(tok, "%var% { }")) // don't warn for trivial initialization
continue;
isTrivialInit = true;

if (isIncrementOrDecrement && tok->astParent() && precedes(tok, tok->astOperand1()))
continue;
Expand Down Expand Up @@ -1308,6 +1309,8 @@ void CheckUnusedVar::checkFunctionVariableUsage()
reportLibraryCfgError(tok, bailoutTypeName);
continue;
}
if (isTrivialInit && findExpressionChanged(expr, start, scopeEnd, mSettings))
continue;

// warn
if (!expr->variable() || !expr->variable()->isMaybeUnused())
Expand Down
14 changes: 7 additions & 7 deletions test/cfg/wxwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void unreadVariable_wxTimeSpan(const long x, const wxLongLong y)
{
// cppcheck-suppress unusedVariable
wxTimeSpan a;
// TODO cppcheck-suppress unreadVariable
// cppcheck-suppress unreadVariable
wxTimeSpan b{};
// cppcheck-suppress unreadVariable
wxTimeSpan c{x};
Expand All @@ -109,7 +109,7 @@ void unreadVariable_wxPosition(const int x)
{
// cppcheck-suppress unusedVariable
wxPosition a;
// TODO cppcheck-suppress unreadVariable
// cppcheck-suppress unreadVariable
wxPosition b{};
// cppcheck-suppress unreadVariable
wxPosition c{x,x};
Expand All @@ -129,7 +129,7 @@ void unreadVariable_wxRegion(const wxCoord x, const wxPoint &pt, const wxRect &r
{
// cppcheck-suppress unusedVariable
wxRegion a;
// TODO cppcheck-suppress unreadVariable
// cppcheck-suppress unreadVariable
wxRegion b{};
// cppcheck-suppress unreadVariable
wxRegion c{x,x,x,x};
Expand Down Expand Up @@ -165,7 +165,7 @@ void unreadVariable_wxSize(const wxSize &s)
{
// cppcheck-suppress unusedVariable
wxSize a;
// TODO cppcheck-suppress unreadVariable
// cppcheck-suppress unreadVariable
wxSize b{};
// cppcheck-suppress unreadVariable
wxSize c{4, 2};
Expand All @@ -179,7 +179,7 @@ void unreadVariable_wxPoint(const wxRealPoint &rp, const int x, const int y)
{
// cppcheck-suppress unusedVariable
wxPoint a;
// TODO cppcheck-suppress unreadVariable
// cppcheck-suppress unreadVariable
wxPoint b{};
// cppcheck-suppress unreadVariable
wxPoint c{4, 2};
Expand All @@ -197,7 +197,7 @@ void unreadVariable_wxRealPoint(const wxPoint &pt, const double x, const double
{
// cppcheck-suppress unusedVariable
wxRealPoint a;
// TODO cppcheck-suppress unreadVariable
// cppcheck-suppress unreadVariable
wxRealPoint b{};
// cppcheck-suppress unreadVariable
wxRealPoint c{4.0, 2.0};
Expand All @@ -215,7 +215,7 @@ void unreadVariable_wxRect(const int x, const wxPoint &pt, const wxSize &sz)
{
// cppcheck-suppress unusedVariable
wxRect a;
// TODO cppcheck-suppress unreadVariable
// cppcheck-suppress unreadVariable
wxRect b{};
// cppcheck-suppress unreadVariable
wxRect c{x,x,x,x};
Expand Down
4 changes: 2 additions & 2 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3778,7 +3778,7 @@ class TestUnusedVar : public TestFixture {
functionVariableUsage("void f() {\n"
" S* s{};\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", "", errout_str());
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout_str());

functionVariableUsage("int f() {\n"
" int i = 0, j = 1;\n"
Expand Down Expand Up @@ -5137,7 +5137,7 @@ class TestUnusedVar : public TestFixture {
" b = false;\n"
" if (*p) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
ASSERT_EQUALS("[test.cpp:4]: (style) Variable 'j' is assigned a value that is never used.\n", errout_str());
}

void localvaralias22() { // #11139
Expand Down

0 comments on commit 69037c9

Please sign in to comment.