Fixed #7733 (False positive: scope of the variable can be reduced (variable is used in hidden code))

This commit is contained in:
Daniel Marjamäki 2020-07-01 08:24:52 +02:00
parent f56a17bf3d
commit d2b2bae7bf
2 changed files with 27 additions and 0 deletions

View File

@ -882,6 +882,9 @@ void CheckOther::checkVariableScope()
if (var->isConst())
continue;
if (mTokenizer->hasIfdef(var->nameToken(), var->scope()->bodyEnd))
continue;
// reference of range for loop variable..
if (Token::Match(var->nameToken()->previous(), "& %var% = %var% .")) {
const Token *otherVarToken = var->nameToken()->tokAt(2);

View File

@ -19,6 +19,7 @@
#include "checkother.h"
#include "library.h"
#include "platform.h"
#include "preprocessor.h"
#include "settings.h"
#include "standards.h"
#include "testsuite.h"
@ -86,6 +87,7 @@ private:
TEST_CASE(varScope24); // pointer / reference
TEST_CASE(varScope25); // time_t
TEST_CASE(varScope26); // range for loop, map
TEST_CASE(varScope27); // #7733 - #if
TEST_CASE(oldStylePointerCast);
TEST_CASE(invalidPointerCast);
@ -300,10 +302,14 @@ private:
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Preprocessor preprocessor(*settings, nullptr);
preprocessor.setDirectives(tokens1);
// Tokenizer..
Tokenizer tokenizer(settings, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
tokenizer.setPreprocessor(&preprocessor);
// Check..
CheckOther checkOther(&tokenizer, settings, this);
@ -1229,6 +1235,24 @@ private:
ASSERT_EQUALS("", errout.str());
}
void varScope27() {
checkP("void f() {\n"
" int x = 0;\n"
"#ifdef X\n"
"#endif\n"
" if (id == ABC) { return x; }\n"
"}");
ASSERT_EQUALS("", errout.str());
checkP("void f() {\n"
"#ifdef X\n"
"#endif\n"
" int x = 0;\n"
" if (id == ABC) { return x; }\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'x' can be reduced.\n", errout.str());
}
void checkOldStylePointerCast(const char code[]) {
// Clear the error buffer..
errout.str("");