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
2 changes: 1 addition & 1 deletion lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ void CheckOther::checkVariableScope()
tok = tok->link();

// parse else if blocks..
} else if (Token::simpleMatch(tok, "else { if (") && Token::simpleMatch(tok->linkAt(3), ") {")) {
} else if (Token::simpleMatch(tok, "else { if (") && tok->next()->isSimplifiedScope() && Token::simpleMatch(tok->linkAt(3), ") {")) {
tok = tok->next();
} else if (tok->varId() == var->declarationId() || tok->str() == "goto") {
reduce = false;
Expand Down
4 changes: 2 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7964,8 +7964,8 @@ void Tokenizer::elseif()

if (Token::Match(tok2, "}|;")) {
if (tok2->next() && tok2->strAt(1) != "else") {
tok->insertToken("{");
tok2->insertToken("}");
tok->insertToken("{")->isSimplifiedScope(true);
tok2->insertToken("}")->isSimplifiedScope(true);
Token::createMutualLinks(tok->next(), tok2->next());
break;
}
Expand Down
12 changes: 12 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class TestOther : public TestFixture {
TEST_CASE(varScope42);
TEST_CASE(varScope43);
TEST_CASE(varScope44);
TEST_CASE(varScope45);

TEST_CASE(oldStylePointerCast);
TEST_CASE(intToPointerCast);
Expand Down Expand Up @@ -1972,6 +1973,17 @@ class TestOther : public TestFixture {
errout_str());
}

void varScope45() {
check("void g(int x, int y) {\n" // #14497
" int a = x, b = y;\n"
" if (a) {}\n"
" else {\n"
" if (b) {}\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:16]: (style) The scope of the variable 'b' can be reduced. [variableScope]\n", errout_str());
}

#define checkOldStylePointerCast(...) checkOldStylePointerCast_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
void checkOldStylePointerCast_(const char* file, int line, const char (&code)[size], Standards::cppstd_t std = Standards::CPPLatest) {
Expand Down
Loading