Improved check (#5553): Detect stricmp(var.c_str(), var.c_str())
This commit is contained in:
parent
5f67bc1b0a
commit
fab6b56360
|
@ -2915,37 +2915,37 @@ void CheckOther::checkAlwaysTrueOrFalseStringCompare()
|
||||||
if (!_settings->isEnabled("warning"))
|
if (!_settings->isEnabled("warning"))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const Token *tok = _tokenizer->tokens();
|
for (const Token* tok = _tokenizer->tokens(); tok; tok = tok->next()) {
|
||||||
while (tok && (tok = Token::findmatch(tok, "strncmp|strcmp|stricmp|strcmpi|strcasecmp|wcscmp|wcsncmp ( %str% , %str% ")) != nullptr) {
|
if (Token::Match(tok, "strncmp|strcmp|stricmp|strcmpi|strcasecmp|wcscmp|wcsncmp (")) {
|
||||||
const std::string &str1 = tok->strAt(2);
|
if (Token::Match(tok->tokAt(2), "%str% , %str% ")) {
|
||||||
const std::string &str2 = tok->strAt(4);
|
const std::string &str1 = tok->strAt(2);
|
||||||
alwaysTrueFalseStringCompareError(tok, str1, str2);
|
const std::string &str2 = tok->strAt(4);
|
||||||
tok = tok->tokAt(5);
|
alwaysTrueFalseStringCompareError(tok, str1, str2);
|
||||||
}
|
tok = tok->tokAt(5);
|
||||||
|
} else if (Token::Match(tok->tokAt(2), "%var% , %var% ")) {
|
||||||
tok = _tokenizer->tokens();
|
const std::string &str1 = tok->strAt(2);
|
||||||
while (tok && (tok = Token::findmatch(tok, "QString :: compare ( %str% , %str% )")) != nullptr) {
|
const std::string &str2 = tok->strAt(4);
|
||||||
const std::string &str1 = tok->strAt(4);
|
if (str1 == str2)
|
||||||
const std::string &str2 = tok->strAt(6);
|
alwaysTrueStringVariableCompareError(tok, str1, str2);
|
||||||
alwaysTrueFalseStringCompareError(tok, str1, str2);
|
tok = tok->tokAt(5);
|
||||||
tok = tok->tokAt(7);
|
} else if (Token::Match(tok->tokAt(2), "%var% . c_str ( ) , %var% . c_str ( ) ")) {
|
||||||
}
|
const std::string &str1 = tok->strAt(2);
|
||||||
|
const std::string &str2 = tok->strAt(8);
|
||||||
tok = _tokenizer->tokens();
|
if (str1 == str2)
|
||||||
while (tok && (tok = Token::findmatch(tok, "strncmp|strcmp|stricmp|strcmpi|strcasecmp|wcscmp|wcsncmp ( %var% , %var% ")) != nullptr) {
|
alwaysTrueStringVariableCompareError(tok, str1, str2);
|
||||||
const std::string &str1 = tok->strAt(2);
|
tok = tok->tokAt(13);
|
||||||
const std::string &str2 = tok->strAt(4);
|
}
|
||||||
if (str1 == str2)
|
} else if (Token::Match(tok, "QString :: compare ( %str% , %str% )")) {
|
||||||
alwaysTrueStringVariableCompareError(tok, str1, str2);
|
const std::string &str1 = tok->strAt(4);
|
||||||
tok = tok->tokAt(5);
|
const std::string &str2 = tok->strAt(6);
|
||||||
}
|
alwaysTrueFalseStringCompareError(tok, str1, str2);
|
||||||
|
tok = tok->tokAt(7);
|
||||||
tok = _tokenizer->tokens();
|
} else if (Token::Match(tok, "!!+ %str% ==|!= %str% !!+")) {
|
||||||
while (tok && (tok = Token::findmatch(tok, "!!+ %str% ==|!= %str% !!+")) != nullptr) {
|
const std::string &str1 = tok->strAt(1);
|
||||||
const std::string &str1 = tok->strAt(1);
|
const std::string &str2 = tok->strAt(3);
|
||||||
const std::string &str2 = tok->strAt(3);
|
alwaysTrueFalseStringCompareError(tok, str1, str2);
|
||||||
alwaysTrueFalseStringCompareError(tok, str1, str2);
|
tok = tok->tokAt(5);
|
||||||
tok = tok->tokAt(5);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4982,6 +4982,16 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout.str());
|
||||||
|
|
||||||
|
check(
|
||||||
|
"int foo(const std::string& buf)\n"
|
||||||
|
"{\n"
|
||||||
|
" if (stricmp(buf.c_str(), buf.c_str()) == 0)"
|
||||||
|
" {"
|
||||||
|
" std::cout << \"Equal\n\""
|
||||||
|
" }"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:3]: (warning) Comparison of identical string variables.\n", errout.str());
|
||||||
|
|
||||||
check_preprocess_suppress(
|
check_preprocess_suppress(
|
||||||
"int main() {\n"
|
"int main() {\n"
|
||||||
" if (\"str\" == \"str\") {\n"
|
" if (\"str\" == \"str\") {\n"
|
||||||
|
|
Loading…
Reference in New Issue