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
1 change: 0 additions & 1 deletion gui/projectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,6 @@ bool ProjectFile::write(const QString &filename)
for (const QString &tag: tags) {
xmlWriter.writeStartElement(CppcheckXml::TagWarningsElementName);
xmlWriter.writeAttribute(CppcheckXml::TagAttributeName, tag);
QStringList warnings;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Cool!

for (const auto& wt: mWarningTags) {
if (wt.second == tag) {
xmlWriter.writeStartElement(CppcheckXml::WarningElementName);
Expand Down
6 changes: 4 additions & 2 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
else if (mTokenizer->isC() ||
i->typeEndToken()->isStandardType() ||
isRecordTypeWithoutSideEffects(i->type()) ||
mSettings->library.detectContainer(i->typeStartToken(), /*iterator*/ false) ||
(i->isStlType() &&
!Token::Match(i->typeStartToken()->tokAt(2), "lock_guard|unique_lock|shared_ptr|unique_ptr|auto_ptr|shared_lock")))
type = Variables::standard;
Expand All @@ -728,7 +729,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
break;
}
}
if (i->isArray() && i->isClass()) // Array of class/struct members. Initialized by ctor.
if (i->isArray() && i->isClass() && // Array of class/struct members. Initialized by ctor except for std::array
!(i->isStlType() && i->valueType() && i->valueType()->containerTypeToken && i->valueType()->containerTypeToken->isStandardType()))
variables.write(i->declarationId(), i->nameToken());
if (i->isArray() && Token::Match(i->nameToken(), "%name% [ %var% ]")) // Array index variable read.
variables.read(i->nameToken()->tokAt(2)->varId(), i->nameToken());
Expand Down Expand Up @@ -1176,7 +1178,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
}
// not assignment/initialization/increment => continue
const bool isAssignment = tok->isAssignmentOp() && tok->astOperand1();
const bool isInitialization = (Token::Match(tok, "%var% (") && tok->variable() && tok->variable()->nameToken() == tok);
const bool isInitialization = (Token::Match(tok, "%var% (|{") && tok->variable() && tok->variable()->nameToken() == tok);
const bool isIncrementOrDecrement = (tok->tokType() == Token::Type::eIncDecOp);
if (!isAssignment && !isInitialization && !isIncrementOrDecrement)
continue;
Expand Down
7 changes: 7 additions & 0 deletions test/cfg/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ QString::iterator QString3()
return it;
}

void QString4()
{
// cppcheck-suppress unusedVariable
QString qs;
}

void QByteArray1(QByteArray byteArrayArg)
{
for (int i = 0; i <= byteArrayArg.size(); ++i) {
Expand Down Expand Up @@ -136,6 +142,7 @@ QList<int>::iterator QList3()

void QLinkedList1()
{
// cppcheck-suppress unreadVariable
QLinkedList<QString> qstringLinkedList1{"one", "two"};

QLinkedList<QString> qstringLinkedList2 = {"one", "two"};
Expand Down
15 changes: 15 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5230,6 +5230,16 @@ class TestUnusedVar : public TestFixture {
" return s;\n"
"}");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("void f() {\n"
" std::string s(\"foo\");\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());

functionVariableUsage("void f() {\n"
" std::string s{ \"foo\" };\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
}

void localvarstring2() { // ticket #2929
Expand Down Expand Up @@ -5627,6 +5637,11 @@ class TestUnusedVar : public TestFixture {
" std::array<int, ArraySize> X; X.dostuff();\n"
"}");
ASSERT_EQUALS("", errout.str());

functionVariableUsage("void f() {\n" // #10686
" std::array<int, 1> a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Unused variable: a\n", errout.str());
}

void localvarFuncPtr() {
Expand Down