Policy/CodeLayout/RequireFinalSemicolon.pm

filter: Statementstmt 92.5% 136 / 147 Branchbran 92.1% 59 / 64 Conditioncond 68.1% 60 / 88 MC/DCmcdc 44.1% 19 / 43 Subroutinesub 92.5% 25 / 27 Podpod 100% 1 / 1 totaltotal 81.0% 300 / 370 SCAR 43.4
CC
64
Cov
85%
_elem_skip_whitespace_and_comments
20.0
_block_is_hash_constructor
16.4
violates
13.4
CRAP 77.1
« Policy/Compatibility/PerlMinimumVersionAndWhy.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitUnknownBackslash.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2  
3 # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 3, or (at your option) any later
6 # version.
7 #
8 # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
15  
16 package Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon;
17 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use Perl::Critic::Pulp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use Perl::Critic::Pulp::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # uncomment this to run the ### lines
28 # use Smart::Comments;
29  
30 40 our $VERSION = 99;
31  
32 40 use constant supported_parameters =>
33 ({ name => 'except_same_line',
34 description => 'Whether to allow no semicolon at the end of blocks with the } closing brace on the same line as the last statement.',
35 behavior => 'boolean',
36 default_string => '1',
37 },
38 { name => 'except_expression_blocks',
39 description => 'Whether to allow no semicolon at the end of do{} expression blocks.',
40 behavior => 'boolean',
41 default_string => '1',
42 40 ▶ });
Subroutine BEGIN: called CC=1 SCAR=0.0
43 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 40 ▶use constant applies_to => 'PPI::Structure::Block';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 sub violates {
48 98 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=13 SCAR=25.9
Pod documented
49 ### RequireFinalSemicolon elem: $elem->content
50  
51 98 ▶ if (_block_is_hash_constructor($elem) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (_block_is_hash_constructor($elem) != 0) 10 88 98
52 ### hash constructor, or likely so, stop ...
53 10 return;
54 }
55  
56 88 ▶ my $block_last = $elem->schild(-1)
Condition100%2/2
$elem->schild(-1) || (return)
l!l
826
MC/DC100%1/1
$elem->schild(-1) || (return)
$elem->schild(-1)
Truth table100%2/2
A
A= $elem->schild(-1)
Aresult
11
00
57 || return; # empty block doesn't need a semi
58  
59 ### block_last: ref($block_last),$block_last->content
60 82 ▶ $block_last->isa('PPI::Statement') || do {
Branch: 1 branch
BranchTrueFalseTotal
$block_last->isa("PPI::Statement") or do { return } 0 82 82
61 ### last in block is not a PPI-Statement ...
62 0 return;
63 };
64 82 ▶ if (_elem_statement_no_need_semicolon($block_last)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elem_statement_no_need_semicolon($block_last)) 11 71 82
65 11 return;
66 }
67  
68 {
69 71 ▶ my $bstat_last = $block_last->schild(-1)
Condition50%1/2
$block_last->schild(-1) || (return)
l!l
710
MC/DC0%0/1
$block_last->schild(-1) || (return)
$block_last->schild(-1)
Truth table50%1/2
A
A= $block_last->schild(-1)
Aresult
11
00
70 || return; # statement shouldn't be empty, should it?
71 ### bstat_last in statement: ref($bstat_last),$bstat_last->content
72  
73 71 ▶ if (_elem_is_semicolon($bstat_last)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elem_is_semicolon($bstat_last)) 10 61 71
74 ### has final semicolon, ok ...
75 10 return;
76 }
77 }
78  
79 61 ▶ if ($self->{'_except_expression_blocks'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_except_expression_blocks"}) 61 0 61
80 61 ▶ if (_block_is_expression($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_block_is_expression($elem)) 13 48 61
81 ### do expression, ok
82 13 return;
83 }
84 ### not a do{} expression
85 }
86  
87 # if don't have final brace then this option doesn't apply as there's no
88 # final brace to be on the same line
89 48 ▶ if ($self->{'_except_same_line'} && $elem->complete) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_except_same_line"} and $elem->complete) 46 2 48
Condition66%2/3
$self->{"_except_same_line"} and $elem->complete
!ll&&!rl&&r
0246
MC/DC50%1/2
$self->{"_except_same_line"} and $elem->complete
$self->{"_except_same_line"} $elem->complete
Truth table66%2/3
A and B
A= $self->{"_except_same_line"}B= $elem->complete
ABresult
0X0
100
111
90 46 ▶ if (! _newline_in_following_sibling($block_last)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_newline_in_following_sibling($block_last)) 25 21 46
91 ### no newline before close, ok
92 25 return;
93 }
94 }
95  
96 23 ▶ my $report_at = $block_last->next_sibling || $block_last;
Condition66%2/3
$block_last->next_sibling || $block_last
l!l&&r!l&&!r
2210
MC/DC0%0/2
$block_last->next_sibling || $block_last
$block_last->next_sibling $block_last
Truth table66%2/3
A || B
A= $block_last->next_siblingB= $block_last
ABresult
1X1
011
000
97 23 return $self->violation
98 ('Put semicolon ; on last statement in a block',
99 '',
100 $report_at);
101 }
102  
103 # return true if $elem is a PPI::Statement subclass which doesn't require a
104 # terminating ";"
105 sub _elem_statement_no_need_semicolon {
106 82 ▶ my ($elem) = @_;
Subroutine _elem_statement_no_need_semicolon: called CC=8 SCAR=21.6
107 82 ▶ return ($elem->isa('PPI::Statement::Compound') # for(){} etc
Condition76%16/21
$elem->isa('PPI::Statement::Compound') || $elem->isa('PPI::Statement::Sub') || $elem->isa('PPI::Statement::Given') || $elem->isa('PPI::Statement::When') || $elem->isa('PPI::Statement::End') || $elem->isa('PPI::Statement::Null') || $elem->isa('PPI::Statement::UnmatchedBrace') || _elem_is_try_block($elem)
l!l&&r!l&&!r
6571
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When") or $elem->isa("PPI::Statement::End") or $elem->isa("PPI::Statement::Null") or $elem->isa("PPI::Statement::UnmatchedBrace")
l!l&&r!l&&!r
6076
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When") or $elem->isa("PPI::Statement::End") or $elem->isa("PPI::Statement::Null")
l!l&&r!l&&!r
6076
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When") or $elem->isa("PPI::Statement::End")
l!l&&r!l&&!r
5176
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given") or $elem->isa("PPI::Statement::When")
l!l&&r!l&&!r
5077
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub") or $elem->isa("PPI::Statement::Given")
l!l&&r!l&&!r
5077
$elem->isa("PPI::Statement::Compound") or $elem->isa("PPI::Statement::Sub")
l!l&&r!l&&!r
5077
MC/DC37%3/8
$elem->isa('PPI::Statement::Compound') || $elem->isa('PPI::Statement::Sub') || $elem->isa('PPI::Statement::Given') || $elem->isa('PPI::Statement::When') || $elem->isa('PPI::Statement::End') || $elem->isa('PPI::Statement::Null') || $elem->isa('PPI::Statement::UnmatchedBrace') || _elem_is_try_block($elem)
$elem->isa("PPI::Statement::Compound") $elem->isa("PPI::Statement::Sub") $elem->isa("PPI::Statement::Given") $elem->isa("PPI::Statement::When") $elem->isa("PPI::Statement::End") $elem->isa("PPI::Statement::Null") $elem->isa("PPI::Statement::UnmatchedBrace") _elem_is_try_block($elem)
Truth table44%4/9
A or B or C or D or E or F or G || H
A= $elem->isa("PPI::Statement::Compound")B= $elem->isa("PPI::Statement::Sub")C= $elem->isa("PPI::Statement::Given")D= $elem->isa("PPI::Statement::When")E= $elem->isa("PPI::Statement::End")F= $elem->isa("PPI::Statement::Null")G= $elem->isa("PPI::Statement::UnmatchedBrace")H= _elem_is_try_block($elem)
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
108 || $elem->isa('PPI::Statement::Sub') # nested named sub
109 || $elem->isa('PPI::Statement::Given')
110 || $elem->isa('PPI::Statement::When')
111 || $elem->isa('PPI::Statement::End') # __END__
112 || $elem->isa('PPI::Statement::Null') # ;
113 || $elem->isa('PPI::Statement::UnmatchedBrace') # stray }
114 || _elem_is_try_block($elem)
115 );
116 }
117  
118 40 my %postfix_loops = (while => 1, until => 1);
119  
120 my %prefix_expressions = (do => 1,
121 map => 1,
122 grep => 1,
123 sort => 1,
124  
125 1640 map { $_ => 1, "List::Util::$_" => 1 }
126 qw(
127 reduce any all none notall first
128 pairfirst pairgrep pairmap
129 ),
130  
131 40 map { $_ => 1, "List::Pairwise::$_" => 1 }
132 qw(
133 mapp map_pairwise grepp grep_pairwise
134 firstp first_pairwise lastp last_pairwise
135 ),
136 );
137  
138 # $elem is a PPI::Structure::Block.
139 # return 1 definitely a hash
140 # 0 definitely a block
141 # -1 not certain
142 #
143 # PPI 1.212 tends to be give PPI::Structure::Block for various things which
144 # are actually anon hash constructors and ought to be
145 # PPI::Structure::Constructor. For example,
146 #
147 # return bless { x => 123 };
148 # return \ { x => 123 };
149 #
150 # _block_is_hash_constructor() tries to recognise some of those blocks which
151 # are actually hash constructors, so as not to apply the final semicolon
152 # rule to hash constructors.
153 #
154 40 my %word_is_block = (sub => 1,
155 do => 1,
156 map => 1,
157 grep => 1,
158 sort => 1,
159  
160 # from Try.pm, TryCatch.pm, Try::Tiny prototypes, etc
161 try => 1,
162 catch => 1,
163 finally => 1,
164  
165 # List::Util first() etc are not of interest to
166 # RequireFinalSemicolon but ProhibitDuplicateHashKeys
167 # shares this code so recognise them for it.
168 %prefix_expressions,
169 );
170 sub _block_is_hash_constructor {
171 109 ▶ my ($elem) = @_;
Subroutine _block_is_hash_constructor: called CC=16 SCAR=28.0
172 ### _block_is_hash_constructor(): ref($elem), "$elem"
173  
174 # if (_block_starts_semi($elem)) {
175 # ### begins with ";", block is correct ...
176 # return 0;
177 # }
178 109 ▶ if (_block_has_multiple_statements($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_block_has_multiple_statements($elem)) 7 102 109
179 ### contains one or more ";", block is correct ...
180 7 return 0;
181 }
182  
183 102 ▶ if (my $prev = $elem->sprevious_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $elem->sprevious_sibling) 95 7 102
184 ### prev: ref($prev), "$prev"
185 95 ▶ if ($prev->isa('PPI::Structure::Condition')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Structure::Condition")) 6 89 95
186 ### prev condition, block is correct ...
187 6 return 0;
188 }
189 89 ▶ if ($prev->isa('PPI::Token::Cast')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Cast")) 9 80 89
190 9 ▶ if ($prev eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq '\\') { } 8 1 9
191 ### ref cast, is a hash ...
192 8 return 1;
193 } else {
194 ### other cast, block is correct (or a variable name) ...
195 1 return 0;
196 }
197 }
198 80 ▶ if ($prev->isa('PPI::Token::Operator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Operator")) 0 80 80
199 ### prev operator, is a hash ...
200 0 return 1;
201 }
202 80 ▶ if (! $prev->isa('PPI::Token::Word')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev->isa("PPI::Token::Word")) 2 78 80
203 ### prev not a word, not sure ...
204 2 return -1;
205 }
206  
207 78 ▶ if ($word_is_block{$prev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word_is_block{$prev}) 49 29 78
208 # "sub { ... }"
209 # "do { ... }"
210 ### do/sub/map/grep/sort, block is correct ...
211 49 return 0;
212 }
213  
214 29 ▶ if (! ($prev = $prev->sprevious_sibling)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev = $prev->sprevious_sibling) 3 26 29
215 # "bless { ... }"
216 # "return { ... }" etc
217 # ENHANCE-ME: notice List::Util first{} and other prototyped things
218 ### nothing else preceding, likely a hash ...
219 3 return -1;
220 }
221 ### prev prev: "$prev"
222  
223 26 ▶ if ($prev eq 'sub') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev eq "sub") 25 1 26
224 # "sub foo {}"
225 ### named sub, block is correct ...
226 25 return 0;
227 }
228 # "word bless { ... }"
229 # "word return { ... }" etc
230 ### other word preceding, likely a hash ...
231 1 return -1;
232 }
233  
234 7 ▶ my $parent = $elem->parent || do {
Condition33%1/3
$elem->parent || do {
l!l&&r!l&&!r
700
MC/DC0%0/2
$elem->parent || do { return 0 }
$elem->parent do { return 0 }
Truth table33%1/3
A || B
A= $elem->parentB= do { return 0 }
ABresult
1X1
011
000
235 ### umm, at toplevel, is a block ...
236 0 return 0;
237 };
238  
239 7 ▶ if ($parent->isa('PPI::Statement::Compound')
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Statement::Compound") and $parent = $parent->parent and $parent->isa("PPI::Structure::List") || $parent->isa("PPI::Structure::Constructor")) 2 5 7
Condition77%7/9
$parent->isa("PPI::Statement::Compound") and $parent = $parent->parent and $parent->isa("PPI::Structure::List") || $parent->isa("PPI::Structure::Constructor")
!ll&&!rl&&r
142
$parent->isa("PPI::Statement::Compound") and $parent = $parent->parent
!ll&&!rl&&r
106
$parent->isa("PPI::Structure::List") or $parent->isa("PPI::Structure::Constructor")
l!l&&r!l&&!r
024
MC/DC50%2/4
$parent->isa("PPI::Statement::Compound") and $parent = $parent->parent and $parent->isa("PPI::Structure::List") || $parent->isa("PPI::Structure::Constructor")
$parent->isa("PPI::Statement::Compound") $parent = $parent->parent $parent->isa("PPI::Structure::List") $parent->isa("PPI::Structure::Constructor")
Truth table60%3/5
A and B and C or D
A= $parent->isa("PPI::Statement::Compound")B= $parent = $parent->parentC= $parent->isa("PPI::Structure::List")D= $parent->isa("PPI::Structure::Constructor")
ABCDresult
0XXX0
10XX0
11000
111X1
11011
240 && ($parent = $parent->parent)
241 && (
242 $parent->isa('PPI::Structure::List')
243 ||
244 $parent->isa('PPI::Structure::Constructor'))) {
245 ### in a list or arrayref, is a hashref ...
246  
247 # This catches
248 # ppidump "[{%args}]"
249 # which comes out (from PPI 1.270) as
250 #
251 # PPI::Structure::Constructor [ ... ]
252 # PPI::Statement::Compound
253 # PPI::Structure::Block { ... }
254 # PPI::Statement
255 # PPI::Token::Symbol '%args'
256 #
257 # It should be like
258 #
259 # PPI::Structure::Constructor [ ... ]
260 # PPI::Statement
261 # PPI::Structure::Constructor { ... }
262 # PPI::Statement::Expression
263 #
264 # which is what ppidump "[{x=>1}]" gives.
265 #
266 # The PPI::Structure::List is for something like
267 # ppidump "func({ %args })"
268 # which in for example PPI 1.220 was PPI::Structure::Block too.
269 # That one is ok in PPI 1.270 (when ready to demand that version).
270 #
271 # The plan would be to remove the whole of this check for
272 # PPI::Statement::Compound if PPI can do the right thing on arrayrefs
273 # too ...
274  
275 2 return 1;
276 }
277  
278 5 return 0;
279 }
280  
281 # $elem is a PPI::Structure::Block
282 # return true if it contains two or more PPI::Statement
283 #
284 sub _block_has_multiple_statements {
285 109 ▶ my ($elem) = @_;
Subroutine _block_has_multiple_statements: called CC=3 SCAR=11.0
286 109 my $count = 0;
287 109 foreach my $child ($elem->schildren) {
288 108 $count++;
289 108 ▶ if ($count >= 2) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($count >= 2) 7 101 108
290 }
291 102 return 0;
292 }
293  
294 # $elem is a PPI::Structure::Block
295 # return true if it starts with a ";"
296 #
297 sub _block_starts_semi {
298 0 ▶ my ($elem) = @_;
Subroutine _block_starts_semi: not called CC=2 SCAR=17.9
299  
300 # note child() not schild() since an initial ";" is not "significant"
301 0 $elem = $elem->child(0);
302 ### first child: $elem && (ref $elem)." $elem"
303  
304 0 $elem = _elem_skip_whitespace_and_comments($elem);
305 0 ▶ return ($elem && $elem->isa('PPI::Statement::Null'));
Condition0%0/3
$elem && $elem->isa('PPI::Statement::Null')
!ll&&!rl&&r
000
MC/DC0%0/2
$elem && $elem->isa('PPI::Statement::Null')
$elem $elem->isa('PPI::Statement::Null')
Truth table0%0/3
A && B
A= $elemB= $elem->isa('PPI::Statement::Null')
ABresult
0X0
100
111
306 }
307  
308 # $elem is a PPI::Element or undef
309 # return the next non-whitespace and non-comment after it
310 sub _elem_skip_whitespace_and_comments {
311 0 ▶ my ($elem) = @_;
Subroutine _elem_skip_whitespace_and_comments: not called CC=4 SCAR=30.0
312 0 ▶ while ($elem
Branch: 1 branch
BranchTrueFalseTotal
if ($elem and $elem->isa("PPI::Token::Whitespace") || $elem->isa("PPI::Token::Comment")) 0 0 0
Condition0%0/6
$elem and $elem->isa("PPI::Token::Whitespace") || $elem->isa("PPI::Token::Comment")
!ll&&!rl&&r
000
$elem->isa("PPI::Token::Whitespace") or $elem->isa("PPI::Token::Comment")
l!l&&r!l&&!r
000
MC/DC0%0/3
$elem and $elem->isa("PPI::Token::Whitespace") || $elem->isa("PPI::Token::Comment")
$elem $elem->isa("PPI::Token::Whitespace") $elem->isa("PPI::Token::Comment")
Truth table0%0/4
A and B or C
A= $elemB= $elem->isa("PPI::Token::Whitespace")C= $elem->isa("PPI::Token::Comment")
ABCresult
0XX0
1000
11X1
1011
313 && ($elem->isa('PPI::Token::Whitespace')
314 || $elem->isa ('PPI::Token::Comment'))) {
315 0 $elem = $elem->next_sibling;
316 ### next elem: $elem && (ref $elem)." $elem"
317 }
318 0 return $elem;
319 }
320  
321 sub _elem_is_semicolon {
322 71 ▶ my ($elem) = @_;
Subroutine _elem_is_semicolon: called CC=2 SCAR=7.1
323 71 ▶ return ($elem->isa('PPI::Token::Structure') && $elem eq ';');
Condition66%2/3
$elem->isa('PPI::Token::Structure') && $elem eq ';'
!ll&&!rl&&r
61010
MC/DC50%1/2
$elem->isa('PPI::Token::Structure') && $elem eq ';'
$elem->isa('PPI::Token::Structure') $elem eq ';'
Truth table66%2/3
A && B
A= $elem->isa('PPI::Token::Structure')B= $elem eq ';'
ABresult
0X0
100
111
324 }
325  
326 # $elem is a PPI::Node
327 # return true if any following sibling (not $elem itself) contains a newline
328 sub _newline_in_following_sibling {
329 46 ▶ my ($elem) = @_;
Subroutine _newline_in_following_sibling: called CC=3 SCAR=11.0
330 46 ▶ while ($elem = $elem->next_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->next_sibling) 52 25 77
331 52 ▶ if ($elem =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem =~ /\n/) 21 31 52
332 21 return 1;
333 }
334 }
335 25 return 0;
336 }
337  
338 # $block is a PPI::Structure::Block
339 # return true if it's "do{}" expression, and not a "do{}while" or "do{}until"
340 # loop
341 sub _block_is_expression {
342 61 ▶ my ($elem) = @_;
Subroutine _block_is_expression: called CC=6 SCAR=17.9
343 ### _block_is_expression(): "$elem"
344  
345 61 ▶ if (my $next = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $elem->snext_sibling) 25 36 61
346 25 ▶ if ($next->isa('PPI::Token::Word')
Branch: 1 branch
BranchTrueFalseTotal
if ($next->isa("PPI::Token::Word") and $postfix_loops{$next}) 6 19 25
Condition100%3/3
$next->isa("PPI::Token::Word") and $postfix_loops{$next}
!ll&&!rl&&r
9106
MC/DC100%2/2
$next->isa("PPI::Token::Word") and $postfix_loops{$next}
$next->isa("PPI::Token::Word") $postfix_loops{$next}
Truth table100%3/3
A and B
A= $next->isa("PPI::Token::Word")B= $postfix_loops{$next}
ABresult
0X0
100
111
347 && $postfix_loops{$next}) {
348 ### {}while or {}until, not an expression
349 6 return 0;
350 }
351 }
352  
353 ### do, map, grep, sort, etc are expressions ..
354 55 my $prev = $elem->sprevious_sibling;
355 return ($prev
356 && $prev->isa('PPI::Token::Word')
357 55 ▶ && $prefix_expressions{$prev});
Condition83%5/6
$prev && $prev->isa('PPI::Token::Word') && $prefix_expressions{$prev}
!ll&&!rl&&r
63613
$prev and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
6049
MC/DC66%2/3
$prev && $prev->isa('PPI::Token::Word') && $prefix_expressions{$prev}
$prev $prev->isa("PPI::Token::Word") $prefix_expressions{$prev}
Truth table75%3/4
A and B && C
A= $prevB= $prev->isa("PPI::Token::Word")C= $prefix_expressions{$prev}
ABCresult
0XX0
10X0
1100
1111
358 }
359  
360 # Return true if $elem is a "try" block like
361 # Try.pm try { } catch {}
362 # TryCatch.pm try { } catch ($err) {} ... catch {}
363 # Syntax::Feature::Try try { } catch ($err) {} ... catch {} finally {}
364 # The return is true only for the block type "try"s of these three modules.
365 # "try" forms from Try::Tiny and its friends are plain subroutine calls
366 # rather than blocks.
367 #
368 sub _elem_is_try_block {
369 119 ▶ my ($elem) = @_;
Subroutine _elem_is_try_block: called CC=5 SCAR=16.6
370 119 ▶ return ($elem->isa('PPI::Statement')
Condition75%9/12
$elem->isa('PPI::Statement') && ($elem = $elem->schild(0)) && $elem->isa('PPI::Token::Word') && $elem->content eq 'try' && _elem_has_preceding_use_trycatch($elem)
!ll&&!rl&&r
103511
$elem->isa("PPI::Statement") and $elem = $elem->schild(0) and $elem->isa("PPI::Token::Word") and $elem->content eq "try"
!ll&&!rl&&r
485516
$elem->isa("PPI::Statement") and $elem = $elem->schild(0) and $elem->isa("PPI::Token::Word")
!ll&&!rl&&r
04871
$elem->isa("PPI::Statement") and $elem = $elem->schild(0)
!ll&&!rl&&r
00119
MC/DC60%3/5
$elem->isa('PPI::Statement') && ($elem = $elem->schild(0)) && $elem->isa('PPI::Token::Word') && $elem->content eq 'try' && _elem_has_preceding_use_trycatch($elem)
$elem->isa("PPI::Statement") $elem = $elem->schild(0) $elem->isa("PPI::Token::Word") $elem->content eq "try" _elem_has_preceding_use_trycatch($elem)
Truth table66%4/6
A and B and C and D && E
A= $elem->isa("PPI::Statement")B= $elem = $elem->schild(0)C= $elem->isa("PPI::Token::Word")D= $elem->content eq "try"E= _elem_has_preceding_use_trycatch($elem)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
371 && ($elem = $elem->schild(0))
372 && $elem->isa('PPI::Token::Word')
373 && $elem->content eq 'try'
374 && _elem_has_preceding_use_trycatch($elem));
375 }
376  
377 # return true if $elem is preceded by any of
378 # use Try
379 # use TryCatch
380 # use syntax 'try'
381 sub _elem_has_preceding_use_trycatch {
382 16 ▶ my ($elem) = @_;
Subroutine _elem_has_preceding_use_trycatch: called CC=1 SCAR=0.0
383 16 my $ret = 0;
384 16 my $document = $elem->top; # PPI::Document, not Perl::Critic::Document
385 $document->find_first (sub {
386 674 ▶ my ($doc, $e) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
387 # ### comment: (ref $e)." ".$e->content
388 674 ▶ if ($e == $elem) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e == $elem) 16 658 674
389 ### not found before target elem, stop ...
390 16 return undef;
391 }
392 658 ▶ if (_elem_is_use_try($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_elem_is_use_try($e)) 11 647 658
393 ### found "use Try" etc, stop ...
394 11 $ret = 1;
395 11 return undef;
396 }
397 647 return 0; # continue
398 16 });
399 16 return $ret;
400 }
401  
402 sub _elem_is_use_try {
403 658 ▶ my ($elem) = @_;
Subroutine _elem_is_use_try: called CC=6 SCAR=18.0
404 658 ▶ ($elem->isa('PPI::Statement::Include') && $elem->type eq 'use')
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Statement::Include") and $elem->type eq "use" or return 0 642 16 658
Condition66%2/3
$elem->isa("PPI::Statement::Include") and $elem->type eq "use"
!ll&&!rl&&r
642016
MC/DC50%1/2
$elem->isa("PPI::Statement::Include") and $elem->type eq "use"
$elem->isa("PPI::Statement::Include") $elem->type eq "use"
Truth table66%2/3
A and B
A= $elem->isa("PPI::Statement::Include")B= $elem->type eq "use"
ABresult
0X0
100
111
405 or return 0;
406 16 my $module = $elem->module;
407 16 ▶ return ($module eq 'Try'
Condition88%8/9
$module eq 'Try' || $module eq 'TryCatch' || $module eq 'syntax' && _syntax_has_feature($elem, 'try')
l!l&&r!l&&!r
925
$module eq "Try" or $module eq "TryCatch"
l!l&&r!l&&!r
367
$module eq "syntax" and _syntax_has_feature($elem, "try")
!ll&&!rl&&r
502
MC/DC75%3/4
$module eq 'Try' || $module eq 'TryCatch' || $module eq 'syntax' && _syntax_has_feature($elem, 'try')
$module eq "Try" $module eq "TryCatch" $module eq "syntax" _syntax_has_feature($elem, "try")
Truth table80%4/5
A or B || C and D
A= $module eq "Try"B= $module eq "TryCatch"C= $module eq "syntax"D= _syntax_has_feature($elem, "try")
ABCDresult
1XXX1
01XX1
00111
000X0
00100
408 || $module eq 'TryCatch'
409 || ($module eq 'syntax'
410 && _syntax_has_feature($elem,'try')));
411 }
412  
413 # $elem is a PPI::Statement::Include of "use syntax".
414 # Return true if $feature (a string) is among the feature names it imports.
415 sub _syntax_has_feature {
416 6 ▶ my ($elem, $feature) = @_;
Subroutine _syntax_has_feature: called CC=1 SCAR=0.0
417 6 return ((grep {$_ eq $feature} _syntax_feature_list($elem)) > 0);
418 }
419  
420 # $elem is a PPI::Statement::Include of "use syntax".
421 # Return a list of the feature names it imports.
422 sub _syntax_feature_list {
423 9 ▶ my ($elem) = @_;
Subroutine _syntax_feature_list: called CC=5 SCAR=16.1
424 ### _syntax_feature_list(): $elem && ref $elem
425 9 my @ret;
426 9 ▶ for ($elem = $elem->schild(2); $elem; $elem = $elem->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 33 9 42
427 33 ▶ if ($elem->isa('PPI::Token::Word')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($elem->isa('PPI::Token::Word')) { } 5 28 33
elsif ($elem->isa('PPI::Token::QuoteLike::Words')) { } 3 25 28
elsif ($elem->isa('PPI::Token::Quote')) { } 5 20 25
428 5 push @ret, $elem->content;
429 } elsif ($elem->isa('PPI::Token::QuoteLike::Words')) {
430 3 push @ret, $elem->literal;
431 } elsif ($elem->isa('PPI::Token::Quote')) {
432 5 push @ret, $elem->string;
433 }
434 }
435 9 return @ret;
436 }
437  
438 40 1;
439 __END__
440  
441 =for stopwords boolean hashref eg Ryde
442  
443 =head1 NAME
444  
445 Perl::Critic::Policy::CodeLayout::RequireFinalSemicolon - require a semicolon at the end of code blocks
446  
447 =head1 DESCRIPTION
448  
449 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
450 add-on. It asks you to put a semicolon C<;> on the final statement of a
451 subroutine or block.
452  
453 sub foo {
454 do_something(); # ok
455 }
456  
457 sub bar {
458 do_something() # bad
459 }
460  
461 The idea is that if you add more code you don't have to notice the previous
462 line needs a terminator. It's also more like the C language, if you
463 consider that a virtue.
464  
465 This is only a matter of style since the code runs the same either way, and
466 on that basis this policy is low severity and under the "cosmetic" theme
467 (see L<Perl::Critic/POLICY THEMES>).
468  
469 =head2 Same Line Closing Brace
470  
471 By default (see L</CONFIGURATION> below), a semicolon is not required when
472 the closing brace is on the same line as the last statement. This is good
473 for constants and one-liners.
474  
475 sub foo { 'my-constant-value' } # ok
476  
477 sub square { return $_[0] ** 2 } # ok
478  
479 =head2 Final Value Expression
480  
481 A semicolon is not required in places where the last statement is an
482 expression giving a value.
483  
484 map { some_thing();
485 $_+123 # ok
486 } @values;
487  
488 do {
489 foo();
490 1+2+3 # ok
491 }
492  
493 This currently means
494  
495 do grep map sort # builtins
496  
497 reduce any all none notall first # List::Util
498 pairfirst pairgrep pairmap
499  
500 mapp map_pairwise grepp grep_pairwise # List::Pairwise
501 firstp first_pairwise lastp last_pairwise
502  
503 These module function names are always treated as expressions. There's no
504 check for whether the respective module is actually in use. Fully qualified
505 names like C<List::Util::first> are recognised too.
506  
507 C<do {} while> or C<do {} until> loops are ordinary blocks, not expression
508 blocks, so still require a semicolon on the last statement inside.
509  
510 do {
511 foo() # bad
512 } until ($condition);
513  
514 =head2 Try/Catch Blocks
515  
516 The C<Try>, C<TryCatch> and C<Syntax::Feature::Try> modules all add C<try>
517 block forms. These are blocks not requiring a terminating semicolon, the
518 same as an C<if> etc doesn't.
519  
520 use TryCatch;
521 sub foo {
522 try {
523 attempt_something();
524 } catch {
525 error_recovery();
526 } # ok, no semi required here for TryCatch
527 }
528  
529 The insides of the C<try> and C<catch> are the same as other blocks, but the
530 C<try> statement itself doesn't require a semicolon. (See policy
531 C<ValuesAndExpressions::ProhibitNullStatements> to notice one added
532 unnecessarily.)
533  
534 For reference, C<PPI> doesn't know C<try>/C<catch> specifically, so when
535 they don't have a final semicolon the next statement runs together and the
536 nature of those parts might be lost. This could upset things like
537 recognition of C<for> loops and could potentially make some perlcritic
538 reports go wrong.
539  
540 The C<try>/C<catch> block exemption here is only for the modules with this
541 block syntax. There are other try modules such as C<Try::Tiny> and friends
542 where a final semicolon is normal and necessary if more code follows
543 (because their C<try> and C<catch> are ordinary function calls prototyped to
544 take code blocks).
545  
546 use Try::Tiny;
547 sub foo {
548 try {
549 attempt_something();
550 } catch {
551 error_recovery();
552 } # bad, semi required here for Try::Tiny
553 }
554  
555 =head2 Disabling
556  
557 If you don't care about this you can always disable from your
558 F<.perlcriticrc> file in the usual way (see L<Perl::Critic/CONFIGURATION>),
559  
560 [-CodeLayout::RequireFinalSemicolon]
561  
562 =head1 CONFIGURATION
563  
564 =over 4
565  
566 =item C<except_same_line> (boolean, default true)
567  
568 If true (the default) then don't demand a semicolon if the closing brace is
569 on the same line as the final statement.
570  
571 sub foo { return 123 } # ok if "except_same_line=yes"
572 # bad if "except_same_line=no"
573  
574 =item C<except_expression_blocks> (boolean, default true)
575  
576 If true (the default) then don't demand a semicolon at the end of an
577 expression block, as described under L</Final Value Expression> above.
578  
579 # ok under "except_expression_blocks=yes"
580 # bad under "except_expression_blocks=no"
581 do { 1+2+3 }
582 map { $_+1 } @array
583 grep {defined} @x
584  
585 The statements and functions for this exception are currently hard coded.
586 Maybe in the future they could be configurable, though multi-line
587 expressions in this sort of thing tends to be unusual anyway. (See policy
588 C<BuiltinFunctions::RequireSimpleSortBlock> for example to demand C<sort> is
589 only one line.)
590  
591 =back
592  
593 =head1 BUGS
594  
595 It's very difficult to distinguish a code block from an anonymous hashref
596 constructor if there might be a function prototype in force, eg.
597  
598 foo { abc => 123 }; # hash ref normally
599 # code block if foo() has prototype
600  
601 C<PPI> tends to assume code. C<RequireFinalSemicolon> currently assumes
602 hashref so as to avoid false violations. Any C<try>, C<catch> or C<finally>
603 are presumed to be code blocks (the various Try modules). Perhaps other
604 common or particular functions or syntax with code blocks could be
605 recognised. In general this sort of ambiguity is another good reason to
606 avoid function prototypes.
607  
608 PPI as of its version 1.270 sometimes takes hashrefs in lists and arrarefs
609 to be code blocks, eg.
610  
611 ppidump 'foo({%y,x=>1})'
612 ppidump '[{%y,x=>1}]'
613  
614 ppidump '[{x=>1,%y}]' # ok, hash
615  
616  
617 =head1 SEE ALSO
618  
619 L<Perl::Critic::Pulp>,
620 L<Perl::Critic>,
621 L<Perl::Critic::Policy::CodeLayout::RequireTrailingCommas>,
622 L<Perl::Critic::Policy::CodeLayout::RequireTrailingCommaAtNewline>,
623 L<Perl::Critic::Policy::Subroutines::RequireFinalReturn>,
624 L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitNullStatements>,
625 L<Perl::Critic::Policy::BuiltinFunctions::RequireSimpleSortBlock>
626  
627 L<List::Util>, L<List::Pairwise>,
628 L<Try>, L<TryCatch>, L<Syntax::Feature::Try>
629  
630 =head1 HOME PAGE
631  
632 L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
633  
634 =head1 COPYRIGHT
635  
636 Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
637  
638 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
639 under the terms of the GNU General Public License as published by the Free
640 Software Foundation; either version 3, or (at your option) any later
641 version.
642  
643 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
644 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
645 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
646 more details.
647  
648 You should have received a copy of the GNU General Public License along with
649 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses>.
650  
651 =cut
« Policy/Compatibility/PerlMinimumVersionAndWhy.pm ↑ Summary Policy/ValuesAndExpressions/ProhibitUnknownBackslash.pm »