Policy/ValuesAndExpressions/NotWithCompare.pm

filter: Statementstmt 96.4% 137 / 142 Branchbran 91.6% 66 / 72 Conditioncond 78.1% 25 / 32 MC/DCmcdc 63.1% 12 / 19 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 91.1% 257 / 282 SCAR 38.9
CC
48
Cov
93%
violates
40.7
_next_gt
4.0
_snext_is_bang
3.1
CRAP 48.9
« Policy/Miscellanea/TextDomainPlaceholders.pm ↑ Summary Policy/ValuesAndExpressions/RequireNumericVersion.pm »
1 # Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # This file is part of Perl-Critic-Pulp.
4  
5 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare;
20 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 # 1.100 for precedence_of() supporting -f etc filetests
26 40 use Perl::Critic::Utils 1.100 qw(is_perl_builtin
27 is_perl_builtin_with_no_arguments
28 40 ▶ precedence_of);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 40 our $VERSION = 99;
31  
32  
33 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 40 ▶use constant default_themes => qw(pulp bugs);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 40 ▶use constant applies_to => 'PPI::Token::Operator';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 40 my %op_postfix = ('++' => 1,
39 '--' => 1);
40  
41 40 my %op_andor = ('&&' => 1,
42 '||' => 1,
43 '//' => 1,
44 'and' => 1,
45 'or' => 1,
46 'xor' => 1);
47  
48 40 my %post_control = (if => 1,
49 unless => 1,
50 until => 1,
51 for => 1,
52 foreach => 1,
53 while => 1);
54  
55 40 my %is_bad_precedence = (precedence_of('=~') => 1,
56 precedence_of('>') => 1,
57 precedence_of('==') => 1);
58 40 my $stop_precedence = max (keys %is_bad_precedence);
59  
60  
61 sub violates {
62 299 ▶ my ($self, $bang_elem, $document) = @_;
Subroutine violates: called CC=40 SCAR=37.1
Pod documented
63 299 ▶ if ($bang_elem->content ne '!') { return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($bang_elem->content ne "!") 182 117 299
64 117 my $constants;
65  
66 # only report when "!" is at the start of an expression, so "-f ! $x" is
67 # not applicable (though bizarre), or with "! ! $x" look only from the
68 # first "!"
69 117 ▶ if (my $prev = $bang_elem->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $bang_elem->sprevious_sibling) 23 94 117
70 23 ▶ if ($prev->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Operator")) 21 2 23
71 21 my $op = $prev->content;
72 21 ▶ if (! $op_andor{$op}) { # but do look following "&&" etc
Branch: 1 branch
BranchTrueFalseTotal
unless ($op_andor{$op}) 15 6 21
73 15 return;
74 }
75 }
76 }
77  
78 102 my $state = 'prefix';
79 102 my $seen_precedence = 1;
80  
81 102 my $elem = $bang_elem;
82 102 for (;;) {
83 251 ▶ $elem or return; # nothing evil up to end of expression
Branch: 1 branch
BranchTrueFalseTotal
$elem or return 0 251 251
84 251 ▶ $elem = $elem->snext_sibling
Branch: 1 branch
BranchTrueFalseTotal
$elem = $elem->snext_sibling or return 20 231 251
85 or return; # nothing evil up to end of expression
86  
87 231 ▶ if ($elem->isa('PPI::Token::Cast')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Cast")) 6 225 231
88 # "\ &foo" is a single form, not a function call
89 6 $elem = _next_cast_operand ($elem);
90 6 $state = 'postfix';
91 6 next;
92 }
93  
94 225 ▶ if ($elem->isa('PPI::Token::Symbol')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Symbol")) 60 165 225
95 60 $state = 'postfix';
96 60 ▶ if ($elem->content =~ /^&/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->content =~ /^&/) 7 53 60
97 7 ▶ if (my $after = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $after = $elem->snext_sibling) 6 1 7
98 6 ▶ if ($after->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after->isa("PPI::Structure::List")) 2 4 6
99 2 $elem = $after; # "! &foo() == 1"
100 2 next;
101 }
102 }
103 # "! &foo ..." varargs function call, eats to "," or ";"
104 5 return;
105 }
106 53 next; # "! $x" etc
107 }
108  
109 165 ▶ if ($elem->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Operator")) 112 53 165
110 112 my $op = $elem->content;
111  
112 112 ▶ if ($state eq 'postfix' && $op_postfix{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "postfix" and $op_postfix{$op}) 1 111 112
Condition100%3/3
$state eq "postfix" and $op_postfix{$op}
!ll&&!rl&&r
32791
MC/DC100%2/2
$state eq "postfix" and $op_postfix{$op}
$state eq "postfix" $op_postfix{$op}
Truth table100%3/3
A and B
A= $state eq "postfix"B= $op_postfix{$op}
ABresult
0X0
100
111
113 1 next; # stay in postfix state after '++' or '--'
114 }
115 111 ▶ if ($state eq 'prefix' && $op eq '<') {
Branch: 1 branch
BranchTrueFalseTotal
if ($state eq "prefix" and $op eq "<") 4 107 111
Condition100%3/3
$state eq "prefix" and $op eq "<"
!ll&&!rl&&r
79284
MC/DC100%2/2
$state eq "prefix" and $op eq "<"
$state eq "prefix" $op eq "<"
Truth table100%3/3
A and B
A= $state eq "prefix"B= $op eq "<"
ABresult
0X0
100
111
116 # in prefix position assume "<" is "<STDIN>" glob or readline
117 4 $elem = _next_gt ($elem);
118 4 $state = 'postfix';
119 4 next; # can leave $elem undef for something dodgy like "! < 123"
120 }
121 107 ▶ my $precedence = precedence_of($op) || return;
Condition50%1/2
precedence_of($op) || (return)
l!l
1070
MC/DC0%0/1
precedence_of($op) || (return)
precedence_of($op)
Truth table50%1/2
A
A= precedence_of($op)
Aresult
11
00
122  
123 107 ▶ if ($precedence > $stop_precedence) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precedence > $stop_precedence) 18 89 107
124 18 return; # something below "==" etc, expression to ! is ok
125 }
126 89 ▶ if (($op eq '==' || $op eq '!=') && _snext_is_bang($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "==" || $op eq "!=" and _snext_is_bang($elem)) 2 87 89
Condition100%6/6
$op eq "==" || $op eq "!=" and _snext_is_bang($elem)
!ll&&!rl&&r
62252
$op eq "==" or $op eq "!="
l!l&&r!l&&!r
26162
MC/DC100%3/3
$op eq "==" || $op eq "!=" and _snext_is_bang($elem)
$op eq "==" $op eq "!=" _snext_is_bang($elem)
Truth table80%4/5
A or B and C
A= $op eq "=="B= $op eq "!="C= _snext_is_bang($elem)
ABCresult
00X0
1X00
0100
1X11
0111
127 2 return; # special case "! $x == ! $y" is ok
128 }
129 87 ▶ if ($op eq '->') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "->") 10 77 87
130 10 ▶ if (my $method = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $elem->snext_sibling) 10 0 10
131 10 $elem = $method;
132 10 $state = 'postfix';
133 10 ▶ if (my $after = $method->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $after = $method->snext_sibling) 10 0 10
134 10 ▶ if ($after->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after->isa("PPI::Token::Operator")) 9 1 10
135 9 next; # "! $foo->bar == 1"
136 }
137 1 ▶ if ($after->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($after->isa("PPI::Structure::List")) 1 0 1
138 1 $elem = $after; # "! $foo->bar() == 1"
139 1 next;
140 }
141 # bogosity "$foo->bar 123, 456" or the like
142 0 return;
143 }
144 }
145 }
146  
147 77 ▶ if ($seen_precedence <= $precedence && $is_bad_precedence{$precedence}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen_precedence <= $precedence and $is_bad_precedence{$precedence}) 49 28 77
Condition100%3/3
$seen_precedence <= $precedence and $is_bad_precedence{$precedence}
!ll&&!rl&&r
32549
MC/DC100%2/2
$seen_precedence <= $precedence and $is_bad_precedence{$precedence}
$seen_precedence <= $precedence $is_bad_precedence{$precedence}
Truth table100%3/3
A and B
A= $seen_precedence <= $precedenceB= $is_bad_precedence{$precedence}
ABresult
0X0
100
111
148 # $op is a compare, so bad
149 49 return $self->violation
150 ("Logical \"!\" attempted with a compare \"$op\"",
151 '', $bang_elem);
152 }
153 28 $seen_precedence = max ($precedence, $seen_precedence);
154 28 $state = 'prefix';
155 28 next;
156 }
157  
158 53 ▶ if ($elem->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Word")) 24 29 53
159 24 my $word = $elem->content;
160  
161 24 ▶ if ($post_control{$word}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($post_control{$word}) 0 24 24
162 0 return; # postfix control like "$foo = ! $foo if ..." ends expression
163 }
164 24 ▶ if (is_perl_builtin_with_no_arguments ($word)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_perl_builtin_with_no_arguments($word)) 5 19 24
165 # eg "! time ..."
166 # "time" is a single token, look at operators past it
167 5 $state = 'postfix';
168 5 next;
169 }
170  
171 19 ▶ $constants ||= _constants ($document);
Condition33%1/3
$constants ||= _constants($document)
l!l&&r!l&&!r
0190
MC/DC0%0/2
$constants ||= _constants($document)
$constants _constants($document)
Truth table33%1/3
A ||= B
A= $constantsB= _constants($document)
ABresult
1X1
011
000
172 19 ▶ if (exists $constants->{$word}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $constants->{$word}) 5 14 19
173 # eg. use constant FOO => 456;
174 # ! FOO ...
175 # the FOO is a single token, look at operators past it
176 5 $state = 'postfix';
177 5 next;
178 }
179  
180 14 ▶ my $next = $elem->snext_sibling
Branch: 1 branch
BranchTrueFalseTotal
my $next = $elem->snext_sibling or return 2 12 14
181 or return; # "! FOO" expression ending at a bareword
182  
183 12 ▶ if ($next->isa('PPI::Structure::List')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Structure::List")) 3 9 12
184 # "! FOO(...)" function call
185 3 $elem = next;
186 0 $state = 'postfix';
187 0 next;
188 }
189  
190 9 ▶ if (is_perl_builtin ($word)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_perl_builtin($word)) 3 6 9
191 3 return; # builtins all taking args, eating "," or ";"
192 }
193  
194 6 ▶ if ($next->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Operator")) 5 1 6
195 5 my $op = $next->content;
196 5 ▶ if ($op eq '<') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "<") 4 1 5
197 4 ▶ if (_next_gt ($next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_next_gt($next)) 2 2 4
198 # "! FOO <*.c>" assumed to be glob passed to varargs func, it
199 # ends at "," or ";" so nothing bad for "!"
200 2 return;
201 }
202 }
203 # other "! FOO > 123" assumed to be a constant
204 3 $state = 'postfix';
205 3 next;
206 }
207  
208 # otherwise word is a no parens call, like "foo 123, 456"
209 # exactly how this parses depends on the prototype, but there's
210 # going to be a "," or ";" terminating, so our "!" is ok
211 1 return;
212 }
213 }
214  
215 0 return;
216 }
217  
218 sub _snext_is_bang {
219 27 ▶ my ($elem) = @_;
Subroutine _snext_is_bang: called CC=3 SCAR=11.3
220 27 my $next = $elem->snext_sibling;
221 27 ▶ return ($next
Condition66%4/6
$next && $next->isa('PPI::Token::Operator') && $next eq '!'
!ll&&!rl&&r
2502
$next and $next->isa("PPI::Token::Operator")
!ll&&!rl&&r
0252
MC/DC33%1/3
$next && $next->isa('PPI::Token::Operator') && $next eq '!'
$next $next->isa("PPI::Token::Operator") $next eq '!'
Truth table50%2/4
A and B && C
A= $nextB= $next->isa("PPI::Token::Operator")C= $next eq '!'
ABCresult
0XX0
10X0
1100
1111
222 && $next->isa('PPI::Token::Operator')
223 && $next eq '!');
224 }
225  
226 # return the next ">" operator following $elem, or undef if no such
227 sub _next_gt {
228 8 ▶ my ($elem) = @_;
Subroutine _next_gt: called CC=4 SCAR=13.9
229 8 ▶ while ($elem = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->snext_sibling) 16 2 18
230 16 ▶ if ($elem->isa('PPI::Token::Operator') && $elem eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa("PPI::Token::Operator") and $elem eq ">") 6 10 16
Condition100%3/3
$elem->isa("PPI::Token::Operator") and $elem eq ">"
!ll&&!rl&&r
826
MC/DC100%2/2
$elem->isa("PPI::Token::Operator") and $elem eq ">"
$elem->isa("PPI::Token::Operator") $elem eq ">"
Truth table100%3/3
A and B
A= $elem->isa("PPI::Token::Operator")B= $elem eq ">"
ABresult
0X0
100
111
231 6 last;
232 }
233 }
234 8 return $elem;
235 }
236  
237 # $elem is a PPI::Token::Cast, return its operand elem, meaning the next
238 # non-Cast (usually a Symbol). Return undef if no non-cast, for something
239 # dodgy like "\" with nothing following.
240 sub _next_cast_operand {
241 6 ▶ my ($elem) = @_;
Subroutine _next_cast_operand: called CC=3 SCAR=11.0
242 6 ▶ while ($elem = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->snext_sibling) 12 0 12
243 12 ▶ if (! $elem->isa('PPI::Token::Cast')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem->isa("PPI::Token::Cast")) 6 6 12
244 6 last;
245 }
246 }
247 6 return $elem;
248 }
249  
250 # return a hashref which has keys for all the "use constant"s defined in
251 # $document
252 sub _constants {
253 19 ▶ my ($document) = @_;
Subroutine _constants: called CC=2 SCAR=7.0
254 19 ▶ return ($document->{__PACKAGE__.'.NotWithCompareConstants'} ||= do {
Condition33%1/3
$document->{"Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare.NotWithCompareConstants"} ||= do {
l!l&&r!l&&!r
0190
MC/DC0%0/2
$document->{"Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare.NotWithCompareConstants"} ||= do { require Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt; my %constants; $document->find(sub { my($document, $elem) = @_; @constants{Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt::_use_constants($elem)} = (); return 0; } ); \%constants }
$document->{"Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare.NotWithCompareConstants"} do { require Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt; my %constants; $document->find(sub { my($document, $elem) = @_; @constants{Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt::_use_constants($elem)} = (); return 0; } ); \%constants }
Truth table33%1/3
A ||= B
A= $document->{"Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare.NotWithCompareConstants"}B= do { require Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt; my %constants; $document->find(sub { my($document, $elem) = @_; @constants{Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt::_use_constants($elem)} = (); return 0; } ); \%constants }
ABresult
1X1
011
000
255 19 require Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt;
256 19 my %constants;
257 $document->find
258 (sub {
259 253 ▶ my ($document, $elem) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
260 253 @constants{ Perl::Critic::Policy::ValuesAndExpressions::ConstantBeforeLt::_use_constants($elem) }
261 = (); # hash slice
262 253 return 0; # no-match, and continue
263 19 });
264 19 \%constants;
265 });
266 }
267  
268 40 1;
269 __END__
270  
271 =for stopwords booleans varargs builtins args Ryde
272  
273 =head1 NAME
274  
275 Perl::Critic::Policy::ValuesAndExpressions::NotWithCompare - logical not used with compare
276  
277 =head1 DESCRIPTION
278  
279 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
280 add-on. It picks up some cases of logical not C<!> used with a comparison,
281 like
282  
283 ! $x =~ /^[123]/ # bad
284 ! $x + $y >= $z # bad
285  
286 In each case precedence means Perl parses this as C<< (!$x) >>, like
287  
288 (! $x) =~ /^[123]/
289 (! $x) + $y >= $z
290  
291 rather than a negated comparison. Usually this is a mistake, so this policy
292 is under the "bugs" theme (see L<Perl::Critic/POLICY THEMES>).
293  
294 As a special case, C<!> on both sides of C<< == >> or C<< != >> is allowed,
295 since it's quite a good way to compare booleans.
296  
297 !$x == !$y # ok
298 !$x != !$y # ok
299  
300 =head1 LIMITATIONS
301  
302 User functions called without parentheses are assumed to be usual varargs
303 style. But a prototype may mean that's not the case, letting a bad
304 C<!>-with-compare expression to go undetected.
305  
306 ! userfunc $x == 123 # indeterminate
307 # without prototype would be ok: ! (userfunc ($x==123))
308 # with ($) prototype would be bad: (! userfunc($x)) == 123
309  
310 Perl builtins with no args, and constant subs created with C<use constant>
311 or C<sub FOO () {...}> in the file under test are recognised. Hopefully
312 anything else too weird is rare.
313  
314 ! time == 1 # bad
315  
316 use constant FIVE => 5;
317 ! FIVE < 1 # bad
318  
319 sub name () { "foo" }
320 ! name =~ /bar/ # bad
321  
322 =head1 SEE ALSO
323  
324 L<Perl::Critic::Pulp>,
325 L<Perl::Critic>
326  
327 =head1 HOME PAGE
328  
329 http://user42.tuxfamily.org/perl-critic-pulp/index.html
330  
331 =head1 COPYRIGHT
332  
333 Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
334  
335 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
336 under the terms of the GNU General Public License as published by the Free
337 Software Foundation; either version 3, or (at your option) any later
338 version.
339  
340 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
341 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
342 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
343 more details.
344  
345 You should have received a copy of the GNU General Public License along with
346 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
347  
348 =cut
« Policy/Miscellanea/TextDomainPlaceholders.pm ↑ Summary Policy/ValuesAndExpressions/RequireNumericVersion.pm »