Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep - Use C<List::MoreUtils::any> instead of C<grep> in boolean context.


Perl-Critic documentation Contained in the Perl-Critic distribution.

Index


Code Index:

NAME

Top

Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep - Use List::MoreUtils::any instead of grep in boolean context.

AFFILIATION

Top

This Policy is part of the core Perl::Critic distribution.

DESCRIPTION

Top

Using grep in boolean context is a common idiom for checking if any elements in a list match a condition. This works because boolean context is a subset of scalar context, and grep returns the number of matches in scalar context. A non-zero number of matches means a match.

But consider the case of a long array where the first element is a match. Boolean grep still checks all of the rest of the elements needlessly. Instead, a better solution is to use the any function from List::MoreUtils, which short-circuits after the first successful match to save time.

CONFIGURATION

Top

This Policy is not configurable except for the standard options.

CAVEATS

Top

The algorithm for detecting boolean context takes a LOT of shortcuts. There are lots of known false negatives. But, I was conservative in writing this, so I hope there are no false positives.

AUTHOR

Top

Chris Dolan <cdolan@cpan.org>

CREDITS

Top

Initial development of this policy was supported by a grant from the Perl Foundation.

COPYRIGHT

Top


Perl-Critic documentation Contained in the Perl-Critic distribution.

##############################################################################
#      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm $
#     $Date: 2011-05-15 16:34:46 -0500 (Sun, 15 May 2011) $
#   $Author: clonezone $
# $Revision: 4078 $
##############################################################################

package Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep;

use 5.006001;
use strict;
use warnings;
use Readonly;

use Perl::Critic::Utils qw{ :severities :classification hashify };
use base 'Perl::Critic::Policy';

our $VERSION = '1.116';

#-----------------------------------------------------------------------------

Readonly::Scalar my $DESC => q{"grep" used in boolean context};
Readonly::Scalar my $EXPL => [71,72];

Readonly::Hash my %POSTFIX_CONDITIONALS => hashify( qw(if unless while until) );
Readonly::Hash my %BOOLEAN_OPERATORS => hashify( qw(&& || ! not or and));

#-----------------------------------------------------------------------------

sub supported_parameters { return ()                     }
sub default_severity     { return $SEVERITY_LOW          }
sub default_themes       { return qw( core pbp performance ) }
sub applies_to           { return 'PPI::Token::Word'     }

#-----------------------------------------------------------------------------

sub violates {
    my ( $self, $elem, undef ) = @_;

    return if $elem ne 'grep';
    return if not is_function_call($elem);
    return if not _is_in_boolean_context($elem);

    return $self->violation( $DESC, $EXPL, $elem );
}

#-----------------------------------------------------------------------------

sub _is_in_boolean_context {
    my ($token) = @_;

    return _does_prev_sibling_cause_boolean($token) || _does_parent_cause_boolean($token);
}

sub _does_prev_sibling_cause_boolean {
    my ($token) = @_;

    my $prev = $token->sprevious_sibling;
    return if !$prev;
    return 1 if $prev->isa('PPI::Token::Word') and $POSTFIX_CONDITIONALS{$prev};
    return if not ($prev->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$prev});
    my $next = $token->snext_sibling;
    return 1 if not $next; # bizarre: grep with no arguments

    # loose heuristic: unparenthesized grep has no following non-boolean operators
    return 1 if not $next->isa('PPI::Structure::List');

    $next = $next->snext_sibling;
    return 1 if not $next;
    return 1 if $next->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$next};
    return;
}

sub _does_parent_cause_boolean {
    my ($token) = @_;

    my $prev = $token->sprevious_sibling;
    return if $prev;
    my $parent = $token->statement->parent;
    for (my $node = $parent; $node; $node = $node->parent) { ## no critic (CStyleForLoop)
        next if $node->isa('PPI::Structure::List');
        return 1 if $node->isa('PPI::Structure::Condition');
    }

    return;
}

1;

__END__

#-----------------------------------------------------------------------------

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 78
#   indent-tabs-mode: nil
#   c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :