From 4d998216bc7b5f175c9db7e1e00501669637dca2 Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Tue, 12 Dec 2023 17:41:13 +0200 Subject: [PATCH 1/2] #12158: improve check: variableScope is not reported when there is else if --- lib/checkother.cpp | 11 +---------- test/testother.cpp | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3e324a6833d..8e23a0617d9 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -995,16 +995,7 @@ void CheckOther::checkVariableScope() // parse else if blocks.. } else if (Token::simpleMatch(tok, "else { if (") && Token::simpleMatch(tok->linkAt(3), ") {")) { - const Token *endif = tok->linkAt(3)->linkAt(1); - bool elseif = false; - if (Token::simpleMatch(endif, "} }")) - elseif = true; - else if (Token::simpleMatch(endif, "} else {") && Token::simpleMatch(endif->linkAt(2),"} }")) - elseif = true; - if (elseif && Token::findmatch(tok->next(), "%varid%", tok->linkAt(1), var->declarationId())) { - reduce = false; - break; - } + tok = tok->next(); } else if (tok->varId() == var->declarationId() || tok->str() == "goto") { reduce = false; break; diff --git a/test/testother.cpp b/test/testother.cpp index d221570159c..ad03ca0b7d6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -101,6 +101,7 @@ class TestOther : public TestFixture { TEST_CASE(varScope33); TEST_CASE(varScope34); TEST_CASE(varScope35); + TEST_CASE(varScope36); // #12158 TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1667,6 +1668,20 @@ class TestOther : public TestFixture { ASSERT_EQUALS("[test.cpp:4]: (style) The scope of the variable 'buf' can be reduced.\n", errout.str()); } + void varScope36() { + // #12158 + check("void f( uint32_t value ) {\n" + " uint32_t i = 0U;\n" + " if ( value > 100U ) { }\n" + " else if( value > 50U ) { }\n" + " else{\n" + " for( i = 0U; i < 5U; i++ ) {}" + " }" + "}", nullptr, false); + ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", errout.str()); + } + + #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) void checkOldStylePointerCast_(const char code[], const char* file, int line) { // Clear the error buffer.. From bb361c73adaf3b9285a9b19c8a9a2625afad700f Mon Sep 17 00:00:00 2001 From: Oleksandr Labetskyi Date: Wed, 13 Dec 2023 18:35:53 +0200 Subject: [PATCH 2/2] #12158: add another test and a small fix --- test/testother.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/testother.cpp b/test/testother.cpp index ad03ca0b7d6..6baa74cb926 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -102,6 +102,7 @@ class TestOther : public TestFixture { TEST_CASE(varScope34); TEST_CASE(varScope35); TEST_CASE(varScope36); // #12158 + TEST_CASE(varScope37); // #12158 TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1675,9 +1676,24 @@ class TestOther : public TestFixture { " if ( value > 100U ) { }\n" " else if( value > 50U ) { }\n" " else{\n" - " for( i = 0U; i < 5U; i++ ) {}" - " }" - "}", nullptr, false); + " for( i = 0U; i < 5U; i++ ) {}\n" + " }\n" + "}\n", nullptr, false); + ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", errout.str()); + } + + void varScope37() { + // #12158 + check("void f( uint32_t value ) {\n" + " uint32_t i = 0U;\n" + " if ( value > 100U ) { }\n" + " else {\n" + " if( value > 50U ) { }\n" + " else{\n" + " for( i = 0U; i < 5U; i++ ) {}\n" + " }\n" + " }\n" + "}\n", nullptr, false); ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'i' can be reduced.\n", errout.str()); }