]> granicus.if.org Git - php/commitdiff
Improve switch continue warning
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 25 Jan 2021 15:17:14 +0000 (16:17 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 25 Jan 2021 15:17:50 +0000 (16:17 +0100)
Don't suggest "continue N+1" if there is no wrapping loop. The
resulting code would be illegal.

Zend/tests/continue_targeting_switch_warning.phpt
Zend/zend_compile.c

index 05a51e5a22963cfd7e8cd66855bc5a2d88d992a6..379108ad1f8e594121cad2cc74216b137b6e9f3a 100644 (file)
@@ -22,6 +22,21 @@ function test() {
         }
     }
 
+    switch ($bar) {
+        case 0:
+            while ($xyz) {
+                continue 2; // INVALID
+            }
+        case 1:
+            while ($xyz) {
+                continue;
+            }
+        case 2:
+            while ($xyz) {
+                break 2;
+            }
+    }
+
     while ($foo) {
         switch ($bar) {
             case 0:
@@ -42,8 +57,10 @@ function test() {
 
 ?>
 --EXPECTF--
-Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in %s on line 6
+Warning: "continue" targeting switch is equivalent to "break" in %s on line 6
 
 Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in %s on line 14
 
-Warning: "continue 2" targeting switch is equivalent to "break 2". Did you mean to use "continue 3"? in %s on line 26
+Warning: "continue 2" targeting switch is equivalent to "break 2" in %s on line 25
+
+Warning: "continue 2" targeting switch is equivalent to "break 2". Did you mean to use "continue 3"? in %s on line 41
index e7f2b0b04a01620165270b72a2b8bd2db24974e0..57c33e3c96c1864f0a29b9b7aaa9d18a40e7876d 100644 (file)
@@ -4891,15 +4891,26 @@ void zend_compile_break_continue(zend_ast *ast) /* {{{ */
 
                if (CG(context).brk_cont_array[cur].is_switch) {
                        if (depth == 1) {
-                               zend_error(E_WARNING,
-                                       "\"continue\" targeting switch is equivalent to \"break\". " \
-                                       "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
-                                       depth + 1);
+                               if (CG(context).brk_cont_array[cur].parent == -1) {
+                                       zend_error(E_WARNING,
+                                               "\"continue\" targeting switch is equivalent to \"break\"");
+                               } else {
+                                       zend_error(E_WARNING,
+                                               "\"continue\" targeting switch is equivalent to \"break\". " \
+                                               "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
+                                               depth + 1);
+                               }
                        } else {
-                               zend_error(E_WARNING,
-                                       "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
-                                       "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
-                                       depth, depth, depth + 1);
+                               if (CG(context).brk_cont_array[cur].parent == -1) {
+                                       zend_error(E_WARNING,
+                                               "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\"",
+                                               depth, depth);
+                               } else {
+                                       zend_error(E_WARNING,
+                                               "\"continue " ZEND_LONG_FMT "\" targeting switch is equivalent to \"break " ZEND_LONG_FMT "\". " \
+                                               "Did you mean to use \"continue " ZEND_LONG_FMT "\"?",
+                                               depth, depth, depth + 1);
+                               }
                        }
                }
        }