File Coverage

blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitUselessTopic.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 8 100.0
condition 10 12 83.3
subroutine 10 10 100.0
pod 4 5 80.0
total 62 65 95.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::RegularExpressions::ProhibitUselessTopic;
2              
3 40     40   28158 use strict;
  40         157  
  40         1256  
4 40     40   259 use warnings;
  40         114  
  40         980  
5 40     40   256 use Readonly;
  40         131  
  40         2134  
6              
7 40     40   302 use Perl::Critic::Utils qw{ :severities :classification :ppi };
  40         114  
  40         2120  
8 40     40   17085 use parent 'Perl::Critic::Policy';
  40         155  
  40         291  
9              
10             our $VERSION = '1.146';
11              
12             ## no critic ( ValuesAndExpressions::RequireInterpolationOfMetachars )
13             ## The numerous $_ variables make false positives.
14             Readonly::Scalar my $DESC => q{Useless use of $_};
15             Readonly::Scalar my $EXPL => q{$_ should be omitted when matching a regular expression};
16              
17 97     97 0 1723 sub supported_parameters { return () }
18 99     99 1 429 sub default_severity { return $SEVERITY_LOW }
19 74     74 1 306 sub default_themes { return qw( core ) }
20 38     38 1 126 sub applies_to { return 'PPI::Token::Magic' }
21              
22             sub violates {
23 47     47 1 88 my ( $self, $elem, undef ) = @_;
24              
25 47         109 my $content = $elem->content;
26 47 100       195 if ( $content eq q{$_} ) {
27             # Is there an op following the $_ ?
28 32         85 my $op_node = $elem->snext_sibling;
29 32 100 66     801 if ( $op_node && $op_node->isa('PPI::Token::Operator') ) {
30             # If the op is a regex match, then we have an unnecessary $_ .
31 31         79 my $op = $op_node->content;
32 31 100 100     166 if ( $op eq q{=~} || $op eq q{!~} ) {
33 30         67 my $target_node = $op_node->snext_sibling;
34 30 100 100     692 if ( $target_node && ($target_node->isa('PPI::Token::Regexp') || $target_node->isa('PPI::Token::QuoteLike::Regexp')) ) {
      66        
35 25         81 return $self->violation( $DESC, $EXPL, $elem );
36             }
37             }
38             }
39             }
40              
41 22         55 return;
42             }
43              
44             1;
45              
46             __END__
47              
48             =pod
49              
50             =head1 NAME
51              
52             Perl::Critic::Policy::RegularExpressions::ProhibitUselessTopic - Don't use $_ to match against regexes.
53              
54             =head1 AFFILIATION
55              
56             This Policy is part of the L<Perl::Critic|Perl::Critic> distribution.
57              
58             =head1 DESCRIPTION
59              
60             It is not necessary to specify the topic variable C<$_> when matching
61             against a regular expression.
62              
63             Match or substitution operations are performed against variables, such as:
64              
65             $x =~ /foo/;
66             $x =~ s/foo/bar/;
67             $x =~ tr/a-mn-z/n-za-m/;
68              
69             If a variable is not specified, the match is against C<$_>.
70              
71             # These are identical.
72             /foo/;
73             $_ =~ /foo/;
74              
75             # These are identical.
76             s/foo/bar/;
77             $_ =~ s/foo/bar/;
78              
79             # These are identical.
80             tr/a-mn-z/n-za-m/;
81             $_ =~ tr/a-mn-z/n-za-m/;
82              
83             This applies to negative matching as well.
84              
85             # These are identical
86             if ( $_ !~ /DEBUG/ ) { ...
87             if ( !/DEBUG ) { ...
88              
89             Including the C<$_ =~> or C<$_ !~> is unnecessary, adds complexity,
90             and is not idiomatic Perl.
91              
92             =head1 CONFIGURATION
93              
94             This Policy is not configurable except for the standard options.
95              
96             =head1 AUTHOR
97              
98             Andy Lester <andy@petdance.com>
99              
100             =head1 COPYRIGHT
101              
102             Copyright (c) 2013 Andy Lester <andy@petdance.com>
103              
104             This library is free software; you can redistribute it and/or modify it
105             under the terms of the Artistic License 2.0.
106              
107             =cut
108              
109             # Local Variables:
110             # mode: cperl
111             # cperl-indent-level: 4
112             # fill-column: 78
113             # indent-tabs-mode: nil
114             # c-indentation-style: bsd
115             # End:
116             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :