]> git.donarmstrong.com Git - perltidy.git/commitdiff
improve error check for unexpected 'elsif' and 'else' (c272)
authorSteve Hancock <perltidy@users.sourceforge.net>
Tue, 3 Oct 2023 14:30:51 +0000 (07:30 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Tue, 3 Oct 2023 14:30:51 +0000 (07:30 -0700)
lib/Perl/Tidy/Tokenizer.pm
t/snippets/expect/lop.def
t/snippets/expect/lop.lop
t/snippets/lop.in
t/snippets/packing_list.txt
t/snippets20.t
t/snippets21.t

index 364a0e9cd159f7d6e4e30b334bd24929884135a6..94b0a97ce31329f0f98f066ac050e4aae8cb2061 100644 (file)
@@ -41,6 +41,9 @@ use constant DEVEL_MODE   => 0;
 use constant EMPTY_STRING => q{};
 use constant SPACE        => q{ };
 
+# Parent sequence number of tree of containers; must be 1
+use constant SEQ_ROOT => 1;
+
 # Decimal values of some ascii characters for quick checks
 use constant ORD_TAB           => 9;
 use constant ORD_SPACE         => 32;
@@ -1715,7 +1718,7 @@ sub prepare_for_a_new_file {
     $total_depth              = 0;
     $rtotal_depth             = [];
     $rcurrent_sequence_number = [];
-    $next_sequence_number     = 2;    # The value 1 is reserved for SEQ_ROOT
+    $next_sequence_number     = SEQ_ROOT + 1;
 
     $rparen_type                     = [];
     $rparen_semicolon_count          = [];
@@ -4289,35 +4292,50 @@ EOM
             $statement_type = $tok;
         }
 
-        # Check for misplaced 'elsif' and 'else', but allow isolated
-        # else or elsif blocks to be formatted.  This is indicated
-        # by a last noblank token of ';'
+        # Check for unexpected 'elsif'
         elsif ( $tok eq 'elsif' ) {
             if (
-                $last_nonblank_token ne ';'
 
                 ## !~ /^(if|elsif|unless)$/
-                && !$is_if_elsif_unless{$last_nonblank_block_type}
+                !$is_if_elsif_unless{$last_nonblank_block_type}
+
+                # Allow isolated blocks of any kind during editing
+                # by checking for a last noblank token of ';' and no
+                # sequence numbers having been issued (c272). The check
+                # on sequence number is not perfect but good enough.
+                && !(
+                       $last_nonblank_token eq ';'
+                    && $next_sequence_number == SEQ_ROOT + 1
+                )
+
               )
             {
                 $self->warning(
                     "expecting '$tok' to follow one of 'if|elsif|unless'\n");
             }
         }
+
+        # Check for unexpected 'else'
         elsif ( $tok eq 'else' ) {
 
             # patched for SWITCH/CASE
             if (
-                $last_nonblank_token ne ';'
 
                 ## !~ /^(if|elsif|unless|case|when)$/
-                && !$is_if_elsif_unless_case_when{$last_nonblank_block_type}
+                !$is_if_elsif_unless_case_when{$last_nonblank_block_type}
 
                 # patch to avoid an unwanted error message for
                 # the case of a parenless 'case' (RT 105484):
                 # switch ( 1 ) { case x { 2 } else { } }
                 ## !~ /^(if|elsif|unless|case|when)$/
                 && !$is_if_elsif_unless_case_when{$statement_type}
+
+                # Allow isolated blocks of any kind during editing (c272)
+                && !(
+                       $last_nonblank_token eq ';'
+                    && $next_sequence_number == SEQ_ROOT + 1
+                )
+
               )
             {
                 $self->warning(
index bd1edbd508ab71d399ee5c656f78a04eb0a38c9a..7a3fbc472a3376c0295d2522b5178632b49876c1 100644 (file)
@@ -16,6 +16,10 @@ lc(      $self->mime_attr('content-type')
       || $self->{MIH_DefaultType}
       || 'text/plain' );
 
+if (1) { ... }
+
 # Padding can also remove spaces; here the space after the '(' is lost:
 elsif ($statement_type =~ /^sub\b/
     || $paren_type[$paren_depth] =~ /^sub\b/ )
+{
+}
index 4c564808479645c2bc9b69a5f173eede64ffae7b..ed978e386338f267038a82e302a50f1c6e85cbf6 100644 (file)
@@ -16,6 +16,10 @@ lc( $self->mime_attr('content-type')
       || $self->{MIH_DefaultType}
       || 'text/plain' );
 
+if (1) { ... }
+
 # Padding can also remove spaces; here the space after the '(' is lost:
 elsif ( $statement_type =~ /^sub\b/
     || $paren_type[$paren_depth] =~ /^sub\b/ )
+{
+}
index c889168b068ea6cb1e0a34d4abadbd53f1d02aa1..2ee4456d3e367c558a773bb86900170a8cfa9d35 100644 (file)
@@ -16,6 +16,10 @@ lc( $self->mime_attr('content-type')
         || $self->{MIH_DefaultType}
         || 'text/plain' );
 
+if (1) { ... }
+
 # Padding can also remove spaces; here the space after the '(' is lost:
-elsif ( $statement_type =~ /^sub\b/
+elsif ($statement_type =~ /^sub\b/
     || $paren_type[$paren_depth] =~ /^sub\b/ )
+{
+}
index de698c3a0bf2d3d993f28dfe751d45ee0edc2093..12afe1e277287cfd74a31265b1b520082eff068a 100644 (file)
 ../snippets28.t        ame.ame
 ../snippets28.t        ame.def
 ../snippets28.t        git124.def
+../snippets28.t        c269.c269
+../snippets28.t        c269.def
 ../snippets3.t ce_wn1.ce_wn
 ../snippets3.t ce_wn1.def
 ../snippets3.t colin.colin
 ../snippets9.t rt98902.def
 ../snippets9.t rt98902.rt98902
 ../snippets9.t rt99961.def
-../snippets28.t        c269.c269
-../snippets28.t        c269.def
index d5a8073015e4b08761d3b62fffce082219e16c00..a348b57dc12f0feab1b86a2c8878309a7a177e74 100644 (file)
@@ -179,9 +179,13 @@ lc( $self->mime_attr('content-type')
         || $self->{MIH_DefaultType}
         || 'text/plain' );
 
+if (1) { ... }
+
 # Padding can also remove spaces; here the space after the '(' is lost:
-elsif ( $statement_type =~ /^sub\b/
+elsif ($statement_type =~ /^sub\b/
     || $paren_type[$paren_depth] =~ /^sub\b/ )
+{
+}
 ----------
 
         'outdent' => <<'----------',
@@ -663,9 +667,13 @@ lc(      $self->mime_attr('content-type')
       || $self->{MIH_DefaultType}
       || 'text/plain' );
 
+if (1) { ... }
+
 # Padding can also remove spaces; here the space after the '(' is lost:
 elsif ($statement_type =~ /^sub\b/
     || $paren_type[$paren_depth] =~ /^sub\b/ )
+{
+}
 #19...........
         },
     };
index 3ed043e01538ce78f991d3e3883df5e7ca928631..822456518b85ea6e0842210312479777094e1e66 100644 (file)
@@ -126,9 +126,13 @@ lc( $self->mime_attr('content-type')
         || $self->{MIH_DefaultType}
         || 'text/plain' );
 
+if (1) { ... }
+
 # Padding can also remove spaces; here the space after the '(' is lost:
-elsif ( $statement_type =~ /^sub\b/
+elsif ($statement_type =~ /^sub\b/
     || $paren_type[$paren_depth] =~ /^sub\b/ )
+{
+}
 ----------
 
         'nib' => <<'----------',
@@ -335,9 +339,13 @@ lc( $self->mime_attr('content-type')
       || $self->{MIH_DefaultType}
       || 'text/plain' );
 
+if (1) { ... }
+
 # Padding can also remove spaces; here the space after the '(' is lost:
 elsif ( $statement_type =~ /^sub\b/
     || $paren_type[$paren_depth] =~ /^sub\b/ )
+{
+}
 #1...........
         },