blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitCommaSeparatedStatements.pm

filter: Statementstmt 80.5% 58 / 72 Branchbran 52.2% 23 / 44 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 94.1% 16 / 17 Podpod 80.0% 4 / 5 totaltotal 71.9% 110 / 153 CC 27 SCAR 23.2
CC
27
Cov
70%
_is_direct_part_of_map_or_grep_block
30.0
violates
16.6
_is_last_statement_in_a_block
6.5
CRAP 10.2
« blib/lib/Perl/Critic/Policy/Documentation/RequirePackageMatchesPodName.pm ↑ Summary blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm »
1 package Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements;
2  
3 40 ▶use 5.010001;
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  
9 40 ▶use Perl::Critic::Utils qw{ :booleans :characters :severities :classification hashify };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Perl::Critic::Utils::PPI qw{ is_ppi_statement_subclass };
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 our $VERSION = '1.150';
15  
16 #-----------------------------------------------------------------------------
17  
18 40 Readonly::Scalar my $DESC => q{Comma used to separate statements};
19 40 Readonly::Scalar my $EXPL => [ 68, 71 ];
20  
21 #-----------------------------------------------------------------------------
22  
23 sub supported_parameters {
24 return (
25 {
26 91 ▶ name => 'allow_last_statement_to_be_comma_separated_in_map_and_grep',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
27 description => 'Allow map and grep blocks to return lists.',
28 default_string => $FALSE,
29 behavior => 'boolean',
30 },
31 );
32 }
33  
34 75 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
35 92 ▶sub default_themes { return qw( core bugs pbp certrule ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
36 33 ▶sub applies_to { return 'PPI::Statement' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
37  
38 #-----------------------------------------------------------------------------
39  
40 sub violates {
41 296 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=13 SCAR=28.1
Pod documented
42  
43 # Grrr... PPI instantiates non-leaf nodes in its class hierarchy...
44 296 ▶ return if is_ppi_statement_subclass($elem);
Branch: 1 branch
BranchTrueFalseTotal
if is_ppi_statement_subclass($elem) 205 91 296
45  
46 # Now, if PPI hasn't introduced any new PPI::Statement subclasses, we've
47 # got an element who's class really is PPI::Statement.
48  
49 91 ▶ return if _is_parent_a_constructor_or_list($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_parent_a_constructor_or_list($elem) 6 85 91
50 85 ▶ return if _is_parent_a_for_loop($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_parent_a_for_loop($elem) 0 85 85
51  
52 85 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"}) 0 85 85
53 $self->{_allow_last_statement_to_be_comma_separated_in_map_and_grep}
54 ) {
55 0 ▶ return if not _is_direct_part_of_map_or_grep_block($elem);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_direct_part_of_map_or_grep_block($elem) 0 0 0
56 }
57  
58 85 foreach my $child ( $elem->schildren() ) {
59 249 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"} or _is_last_statement_in_a_block($child)) 249 0 249
Condition33%1/3
$self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"} or _is_last_statement_in_a_block($child)
l!l&&r!l&&!r
00249
MC/DC0%0/2
$self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"} or _is_last_statement_in_a_block($child)
$self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"} _is_last_statement_in_a_block($child)
Truth table33%1/3
A or B
A= $self->{"_allow_last_statement_to_be_comma_separated_in_map_and_grep"}B= _is_last_statement_in_a_block($child)
ABresult
1X1
011
000
60 not $self->{_allow_last_statement_to_be_comma_separated_in_map_and_grep}
61 and not _is_last_statement_in_a_block($child)
62 ) {
63 249 ▶ if ( $child->isa('PPI::Token::Word') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child->isa('PPI::Token::Word')) { } 43 206 249
elsif ($child->isa('PPI::Token::Operator')) { } 32 174 206
64 43 ▶ return if _succeeding_commas_are_list_element_separators($child);
Branch: 1 branch
BranchTrueFalseTotal
if _succeeding_commas_are_list_element_separators($child) 20 23 43
65 }
66 elsif ( $child->isa('PPI::Token::Operator') ) {
67 32 ▶ if ( $child->content() eq $COMMA ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->content eq $Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements::COMMA) 0 32 32
68 0 return $self->violation($DESC, $EXPL, $elem);
69 }
70 }
71 }
72 }
73  
74 65 return;
75 }
76  
77 sub _is_parent_a_constructor_or_list {
78 91 ▶ my ($elem) = @_;
Subroutine _is_parent_a_constructor_or_list: called CC=3 SCAR=11.3
79  
80 91 my $parent = $elem->parent();
81  
82 91 ▶ return if not $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 91 91
83  
84 return (
85 91 ▶ $parent->isa('PPI::Structure::Constructor')
Condition66%2/3
$parent->isa('PPI::Structure::Constructor') || $parent->isa('PPI::Structure::List')
l!l&&r!l&&!r
0685
MC/DC50%1/2
$parent->isa('PPI::Structure::Constructor') || $parent->isa('PPI::Structure::List')
$parent->isa('PPI::Structure::Constructor') $parent->isa('PPI::Structure::List')
Truth table66%2/3
A || B
A= $parent->isa('PPI::Structure::Constructor')B= $parent->isa('PPI::Structure::List')
ABresult
1X1
011
000
86 or $parent->isa('PPI::Structure::List')
87 );
88 }
89  
90 sub _is_parent_a_for_loop {
91 85 ▶ my ($elem) = @_;
Subroutine _is_parent_a_for_loop: called CC=3 SCAR=11.0
92  
93 85 my $parent = $elem->parent();
94  
95 85 ▶ return if not $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 85 85
96  
97 85 ▶ return if not $parent->isa('PPI::Structure::For');
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("PPI::Structure::For") 79 6 85
98  
99 6 return 1 == scalar $parent->schildren(); # Multiple means C-style loop.
100 }
101  
102 sub _is_direct_part_of_map_or_grep_block {
103 0 ▶ my ($elem) = @_;
Subroutine _is_direct_part_of_map_or_grep_block: not called CC=5 SCAR=34.0
104  
105 0 my $parent = $elem->parent();
106 0 ▶ return if not $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
107 0 ▶ return if not $parent->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("PPI::Structure::Block") 0 0 0
108  
109 0 my $block_prior_sibling = $parent->sprevious_sibling();
110 0 ▶ return if not $block_prior_sibling;
Branch: 1 branch
BranchTrueFalseTotal
unless $block_prior_sibling 0 0 0
111 0 ▶ return if not $block_prior_sibling->isa('PPI::Token::Word');
Branch: 1 branch
BranchTrueFalseTotal
unless $block_prior_sibling->isa("PPI::Token::Word") 0 0 0
112  
113 0 state $is_map_or_grep = { hashify( qw( map grep ) ) };
114 0 return $is_map_or_grep->{$block_prior_sibling};
115 }
116  
117 sub _is_last_statement_in_a_block {
118 249 ▶ my ($elem) = @_;
Subroutine _is_last_statement_in_a_block: called CC=4 SCAR=18.7
119  
120 249 my $parent = $elem->parent();
121 249 ▶ return if not $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 249 249
122 249 ▶ return if not $parent->isa('PPI::Structure::Block');
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("PPI::Structure::Block") 249 0 249
123  
124 0 my $next_sibling = $elem->snext_sibling();
125 0 ▶ return if not $next_sibling;
Branch: 1 branch
BranchTrueFalseTotal
unless $next_sibling 0 0 0
126  
127 0 return 1;
128 }
129  
130 sub _succeeding_commas_are_list_element_separators {
131 43 ▶ my ($elem) = @_;
Subroutine _succeeding_commas_are_list_element_separators: called CC=4 SCAR=13.9
132  
133 43 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (is_perl_builtin_with_zero_and_or_one_arguments($elem) and not is_perl_builtin_with_multiple_arguments($elem)) 8 35 43
Condition100%3/3
is_perl_builtin_with_zero_and_or_one_arguments($elem) and not is_perl_builtin_with_multiple_arguments($elem)
!ll&&!rl&&r
3418
MC/DC100%2/2
is_perl_builtin_with_zero_and_or_one_arguments($elem) and not is_perl_builtin_with_multiple_arguments($elem)
is_perl_builtin_with_zero_and_or_one_arguments($elem) not is_perl_builtin_with_multiple_arguments($elem)
Truth table100%3/3
A and not(B)
A= is_perl_builtin_with_zero_and_or_one_arguments($elem)B= not is_perl_builtin_with_multiple_arguments($elem)
ABresult
0X0
100
111
134 is_perl_builtin_with_zero_and_or_one_arguments($elem)
135 and not is_perl_builtin_with_multiple_arguments($elem)
136 ) {
137 8 return;
138 }
139  
140 35 my $sibling = $elem->snext_sibling();
141  
142 35 ▶ return 1 if not $sibling; # There won't be any succeeding commas.
Branch: 1 branch
BranchTrueFalseTotal
unless $sibling 0 35 35
143  
144 35 return not $sibling->isa('PPI::Structure::List');
145 }
146  
147 40 1;
148  
149 __END__
150  
151 #-----------------------------------------------------------------------------
152  
153 =pod
154  
155 =head1 NAME
156  
157 Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements - Don't use the comma operator as a statement separator.
158  
159 =head1 AFFILIATION
160  
161 This Policy is part of the core L<Perl::Critic|Perl::Critic>
162 distribution.
163  
164  
165 =head1 DESCRIPTION
166  
167 Perl's comma statement separator has really low precedence, which
168 leads to code that looks like it's using the comma list element
169 separator not actually doing so. Conway suggests that the statement
170 separator not be used in order to prevent this situation.
171  
172 The confusion that the statement separator causes is primarily due to
173 the assignment operators having higher precedence.
174  
175 For example, trying to combine two arrays into another like this won't
176 work:
177  
178 @x = @y, @z;
179  
180 because it is equivalent to
181  
182 @x = @y;
183 @z;
184  
185 Conversely, there are the built-in functions, like C<print>, that
186 normally force the rest of the statement into list context, but don't
187 when called like a subroutine.
188  
189 This is not likely to produce what is intended:
190  
191 print join q{, }, 2, 3, 5, 7, ": the single-digit primes.\n";
192  
193 The obvious fix is to add parentheses. Placing them like
194  
195 print join( q{, }, 2, 3, 5, 7 ), ": the single-digit primes.\n";
196  
197 will work, but
198  
199 print ( join q{, }, 2, 3, 5, 7 ), ": the single-digit primes.\n";
200  
201 will not, because it is equivalent to
202  
203 print( join q{, }, 2, 3, 5, 7 );
204 ": the single-digit primes.\n";
205  
206  
207 =head1 CONFIGURATION
208  
209 This policy can be configured to allow the last statement in a C<map>
210 or C<grep> block to be comma separated. This is done via the
211 C<allow_last_statement_to_be_comma_separated_in_map_and_grep> option
212 like so:
213  
214 [ValuesAndExpressions::ProhibitCommaSeparatedStatements]
215 allow_last_statement_to_be_comma_separated_in_map_and_grep = 1
216  
217 With this option off (the default), the following code violates this
218 policy.
219  
220 %hash = map {$_, 1} @list;
221  
222 With this option on, this statement is allowed. Even if this option
223 is off, using a fat comma C<< => >> works, but that forces
224 stringification on the first value, which may not be what you want.
225  
226  
227 =head1 BUGS
228  
229 Needs to check for C<scalar( something, something )>.
230  
231  
232 =head1 AUTHOR
233  
234 Elliot Shank C<< <perl@galumph.com> >>
235  
236  
237 =head1 COPYRIGHT
238  
239 Copyright (c) 2007-2023 Elliot Shank.
240  
241 This program is free software; you can redistribute it and/or modify
242 it under the same terms as Perl itself. The full text of this license
243 can be found in the LICENSE file included with this module.
244  
245 =cut
246  
247 # Local Variables:
248 # mode: cperl
249 # cperl-indent-level: 4
250 # fill-column: 78
251 # indent-tabs-mode: nil
252 # c-indentation-style: bsd
253 # End:
254 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Documentation/RequirePackageMatchesPodName.pm ↑ Summary blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm »