| 1 | package Perl::Critic::Policy::BuiltinFunctions::ProhibitIndirectBooleanGrep 0.001; | ||
| 2 | |||
| 3 | # ABSTRACT: Use any or first, not a sub that returns a grep, for truth or the first match. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use List::Util qw{any first}; | ||
| 11 | use Perl::Critic::Distribution (); | ||
| 12 | use Perl::Critic::Utils qw{ :severities }; | ||
| 13 | |||
| 14 | use parent qw{Perl::Critic::Policy}; | ||
| 15 | |||
| 16 | |||
| 17 | Readonly::Scalar my $DESC_TRUTH => q{A sub that returns a grep, tested for truth}; | ||
| 18 | Readonly::Scalar my $EXPL_TRUTH => q{Use List::Util::any, which stops at the first match}; | ||
| 19 | Readonly::Scalar my $DESC_FIRST => q{A sub that returns a grep, for its first element}; | ||
| 20 | Readonly::Scalar my $EXPL_FIRST => q{Use List::Util::first, which stops at the first match}; | ||
| 21 | |||
| 22 | Readonly::Hash my %CONDITIONAL => map { $_ => 1 } qw{ if elsif unless while until }; | ||
| 23 | Readonly::Hash my %NEGATION => map { $_ => 1 } qw{ ! not }; | ||
| 24 | Readonly::Hash my %LOGICAL => map { $_ => 1 } qw{ && || and or }; | ||
| 25 | |||
| 26 | # Change it when what the collector returns changes shape. | ||
| 27 | Readonly::Scalar my $DATA_FORMAT => 1; | ||
| 28 | |||
| 29 | # The part of a name after its last ::, which is all of a bare name. | ||
| 30 | my $bare_name = sub { | ||
| 31 | my ($name) = @_; | ||
| 32 | my $at = rindex $name, '::'; | ||
| 33 | return $at < 0 ? $name : substr $name, $at + 2; | ||
| 34 | }; | ||
| 35 | |||
| 36 | |||
| 37 | sub supported_parameters { return () } | ||
| 38 | sub default_severity { return $SEVERITY_LOW } | ||
| 39 | sub default_themes { return qw{ performance } } | ||
| 40 | sub applies_to { return 'PPI::Document' } | ||
| 41 | |||
| 42 | |||
| 43 | sub initialize_if_enabled { | ||
| 44 | my ( $self, $config ) = @_; | ||
| 45 | |||
| 46 | Perl::Critic::Distribution->register( | ||
| 47 | name => __PACKAGE__, | ||
| 48 | version => join( q{/}, $DATA_FORMAT, Perl::Critic::Distribution->stamp(__FILE__) // q{} ), | ||
| 49 | collect => sub { | ||
| 50 | my ($ppi) = @_; | ||
| 51 | return { grep_subs => [ map { $_->[1] } grep { !$_->[0]->type } grep_subs_in( $ppi, packages_in($ppi) ) ] }; | ||
| 52 | }, | ||
| 53 | ); | ||
| 54 | return $self->SUPER::initialize_if_enabled($config); | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub violates { | ||
| 59 | my ( $self, undef, $doc ) = @_; | ||
| 60 | |||
| 61 | # The subs of this file by the name that a call here uses, and those of | ||
| 62 | # the rest of the distribution by their full name. | ||
| 63 | my $packages = packages_in($doc); | ||
| 64 | my %local = map { ( $_->[0]->name => 1 ) } grep_subs_in( $doc, $packages ); | ||
| 65 | my %remote; | ||
| 66 | my $filename = $doc->filename; | ||
| 67 | if ( my $dist = defined $filename && Perl::Critic::Distribution->for_file($filename) ) { | ||
| 68 | %remote = map { | ||
| 69 | map { ( $_ => 1 ) } | ||
| 70 | @{ $_->{grep_subs} } | ||
| 71 | } values %{ $dist->collected(__PACKAGE__) }; | ||
| 72 | } | ||
| 73 | return if !%local && !%remote; | ||
| 74 | |||
| 75 | # The last part of each full name, so that a word that cannot be one is | ||
| 76 | # passed over before its package is looked for. | ||
| 77 | my %bare = map { ( $bare_name->($_) => 1 ) } keys %remote; | ||
| 78 | |||
| 79 | my @violations; | ||
| 80 | foreach my $word ( @{ $doc->find('PPI::Token::Word') || [] } ) { | ||
| 81 | my $name = $word->content; | ||
| 82 | next if !$local{$name} && !$bare{ $bare_name->($name) }; | ||
| 83 | my $full = index( $name, '::' ) >= 0 ? $name : package_at( $word, $packages ) . "::$name"; | ||
| 84 | next if !$local{$name} && !$remote{$full}; | ||
| 85 | next if !is_call($word); | ||
| 86 | |||
| 87 | my $use = use_of($word) or next; | ||
| 88 | push @violations, $use eq 'first' | ||
| 89 | ? $self->violation( $DESC_FIRST, $EXPL_FIRST, $word ) | ||
| 90 | : $self->violation( $DESC_TRUTH, $EXPL_TRUTH, $word ); | ||
| 91 | } | ||
| 92 | return @violations; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | sub grep_subs_in { | ||
| 97 | my ( $ppi, $packages ) = @_; | ||
| 98 | |||
| 99 | my @found; | ||
| 100 | foreach my $sub ( @{ $ppi->find('PPI::Statement::Sub') || [] } ) { | ||
| 101 | my $block = $sub->block or next; | ||
| 102 | next if $block->find_any( sub { $_[1]->isa('PPI::Token::Word') && $_[1]->content eq 'wantarray' } ); | ||
| 103 | next if !returns_grep($block); | ||
| 104 | |||
| 105 | my $name = $sub->name; | ||
| 106 | push @found, [ $sub, index( $name, '::' ) >= 0 ? $name : package_at( $sub, $packages ) . "::$name" ]; | ||
| 107 | } | ||
| 108 | return @found; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub returns_grep { | ||
| 113 | my ($block) = @_; | ||
| 114 | |||
| 115 | my $starts_grep = sub { | ||
| 116 | my ( $statement, $skip ) = @_; | ||
| 117 | my @parts = $statement->schildren; | ||
| 118 | shift @parts if $skip; | ||
| 119 | return @parts && $parts[0]->isa('PPI::Token::Word') && $parts[0]->content eq 'grep'; | ||
| 120 | }; | ||
| 121 | |||
| 122 | my $returns = $block->find( sub { $_[1]->isa('PPI::Statement::Break') && ( $_[1]->schild(0) // q{} ) eq 'return' } ) || []; | ||
| 123 | foreach my $return (@$returns) { | ||
| 124 | next if !$starts_grep->( $return, 1 ); | ||
| 125 | |||
| 126 | # One inside an inner sub returns from that sub. | ||
| 127 | my $inner = 0; | ||
| 128 | for ( my $up = $return->parent; $up && $up != $block; $up = $up->parent ) { | ||
| 129 | $inner ||= $up->isa('PPI::Statement::Sub') || ( $up->isa('PPI::Structure::Block') && ( $up->sprevious_sibling // q{} ) eq 'sub' ); | ||
| 130 | } | ||
| 131 | return 1 if !$inner; | ||
| 132 | } | ||
| 133 | |||
| 134 | my $last = ( grep { $_->isa('PPI::Statement') } $block->schildren )[-1]; | ||
| 135 | return $last && ref $last eq 'PPI::Statement' && $starts_grep->( $last, 0 ); | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | sub packages_in { | ||
| 140 | my ($ppi) = @_; | ||
| 141 | return $ppi->find('PPI::Statement::Package') || []; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | sub package_at { | ||
| 146 | my ( $elem, $packages ) = @_; | ||
| 147 | |||
| 148 | my $package = 'main'; | ||
| 149 | foreach my $statement (@$packages) { | ||
| 150 | my $block = first { $_->isa('PPI::Structure::Block') } $statement->schildren; | ||
| 151 | if ($block) { | ||
| 152 | for ( my $up = $elem->parent; $up; $up = $up->parent ) { | ||
| 153 | return $statement->namespace if $up == $block; | ||
| 154 | } | ||
| 155 | next; | ||
| 156 | } | ||
| 157 | my ( $line, $col ) = @{ $statement->location }[ 0, 1 ]; | ||
| 158 | my ( $at_line, $at_col ) = @{ $elem->location }[ 0, 1 ]; | ||
| 159 | $package = $statement->namespace if $line < $at_line || ( $line == $at_line && $col < $at_col ); | ||
| 160 | } | ||
| 161 | return $package; | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | sub is_call { | ||
| 166 | my ($word) = @_; | ||
| 167 | |||
| 168 | my $parent = $word->parent; | ||
| 169 | return 0 if $parent->isa('PPI::Statement::Sub'); | ||
| 170 | my $before = $word->sprevious_sibling; | ||
| 171 | return 0 if $before && $before->isa('PPI::Token::Operator') && $before->content eq '->'; | ||
| 172 | my $after = $word->snext_sibling; | ||
| 173 | return 0 if $after && $after->isa('PPI::Token::Operator') && $after->content eq '=>'; | ||
| 174 | return 0 if $parent->parent && $parent->parent->isa('PPI::Structure::Subscript') && $parent->schildren == 1; | ||
| 175 | return 1; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | sub use_of { | ||
| 180 | my ($word) = @_; | ||
| 181 | |||
| 182 | # The call as an operand: the word, and its argument list when it has one. | ||
| 183 | my $end = $word->snext_sibling; | ||
| 184 | $end = $word if !( $end && $end->isa('PPI::Structure::List') ); | ||
| 185 | |||
| 186 | my $before = $word->sprevious_sibling; | ||
| 187 | my $statement = $word->parent; | ||
| 188 | |||
| 189 | # The semicolon that ends the statement is not a thing after the call. | ||
| 190 | my $after = $end->snext_sibling; | ||
| 191 | undef $after if $after && $after->isa('PPI::Token::Structure') && $after->content eq ';'; | ||
| 192 | |||
| 193 | return 'truth' if $before && $NEGATION{ $before->content }; | ||
| 194 | return 'truth' if $after && $after->content eq '?' && ( !$before || $before->content eq '=' || $before->content eq 'return' ); | ||
| 195 | |||
| 196 | # In a condition, alone or among logical operators and their operands. | ||
| 197 | my $holder = $statement->parent; | ||
| 198 | if ( $holder && $holder->isa('PPI::Structure::Condition') ) { | ||
| 199 | my $only_logic = 1; | ||
| 200 | foreach my $part ( $statement->schildren ) { | ||
| 201 | next if $part == $word || $part == $end; | ||
| 202 | next if $LOGICAL{ $part->content } || $NEGATION{ $part->content }; | ||
| 203 | $only_logic = 0 if $part->isa('PPI::Token::Operator'); | ||
| 204 | } | ||
| 205 | return 'truth' if $only_logic; | ||
| 206 | } | ||
| 207 | |||
| 208 | # After a postfix modifier, by place among the parts of the statement, | ||
| 209 | # because a statement can wrap. | ||
| 210 | my @parts = $statement->schildren; | ||
| 211 | my ($at) = grep { $parts[$_] == $word } 0 .. $#parts; | ||
| 212 | my $modifier = first { $_ > 0 && $parts[$_]->isa('PPI::Token::Word') && $CONDITIONAL{ $parts[$_]->content } } 0 .. $at - 1; | ||
| 213 | if ( defined $modifier ) { | ||
| 214 | return 'truth' if $before == $parts[$modifier]; | ||
| 215 | return 'truth' | ||
| 216 | if $LOGICAL{ $before->content } | ||
| 217 | && !any { $_->isa('PPI::Token::Operator') && !$LOGICAL{ $_->content } && !$NEGATION{ $_->content } } @parts[ $modifier + 1 .. $at - 1 ]; | ||
| 218 | } | ||
| 219 | |||
| 220 | # The left of a logical operator that starts the statement, as in a check | ||
| 221 | # followed by or die. | ||
| 222 | return 'truth' if !$before && $after && $LOGICAL{ $after->content } && $statement->schild(0) == $word; | ||
| 223 | |||
| 224 | # A list of one scalar on the left of the assignment, declared or not. | ||
| 225 | if ( $before && $before->content eq '=' && !$after ) { | ||
| 226 | my $target = $before->sprevious_sibling; | ||
| 227 | if ( $target && $target->isa('PPI::Structure::List') ) { | ||
| 228 | my @symbols = @{ $target->find('PPI::Token::Symbol') || [] }; | ||
| 229 | return 'first' if @symbols == 1 && $symbols[0]->raw_type eq '$'; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | __END__ | ||