From 9d74cd57577dfb699346857d6213d7375fb1f09c Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 23 Aug 2023 10:42:51 -0700 Subject: [PATCH] convert two boolean grep's to 'first' Allows perlcritic to check for this; needed to use 'List::Util::first' since 'any' was not in Perl 5.8. --- .perlcriticrc | 27 +++++++++++---------------- lib/Perl/Tidy/Formatter.pm | 6 +++--- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/.perlcriticrc b/.perlcriticrc index 88ed59d8..17814ee6 100644 --- a/.perlcriticrc +++ b/.perlcriticrc @@ -1,4 +1,4 @@ -# perlcritic is a useful tool for locating potential code problems. +# Perlcritic is a very useful tool for locating potential code problems. # This file customizes it to the specific needs of Perl::Tidy. # Some useful links: @@ -11,7 +11,9 @@ # Many of the policies are excellent starting points for new code, but # important exceptions often exist which make it impossible to use them as # rigid rules. I have found that using 'no critic' comments is much too -# troublesome, so non-configurable policies are either 'on' or 'off'. +# troublesome, so non-configurable policies which have exceptions must be +# turned off. This file lists the policies which must be turned off for +# Perl::Tidy, at least for now. # severity = 1 gives the most strict checking. severity = 1 @@ -22,7 +24,7 @@ verbose = %f: [%p] %m at line %l, column %c.\n # Following is a list of policies to be skipped for severity=4: #-------------------------------------------------------------- -# There is a localization in Tokenizer.pm that is essential +# There is a localization in Tokenizer.pm that is essential for recursion [-Variables::ProhibitLocalVars] # Immediate initialization of locals is not appropriate where used @@ -62,7 +64,9 @@ short_subroutine_statements = 2 # Perlcritic doesn't know that ARGV in Perl::Tidy actually is localized. # Localization of @ARGV could be avoided by calling GetOptionsFromArray # instead of GetOptions, but that is not available before perl 5.10, and -# we want to continue supporting Perl 5.8. +# we want to continue supporting Perl 5.8. So we have to skip this for now. +# When the time comes to make perl 5.10 the earliest version supported, +# this can be fixed. [-Variables::RequireLocalizedPunctuationVars] # Unfortunately the perlcritic coding for this policy is buggy when lines=n is @@ -92,7 +96,7 @@ max_mccabe=180 [ControlStructures::ProhibitDeepNests] max_nests=9 -# There are many cases where this is not possible +# This would be nice, but there are many cases where this is really impossible. [-ControlStructures::ProhibitCascadingIfElse] # This is a reasonable starting point but does not work well as a rigid rule. @@ -112,6 +116,8 @@ max_nests=9 # These would be okay for new code, but do not change any debugged regular # expressions without good reason. It is too easy to introduce a subtle error. +# A problem with ReqireExtendedFormatting is that it makes things needlessly +# complex when matching things like line feeds and carriage returns. [-RegularExpressions::RequireExtendedFormatting] [-RegularExpressions::ProhibitComplexRegexes] [-RegularExpressions::ProhibitUnusedCapture] @@ -163,17 +169,6 @@ max_nests=9 # return if (!$everything_is_ok); [-ControlStructures::ProhibitUnlessBlocks] -# The very few instances of boolean grep in Perl::Tidy are fine. Profiling -# shows that changing them would not result in a measureable improvement in -# speed. -[-BuiltinFunctions::ProhibitBooleanGrep] - -# The only escaped characters in Perl::Tidy are in code for detecting and -# setting line endings ( CR and LF ). This is fully debugged coding and best -# left unchanged. But this is a good idea and someday I might go through and -# fix these. -[-ValuesAndExpressions::ProhibitEscapedCharacters] - # This is a good general idea but has to be turned off because there are many # cases where a number has been explained in a comment or is obvious. [-ValuesAndExpressions::ProhibitMagicNumbers] diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index f8cc0c8c..4f51f9fc 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -52,7 +52,7 @@ use constant SPACE => q{ }; use Carp; use English qw( -no_match_vars ); -use List::Util qw( min max ); # min, max are in Perl 5.8 +use List::Util qw( min max first ); # min, max first are in Perl 5.8 our $VERSION = '20230701.02'; # The Tokenizer will be loaded with the Formatter @@ -25226,11 +25226,11 @@ sub study_list_complexity { } } else { - if ( grep { $_ eq 'b' } @types_to_go[ $ib .. $ie ] ) { + if ( first { $_ eq 'b' } @types_to_go[ $ib .. $ie ] ) { $complex_item_count++; $weighted_length *= 2; } - if ( grep { $_ eq '..' } @types_to_go[ $ib .. $ie ] ) { + if ( first { $_ eq '..' } @types_to_go[ $ib .. $ie ] ) { $weighted_length += 4; } } -- 2.39.5