File Coverage

blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitUselessTopic.pm
Criterion Covered Total %
statement 24 31 77.4
branch 1 8 12.5
condition 0 9 0.0
subroutine 11 11 100.0
pod 4 5 80.0
total 40 64 62.5


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::RegularExpressions::ProhibitUselessTopic;
2              
3 40     40   27009 use 5.010;
  40         214  
4 40     40   320 use strict;
  40         115  
  40         859  
5 40     40   257 use warnings;
  40         134  
  40         983  
6 40     40   320 use Readonly;
  40         132  
  40         2267  
7              
8 40     40   311 use Perl::Critic::Utils qw{ :severities :classification :ppi hashify };
  40         144  
  40         2204  
9 40     40   16977 use parent 'Perl::Critic::Policy';
  40         149  
  40         299  
10              
11             our $VERSION = '1.150';
12              
13             ## no critic ( ValuesAndExpressions::RequireInterpolationOfMetachars )
14             ## The numerous $_ variables make false positives.
15             Readonly::Scalar my $DESC => q{Useless use of $_};
16             Readonly::Scalar my $EXPL => q{$_ should be omitted when matching a regular expression};
17              
18 89     89 0 1649 sub supported_parameters { return () }
19 74     74 1 300 sub default_severity { return $SEVERITY_LOW }
20 74     74 1 328 sub default_themes { return qw( core ) }
21 30     30 1 80 sub applies_to { return 'PPI::Token::Magic' }
22              
23             sub violates {
24 15     15 1 26 my ( $self, $elem, undef ) = @_;
25              
26 15 50       25 if ( $elem->content eq q{$_} ) {
27             # Is there an op following the $_ ?
28 0         0 my $op_node = $elem->snext_sibling;
29 0 0 0     0 if ( $op_node && $op_node->isa('PPI::Token::Operator') ) {
30             # If the op is a regex match, then we have an unnecessary $_ .
31 0         0 state $is_regex_op = { hashify( qw( =~ !~ ) ) };
32 0 0       0 if ( $is_regex_op->{$op_node->content} ) {
33 0         0 my $target_node = $op_node->snext_sibling;
34 0 0 0     0 if ( $target_node && ($target_node->isa('PPI::Token::Regexp') || $target_node->isa('PPI::Token::QuoteLike::Regexp')) ) {
      0        
35 0         0 return $self->violation( $DESC, $EXPL, $elem );
36             }
37             }
38             }
39             }
40              
41 15         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-2023 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 :