1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep;
15 use Perl::Critic::Utils qw{ :severities :classification hashify };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{"grep" used in boolean context};
23 Readonly::Scalar my $EXPL => [71,72];
25 Readonly::Hash my %POSTFIX_CONDITIONALS => hashify( qw(if unless while until) );
26 Readonly::Hash my %BOOLEAN_OPERATORS => hashify( qw(&& || ! not or and));
28 #-----------------------------------------------------------------------------
30 sub supported_parameters { return () }
31 sub default_severity { return $SEVERITY_LOW }
32 sub default_themes { return qw( core pbp performance ) }
33 sub applies_to { return 'PPI::Token::Word' }
35 #-----------------------------------------------------------------------------
38 my ( $self, $elem, undef ) = @_;
40 return if $elem ne 'grep';
41 return if not is_function_call($elem);
42 return if not _is_in_boolean_context($elem);
44 return $self->violation( $DESC, $EXPL, $elem );
47 #-----------------------------------------------------------------------------
49 sub _is_in_boolean_context {
52 return _does_prev_sibling_cause_boolean($token) || _does_parent_cause_boolean($token);
55 sub _does_prev_sibling_cause_boolean {
58 my $prev = $token->sprevious_sibling;
60 return 1 if $prev->isa('PPI::Token::Word') and $POSTFIX_CONDITIONALS{$prev};
61 return if not ($prev->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$prev});
62 my $next = $token->snext_sibling;
63 return 1 if not $next; # bizarre: grep with no arguments
65 # loose heuristic: unparenthesized grep has no following non-boolean operators
66 return 1 if not $next->isa('PPI::Structure::List');
68 $next = $next->snext_sibling;
69 return 1 if not $next;
70 return 1 if $next->isa('PPI::Token::Operator') and $BOOLEAN_OPERATORS{$next};
74 sub _does_parent_cause_boolean {
77 my $prev = $token->sprevious_sibling;
79 my $parent = $token->statement->parent;
80 for (my $node = $parent; $node; $node = $node->parent) { ##no critic 'CStyleForLoop'
81 next if $node->isa('PPI::Structure::List');
82 return 1 if $node->isa('PPI::Structure::Condition');
92 #-----------------------------------------------------------------------------
98 Perl::Critic::Policy::BuiltinFunctions::ProhibitBooleanGrep - Use C<List::MoreUtils::any> instead of C<grep> in boolean context.
102 This Policy is part of the core L<Perl::Critic> distribution.
107 Using C<grep> in boolean context is a common idiom for checking if any
108 elements in a list match a condition. This works because boolean context is a
109 subset of scalar context, and grep returns the number of matches in scalar
110 context. A non-zero number of matches means a match.
112 But consider the case of a long array where the first element is a match.
113 Boolean C<grep> still checks all of the rest of the elements needlessly.
114 Instead, a better solution is to use the C<any> function from
115 L<List::MoreUtils>, which short-circuits after the first successful match to
121 This Policy is not configurable except for the standard options.
126 The algorithm for detecting boolean context takes a LOT of shortcuts. There
127 are lots of known false negatives. But, I was conservative in writing this,
128 so I hope there are no false positives.
132 Chris Dolan <cdolan@cpan.org>
136 Initial development of this policy was supported by a grant from the Perl
141 Copyright (c) 2007-2008 Chris Dolan. Many rights reserved.
143 This program is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself. The full text of this license
145 can be found in the LICENSE file included with this module.
151 # cperl-indent-level: 4
153 # indent-tabs-mode: nil
154 # c-indentation-style: bsd
156 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :