From: Steve Hancock Date: Mon, 5 Aug 2024 03:21:44 +0000 (-0700) Subject: allow -wvt=u and -wvt=c in .perltidyrc X-Git-Tag: 20240511.09~1 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=29bf936d41fd9229ef20dc7eb44c04a1f724163c;p=perltidy.git allow -wvt=u and -wvt=c in .perltidyrc --- diff --git a/CHANGES.md b/CHANGES.md index cbd1df71..c413bacc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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. diff --git a/bin/perltidy b/bin/perltidy index 87a54d48..10f0c799 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -6171,10 +6171,12 @@ will process F normally but issue a warning if either of the issues B or B, described above, are encountered. The B and B 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 diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index a6ca3cd4..268ce4d9 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -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 =