blib/lib/Perl/Critic/Policy/RegularExpressions/ProhibitUselessTopic.pm

filter: Statementstmt 80.0% 28 / 35 Branchbran 12.5% 1 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 60.2% 44 / 73 CC 8 SCAR 30.8
CC
8
Cov
56%
violates
47.6
BEGIN
1.0
BEGIN
1.0
CRAP 21.7
« blib/lib/Perl/Critic/Policy/References/ProhibitDoubleSigils.pm ↑ Summary blib/lib/Test/Perl/Critic/Policy.pm »
1 package Perl::Critic::Policy::RegularExpressions::ProhibitUselessTopic;
2  
3 40 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :classification :ppi hashify };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 our $VERSION = '1.150';
12  
13 ## no critic ( ValuesAndExpressions::RequireInterpolationOfMetachars )
14 ## The numerous $_ variables make false positives.
15 40 Readonly::Scalar my $DESC => q{Useless use of $_};
16 40 Readonly::Scalar my $EXPL => q{$_ should be omitted when matching a regular expression};
17  
18 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
19 75 ▶sub default_severity { return $SEVERITY_LOW }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
20 74 ▶sub default_themes { return qw( core ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
21 31 ▶sub applies_to { return 'PPI::Token::Magic' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
22  
23 sub violates {
24 15 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=8 SCAR=38.6
Pod documented
25  
26 15 ▶ if ( $elem->content eq q{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->content eq "\$_") 0 15 15
27 # Is there an op following the $_ ?
28 0 my $op_node = $elem->snext_sibling;
29 0 ▶ if ( $op_node && $op_node->isa('PPI::Token::Operator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op_node and $op_node->isa("PPI::Token::Operator")) 0 0 0
Condition0%0/3
$op_node and $op_node->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
MC/DC0%0/2
$op_node and $op_node->isa("PPI::Token::Operator")
$op_node $op_node->isa("PPI::Token::Operator")
Truth table0%0/3
A and B
A= $op_nodeB= $op_node->isa("PPI::Token::Operator")
ABresult
0X0
100
111
30 # If the op is a regex match, then we have an unnecessary $_ .
31 0 state $is_regex_op = { hashify( qw( =~ !~ ) ) };
32 0 ▶ if ( $is_regex_op->{$op_node->content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$is_regex_op{$op_node->content}) 0 0 0
33 0 my $target_node = $op_node->snext_sibling;
34 0 ▶ if ( $target_node && ($target_node->isa('PPI::Token::Regexp') || $target_node->isa('PPI::Token::QuoteLike::Regexp')) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_node and $target_node->isa("PPI::Token::Regexp") || $target_node->isa("PPI::Token::QuoteLike::Regexp")) 0 0 0
Condition0%0/6
$target_node and $target_node->isa("PPI::Token::Regexp") || $target_node->isa("PPI::Token::QuoteLike::Regexp")
!ll&&!rl&&r
000
$target_node->isa("PPI::Token::Regexp") or $target_node->isa("PPI::Token::QuoteLike::Regexp")
l!l&&r!l&&!r
000
MC/DC0%0/3
$target_node and $target_node->isa("PPI::Token::Regexp") || $target_node->isa("PPI::Token::QuoteLike::Regexp")
$target_node $target_node->isa("PPI::Token::Regexp") $target_node->isa("PPI::Token::QuoteLike::Regexp")
Truth table0%0/4
A and B or C
A= $target_nodeB= $target_node->isa("PPI::Token::Regexp")C= $target_node->isa("PPI::Token::QuoteLike::Regexp")
ABCresult
0XX0
1000
11X1
1011
35 0 return $self->violation( $DESC, $EXPL, $elem );
36 }
37 }
38 }
39 }
40  
41 15 return;
42 }
43  
44 40 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 :
« blib/lib/Perl/Critic/Policy/References/ProhibitDoubleSigils.pm ↑ Summary blib/lib/Test/Perl/Critic/Policy.pm »