File tree Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Expand file tree Collapse file tree 2 files changed +41
-2
lines changed Original file line number Diff line number Diff line change @@ -871,8 +871,21 @@ static bool isVardeclInSwitch(const Token* tok)
871
871
return false ;
872
872
if (!isNestedInSwitch (tok->scope ()))
873
873
return false ;
874
- const Token* end = Token::findsimplematch (tok, " ;" );
875
- return end && end->previous ()->variable () && end->previous ()->variable ()->nameToken () == end->previous ();
874
+ if (const Token* end = Token::findsimplematch (tok, " ;" )) {
875
+ for (const Token* tok2 = tok; tok2 != end; tok2 = tok2->next ()) {
876
+ if (tok2->isKeyword () && tok2->str () == " case" )
877
+ return false ;
878
+ if (tok2->variable () && tok2->variable ()->nameToken () == tok2) {
879
+ end = tok2->scope ()->bodyEnd ;
880
+ for (const Token* tok3 = tok2; tok3 != end; tok3 = tok3->next ()) {
881
+ if (tok3->isKeyword ())
882
+ return tok3->str () == " case" ;
883
+ }
884
+ return false ;
885
+ }
886
+ }
887
+ }
888
+ return false ;
876
889
}
877
890
878
891
// ---------------------------------------------------------------------------
Original file line number Diff line number Diff line change @@ -5710,6 +5710,20 @@ class TestOther : public TestFixture {
5710
5710
" }\n " );
5711
5711
ASSERT_EQUALS (" " , errout_str ());
5712
5712
5713
+ check (" int f(int i) {\n "
5714
+ " switch (i) {\n "
5715
+ " case 0:\n "
5716
+ " return 0;\n "
5717
+ " int a[1];\n "
5718
+ " case 1:\n "
5719
+ " case 2:\n "
5720
+ " a[0] = 5;\n "
5721
+ " return a[0] + i;\n "
5722
+ " }\n "
5723
+ " return 3;\n "
5724
+ " }\n " );
5725
+ ASSERT_EQUALS (" " , errout_str ());
5726
+
5713
5727
check (" int f(int i) {\n "
5714
5728
" switch (i) {\n "
5715
5729
" case 0:\n "
@@ -5780,6 +5794,18 @@ class TestOther : public TestFixture {
5780
5794
" return x;\n "
5781
5795
" }\n " );
5782
5796
ASSERT_EQUALS (" " , errout_str ());
5797
+
5798
+ check (" bool f(int x, int y) {\n " // #13544
5799
+ " switch (x) {\n "
5800
+ " case 1: {\n "
5801
+ " return y != 0;\n "
5802
+ " int z = y + 5;\n "
5803
+ " return z != 7;\n "
5804
+ " }\n "
5805
+ " }\n "
5806
+ " return false;\n "
5807
+ " }\n " );
5808
+ ASSERT_EQUALS (" [test.cpp:5]: (style) Statements following 'return' will never be executed.\n " , errout_str ());
5783
5809
}
5784
5810
5785
5811
You can’t perform that action at this time.
0 commit comments