Fix #7732 false negative: (style) Unused variable: std::pair (#3695)

This commit is contained in:
chrchr-github 2022-01-13 08:05:05 +01:00 committed by GitHub
parent af0a585a70
commit 95dc05b21d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 3 deletions

View File

@ -5104,6 +5104,12 @@
<function name="crend" yields="end-iterator"/> <function name="crend" yields="end-iterator"/>
</access> </access>
</container> </container>
<type-checks>
<unusedvar>
<suppress>QApplication</suppress>
<suppress>QMutexLocker</suppress>
</unusedvar>
</type-checks>
<!-- Treat QStringList as QList<QString> since we can't remove the template parameter when we inherit. --> <!-- Treat QStringList as QList<QString> since we can't remove the template parameter when we inherit. -->
<define name="QStringList" value="QList&lt;QString&gt;"/> <define name="QStringList" value="QList&lt;QString&gt;"/>
<define name="Q_ARG(type, data)" value="QArgument&lt;type &gt;(#type, data)"/> <define name="Q_ARG(type, data)" value="QArgument&lt;type &gt;(#type, data)"/>

View File

@ -8342,6 +8342,12 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init
<suppress>std::lock_guard</suppress> <suppress>std::lock_guard</suppress>
<suppress>std::unique_lock</suppress> <suppress>std::unique_lock</suppress>
<suppress>std::shared_lock</suppress> <suppress>std::shared_lock</suppress>
<suppress>std::fstream</suppress>
<suppress>std::wfstream</suppress>
<suppress>std::ofstream</suppress>
<suppress>std::wofstream</suppress>
<suppress>std::basic_fstream</suppress>
<suppress>std::basic_ofstream</suppress>
<check>std::pair</check> <check>std::pair</check>
</unusedvar> </unusedvar>
</type-checks> </type-checks>

View File

@ -1192,7 +1192,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
continue; continue;
if (tok->isName()) { if (tok->isName()) {
if (isRaiiClass(tok->valueType(), mTokenizer->isCPP(), true)) if (isRaiiClass(tok->valueType(), mTokenizer->isCPP(), false))
continue; continue;
tok = tok->next(); tok = tok->next();
} }
@ -1223,6 +1223,8 @@ void CheckUnusedVar::checkFunctionVariableUsage()
op1tok = op1tok->astOperand1(); op1tok = op1tok->astOperand1();
const Variable *op1Var = op1tok ? op1tok->variable() : nullptr; const Variable *op1Var = op1tok ? op1tok->variable() : nullptr;
if (!op1Var && Token::Match(tok, "(|{") && tok->previous() && tok->previous()->variable())
op1Var = tok->previous()->variable();
std::string bailoutTypeName; std::string bailoutTypeName;
if (op1Var) { if (op1Var) {
if (op1Var->isReference() && op1Var->nameToken() != tok->astOperand1()) if (op1Var->isReference() && op1Var->nameToken() != tok->astOperand1())
@ -1278,8 +1280,8 @@ void CheckUnusedVar::checkFunctionVariableUsage()
Severity::information, Severity::information,
"checkLibraryCheckType", "checkLibraryCheckType",
"--check-library: Provide <type-checks><unusedvar> configuration for " + bailoutTypeName); "--check-library: Provide <type-checks><unusedvar> configuration for " + bailoutTypeName);
continue;
} }
continue;
} }
// warn // warn

View File

@ -5431,6 +5431,17 @@ private:
" std::unique_lock<std::mutex> lock(m);\n" // #4624 " std::unique_lock<std::mutex> lock(m);\n" // #4624
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
functionVariableUsage("void f() {\n" // #7732
" const std::pair<std::string, std::string> p(\"a\", \"b\");\n"
" std::pair<std::string, std::string> q{\"a\", \"b\" };\n"
" auto r = std::pair<std::string, std::string>(\"a\", \"b\");\n"
" auto s = std::pair<std::string, std::string>{ \"a\", \"b\" };\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' is assigned a value that is never used.\n"
"[test.cpp:3]: (style) Variable 'q' is assigned a value that is never used.\n"
"[test.cpp:4]: (style) Variable 'r' is assigned a value that is never used.\n"
"[test.cpp:5]: (style) Variable 's' is assigned a value that is never used.\n", errout.str());
} }
void localVarClass() { void localVarClass() {
@ -5610,7 +5621,7 @@ private:
" int buf[6];\n" " int buf[6];\n"
" Data data(buf);\n" " Data data(buf);\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (information) --check-library: Provide <type-checks><unusedvar> configuration for Data\n", errout.str());
} }
void localvarCpp11Initialization() { void localvarCpp11Initialization() {