Policy/ValuesAndExpressions/ProhibitNullStatements.pm

filter: Statementstmt 100% 67 / 67 Branchbran 89.2% 25 / 28 Conditioncond 78.5% 22 / 28 MC/DCmcdc 58.8% 10 / 17 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 89.6% 139 / 155 CC 25 SCAR 18.1
CC
25
Cov
93%
_is_block_disambiguator
9.1
_is_end_of_try_block
8.3
violates
8.0
CRAP 6.1
« Policy/ValuesAndExpressions/ProhibitArrayAssignAref.pm ↑ Summary Pulp/Utils.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::ProhibitNullStatements;
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 base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon; # for try helpers
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 40 our $VERSION = 100;
28  
29  
30 40 ▶use constant supported_parameters =>
Subroutine BEGIN: called CC=1 SCAR=0.0
31 ({ name => 'allow_perl4_semihash',
32 description => 'Whether to allow Perl 4 style ";#" comments.',
33 behavior => 'boolean',
34 default_string => '0',
35 40 });
36 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 40 ▶use constant applies_to => ('PPI::Statement::Null', 'PPI::Token::Structure');
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 154 ▶sub violates {
Subroutine violates: called CC=8 SCAR=20.8
Pod documented
41 154 my ($self, $elem, $document) = @_;
42  
43 154 ▶ if ($elem->isa('PPI::Statement::Null')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->isa('PPI::Statement::Null')) { } 19 135 154
44  
45 # if allow_perl4_semihash then ";# comment ..." ok
46 19 ▶ if ($self->{'_allow_perl4_semihash'} && _is_perl4_semihash($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_allow_perl4_semihash"} and _is_perl4_semihash($elem)) 2 17 19
Condition100%3/3
$self->{"_allow_perl4_semihash"} and _is_perl4_semihash($elem)
!ll&&!rl&&r
1522
MC/DC100%2/2
$self->{"_allow_perl4_semihash"} and _is_perl4_semihash($elem)
$self->{"_allow_perl4_semihash"} _is_perl4_semihash($elem)
Truth table100%3/3
A and B
A= $self->{"_allow_perl4_semihash"}B= _is_perl4_semihash($elem)
ABresult
0X0
100
111
47 2 return; # ok
48 }
49  
50 # "for (;;)" is ok, like
51 #
52 # PPI::Structure::ForLoop ( ... )
53 # PPI::Statement::Null
54 # PPI::Token::Structure ';'
55 # PPI::Statement::Null
56 # PPI::Token::Structure ';'
57 #
58 # or the incompatible change in ppi 1.205
59 #
60 # PPI::Token::Word 'for'
61 # PPI::Structure::For ( ... )
62 # PPI::Statement::Null
63 # PPI::Token::Structure ';'
64 # PPI::Statement::Null
65 # PPI::Token::Structure ';'
66  
67 17 my $parent = $elem->parent;
68 17 ▶ if ($parent->isa('PPI::Structure::For')
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Structure::For") or $parent->isa("PPI::Structure::ForLoop")) 2 15 17
Condition66%2/3
$parent->isa("PPI::Structure::For") or $parent->isa("PPI::Structure::ForLoop")
l!l&&r!l&&!r
2015
MC/DC50%1/2
$parent->isa("PPI::Structure::For") or $parent->isa("PPI::Structure::ForLoop")
$parent->isa("PPI::Structure::For") $parent->isa("PPI::Structure::ForLoop")
Truth table66%2/3
A or B
A= $parent->isa("PPI::Structure::For")B= $parent->isa("PPI::Structure::ForLoop")
ABresult
1X1
011
000
69 || $parent->isa('PPI::Structure::ForLoop')) {
70 2 return; # ok
71 }
72  
73 # "map {; ...}" or "grep {; ...}" ok
74 15 ▶ if (_is_block_disambiguator ($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_block_disambiguator($elem)) 7 8 15
75 7 return; # ok
76 }
77 } else {
78 # PPI::Token::Structure ...
79  
80 135 ▶ if (! _is_end_of_try_block($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_end_of_try_block($elem)) 131 4 135
81 # not a semi at the end of a try {} catch {}; block, ok
82 131 return;
83 }
84 }
85  
86 # any other PPI::Statement::Null is a bare ";" and is not ok, like
87 #
88 # PPI::Statement::Null
89 # PPI::Token::Structure ';'
90 #
91 12 return $self->violation ('Null statement (stray semicolon)',
92 '',
93 $elem);
94 }
95  
96 40 my %is_try_catch_keyword = (try => 1,
97 catch => 1,
98 finally => 1);
99  
100 # $elem is a PPI::Token::Structure
101 # Return true if it's a semicolon ; at the end of a try/catch block for any
102 # Try.pm, TryCatch.pm or Syntax::Feature::Try. Such a ; is unnecessary.
103 135 ▶sub _is_end_of_try_block {
Subroutine _is_end_of_try_block: called CC=8 SCAR=21.2
104 135 my ($elem) = @_;
105  
106 135 ▶ ($elem->content eq ';'
Branch: 1 branch
BranchTrueFalseTotal
$elem->content eq ";" and Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_elem_is_try_block($elem->parent) or return 0 6 129 135
Condition100%3/3
$elem->content eq ";" and Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_elem_is_try_block($elem->parent)
!ll&&!rl&&r
92376
MC/DC100%2/2
$elem->content eq ";" and Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_elem_is_try_block($elem->parent)
$elem->content eq ";" Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_elem_is_try_block($elem->parent)
Truth table100%3/3
A and B
A= $elem->content eq ";"B= Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_elem_is_try_block($elem->parent)
ABresult
0X0
100
111
107 && Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon::_elem_is_try_block($elem->parent))
108 || return 0;
109  
110 # ppidump "try {} foo(123);" gives
111 # PPI::Statement
112 # PPI::Token::Word 'try'
113 # PPI::Structure::Block { ... }
114 # PPI::Token::Word 'foo'
115 # PPI::Structure::List ( ... )
116 # PPI::Statement::Expression
117 # PPI::Token::Number '123'
118 # PPI::Token::Structure ';'
119 6 for (;;) {
120 15 ▶ $elem = $elem->sprevious_sibling || return 1;
Condition100%2/2
$elem->sprevious_sibling || (return 1)
l!l
114
MC/DC100%1/1
$elem->sprevious_sibling || (return 1)
$elem->sprevious_sibling
Truth table100%2/2
A
A= $elem->sprevious_sibling
Aresult
11
00
121 11 ▶ $elem->isa('PPI::Structure::Block') || return 0;
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Structure::Block") or return 0 9 2 11
122  
123 9 ▶ $elem = $elem->sprevious_sibling || return 0;
Condition50%1/2
$elem->sprevious_sibling || (return 0)
l!l
90
MC/DC0%0/1
$elem->sprevious_sibling || (return 0)
$elem->sprevious_sibling
Truth table50%1/2
A
A= $elem->sprevious_sibling
Aresult
11
00
124 9 ▶ ($elem->isa('PPI::Token::Word') && $is_try_catch_keyword{$elem->content})
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Word") and $is_try_catch_keyword{$elem->content} or return 0 9 0 9
Condition33%1/3
$elem->isa("PPI::Token::Word") and $is_try_catch_keyword{$elem->content}
!ll&&!rl&&r
009
MC/DC0%0/2
$elem->isa("PPI::Token::Word") and $is_try_catch_keyword{$elem->content}
$elem->isa("PPI::Token::Word") $is_try_catch_keyword{$elem->content}
Truth table33%1/3
A and B
A= $elem->isa("PPI::Token::Word")B= $is_try_catch_keyword{$elem->content}
ABresult
0X0
100
111
125 || return 0;
126 }
127 }
128  
129 # _is_block_disambiguator($elem) takes a PPI::Statement::Null $elem and
130 # returns true if it's at the start of a "map {; ...}" or "grep {; ...}"
131 #
132 # PPI structure like the following, with the Whitespace optional of course,
133 # and allow comments in there too in case someone wants to write "# force
134 # block" or something
135 #
136 # PPI::Token::Word 'map'
137 # PPI::Token::Whitespace ' '
138 # PPI::Structure::Block { ... }
139 # PPI::Token::Whitespace ' '
140 # PPI::Statement::Null
141 # PPI::Token::Structure ';'
142 #
143 15 ▶sub _is_block_disambiguator {
Subroutine _is_block_disambiguator: called CC=9 SCAR=22.1
144 15 my ($elem) = @_;
145  
146 15 my $block = $elem->parent;
147 15 ▶ $block ->isa('PPI::Structure::Block')
Branch: 1 branch
BranchTrueFalseTotal
$block->isa("PPI::Structure::Block") or return 0 10 5 15
148 or return 0; # not in a block
149  
150 # not "sprevious" here, don't want to skip other null statements, just
151 # whitespace and comments
152 10 my $prev = $elem->previous_sibling;
153 10 ▶ while ($prev && ($prev->isa ('PPI::Token::Whitespace')
Branch: 1 branch
BranchTrueFalseTotal
if ($prev and $prev->isa("PPI::Token::Whitespace") || $prev->isa("PPI::Token::Comment")) 15 10 25
Condition100%6/6
$prev and $prev->isa("PPI::Token::Whitespace") || $prev->isa("PPI::Token::Comment")
!ll&&!rl&&r
7315
$prev->isa("PPI::Token::Whitespace") or $prev->isa("PPI::Token::Comment")
l!l&&r!l&&!r
1233
MC/DC100%3/3
$prev and $prev->isa("PPI::Token::Whitespace") || $prev->isa("PPI::Token::Comment")
$prev $prev->isa("PPI::Token::Whitespace") $prev->isa("PPI::Token::Comment")
Truth table100%4/4
A and B or C
A= $prevB= $prev->isa("PPI::Token::Whitespace")C= $prev->isa("PPI::Token::Comment")
ABCresult
0XX0
1000
11X1
1011
154 || $prev->isa ('PPI::Token::Comment'))) {
155 15 $prev = $prev->previous_sibling;
156 }
157 10 ▶ if ($prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev) 3 7 10
158 3 return 0; # not at the start of the block
159 }
160  
161 7 ▶ my $word = $block->sprevious_sibling
Branch: 1 branch
BranchTrueFalseTotal
my $word = $block->sprevious_sibling or return 0 7 0 7
162 or return 0; # nothing preceding the block
163 7 ▶ $word->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
$word->isa("PPI::Token::Word") or return 0 7 0 7
164 or return 0;
165 7 my $content = $word->content;
166 7 ▶ return ($content eq 'map' || $content eq 'grep');
Condition66%2/3
$content eq 'map' || $content eq 'grep'
l!l&&r!l&&!r
610
MC/DC0%0/2
$content eq 'map' || $content eq 'grep'
$content eq 'map' $content eq 'grep'
Truth table66%2/3
A || B
A= $content eq 'map'B= $content eq 'grep'
ABresult
1X1
011
000
167 }
168  
169 # _is_perl4_semihash($elem) takes a PPI::Statement::Null $elem and returns
170 # true if it's a Perl 4 style start-of-line ";# comment ..."
171 #
172 # When at the very start of a document,
173 #
174 # PPI::Document
175 # PPI::Statement::Null
176 # PPI::Token::Structure ';'
177 # PPI::Token::Comment '# foo'
178 #
179 # When in the middle,
180 #
181 # PPI::Token::Whitespace '\n'
182 # PPI::Statement::Null
183 # PPI::Token::Structure ';'
184 # PPI::Token::Comment '# hello'
185 #
186 4 ▶sub _is_perl4_semihash {
Subroutine _is_perl4_semihash: called CC=3 SCAR=11.0
187 4 my ($elem) = @_;
188  
189 # must be at the start of the line
190 # though not sure about this, the pl2pm program allows whitespace before
191 4 ▶ ($elem->location->[1] == 1)
Branch: 1 branch
BranchTrueFalseTotal
$elem->location->[1] == 1 or return 0 3 1 4
192 or return 0;
193  
194 # must be immediately followed by a comment
195 3 my $next = $elem->next_sibling;
196 3 ▶ return ($next && $next->isa('PPI::Token::Comment'));
Condition66%2/3
$next && $next->isa('PPI::Token::Comment')
!ll&&!rl&&r
012
MC/DC50%1/2
$next && $next->isa('PPI::Token::Comment')
$next $next->isa('PPI::Token::Comment')
Truth table66%2/3
A && B
A= $nextB= $next->isa('PPI::Token::Comment')
ABresult
0X0
100
111
197 }
198  
199  
200 40 1;
201 __END__
« Policy/ValuesAndExpressions/ProhibitArrayAssignAref.pm ↑ Summary Pulp/Utils.pm »