]> git.donarmstrong.com Git - perltidy.git/commitdiff
allow -wvt=u and -wvt=c in .perltidyrc
authorSteve Hancock <perltidy@users.sourceforge.net>
Mon, 5 Aug 2024 03:21:44 +0000 (20:21 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Mon, 5 Aug 2024 03:21:44 +0000 (20:21 -0700)
CHANGES.md
bin/perltidy
lib/Perl/Tidy/Formatter.pm

index cbd1df713a50dd8a606e011d43b250d2be87552c..c413bacc00d5bf5aa6ebf5731f2ac76bc915e533 100644 (file)
@@ -17,6 +17,9 @@
       is not specified, and when 'asub' is requested with -cscl=asub.
       Use -cscxl=asub to prevent this.
 
+    - Include check for unused constants in --dump-unusual-variables and
+      --warn-variable-types (new issue type 'c').
+
     - Include signature variables in --dump-unusual-variables and
       --warn-variable-types; see git #158.
 
@@ -54,8 +57,7 @@
       where 's' is a control string. These are explained in the manual.
 
     - Updates for issue git #151:
-      (1) --warn-variable-types=u is now okay if it is on the command line
-      with a named file.
+      (1) --warn-variable-types=u is now okay if a named file is processed.
       (2) --warn-variable-exclusion-list=s now allows leading and/or
       trailing * on variable names to allow a wildcard match. For example
       -wvxl='*_unused' is okay and would match $var1_unused and $var2_unused.
index 87a54d48b04d3def7202b1a3df3cff6dfdc97388..10f0c79902f91791cd9669427a32f380c1d6fa9f 100755 (executable)
@@ -6171,10 +6171,12 @@ will process F<somefile.pl> normally but issue a warning if either of
 the issues B<s> or B<r>, described above, are encountered.
 
 The B<u> and B<c> options (unused variables and constants) have a limitation:
-they are not allowed in a F<.perltidyrc> configuration file.  But they can be
-used on the command line provided that perltidy is operating on a named file.
+they will be silently turned off if perltidy is not operating on a named file.
 This rule is necessary to avoid warnings when perltidy is run on small snippets
-of code from within an editor.
+of code from within an editor which passes data through the standard input.  If
+this precaution is not sufficient to prevent incorrect warnings from within an
+editor which uses temporary files, a solution might be to remove the B<-wvt>
+parameter from a F<.perltidyrc> file and only use it on the command line.
 
 A companion flag, B<--warn-variable-exclusion-list=string>, or B<-wvxl=string>,
 can be used to skip warning checks for a list of variable names.  A leading
index a6ca3cd4c3c11b5d3bcd7a32fa3fb824d9a61c37..268ce4d9920a96405fe077afbfe2bc8ebdb0f64f 100644 (file)
@@ -9904,7 +9904,10 @@ sub initialize_warn_hash {
             }
 
             # Special check for -wvt
-            elsif ( ( $opt eq 'u' || $opt eq 'c' )
+            # Deactivated for now to allow -wvt in perltidyrc files. This can
+            # eventually be removed if allowing this does not cause problems.
+            elsif (0
+                && ( $opt eq 'u' || $opt eq 'c' )
                 && $long_name eq 'warn-variable-types' )
             {
                 if ( !$wvt_in_args ) {
@@ -10035,12 +10038,30 @@ sub initialize_warn_variable_types {
     #   $wvt_in_args = true if the -wvt parameter was on the command line
     #   $num_files = number of files on the command line
 
-    my @all_opts = qw(r s p);
-    if ( $wvt_in_args && $num_files ) { push @all_opts, 'u', 'c' }
+    my @all_opts = qw(r s p u c);
     $rwarn_variable_types =
       initialize_warn_hash( 'warn-variable-types', 0, \@all_opts,
         $wvt_in_args );
 
+    # Turn off types 'u' and 'c' if we are not operating on a named file
+    # or are under editor line range control
+    if ( $rOpts->{'line-range-tidy'} || !$num_files ) {
+        $rwarn_variable_types->{u} = 0;
+        $rwarn_variable_types->{c} = 0;
+    }
+
+    # Set 'u' and 'c' conditional on starting indentation = 0 if just 1 file
+    # and -wvt is not on cmd line. The reason is that if -wvt is in the
+    # perltidyrc file, and we are operating on just one file, it could be
+    # a temporary file created by an editor. Requiring a starting level
+    # of zero is a defensive strategy for minimizing the chance of
+    # incorrect warnings when formatting a short snippet.
+    else {
+        if ( !$wvt_in_args && $num_files <= 1 ) {
+            $rwarn_variable_types->{require_sil_zero} = 1;
+        }
+    }
+
     $ris_warn_variable_excluded_name =
       make_excluded_name_hash('warn-variable-exclusion-list');
     return;
@@ -10092,8 +10113,25 @@ sub warn_variable_types {
     my $wv_option = $rOpts->{$wv_key};
     return unless ( %{$rwarn_variable_types} );
 
+    # Make a copy of the control hash
+    my $rwarn_variable_types_copy = {};
+    foreach my $key ( keys %{$rwarn_variable_types} ) {
+        next if ( length($key) > 1 );
+        $rwarn_variable_types_copy->{$key} = $rwarn_variable_types->{$key};
+    }
+
+    # If requested, we must turn off 'u' and 'c' if starting level is not zero
+    if ( $rwarn_variable_types->{require_sil_zero} ) {
+        my $rLL = $self->[_rLL_];
+        my $sil = $rLL->[0]->[_LEVEL_];
+        if ($sil) {
+            $rwarn_variable_types_copy->{u} = 0;
+            $rwarn_variable_types_copy->{c} = 0;
+        }
+    }
+
     my ( $rwarnings, $issue_type_string ) =
-      $self->scan_variable_usage($rwarn_variable_types);
+      $self->scan_variable_usage($rwarn_variable_types_copy);
     return unless ( $rwarnings && @{$rwarnings} );
 
     $rwarnings =