File Coverage

blib/lib/Perl/Critic/Policy/ValuesAndExpressions/ProhibitCommaSeparatedStatements.pm
Criterion Covered Total %
statement 54 68 79.4
branch 23 44 52.2
condition 6 9 66.6
subroutine 16 17 94.1
pod 4 5 80.0
total 103 143 72.0


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements;
2              
3 40     40   24478 use 5.010001;
  40         163  
4 40     40   199 use strict;
  40         69  
  40         817  
5 40     40   144 use warnings;
  40         67  
  40         1576  
6 40     40   203 use Readonly;
  40         255  
  40         2281  
7              
8              
9 40     40   274 use Perl::Critic::Utils qw{ :booleans :characters :severities :classification hashify };
  40         73  
  40         2151  
10 40     40   17274 use Perl::Critic::Utils::PPI qw{ is_ppi_statement_subclass };
  40         95  
  40         1973  
11              
12 40     40   181 use parent 'Perl::Critic::Policy';
  40         86  
  40         1863  
13              
14             our $VERSION = '1.156';
15              
16             #-----------------------------------------------------------------------------
17              
18             Readonly::Scalar my $DESC => q{Comma used to separate statements};
19             Readonly::Scalar my $EXPL => [ 68, 71 ];
20              
21             #-----------------------------------------------------------------------------
22              
23             sub supported_parameters {
24             return (
25             {
26 91     91 0 1140 name => 'allow_last_statement_to_be_comma_separated_in_map_and_grep',
27             description => 'Allow map and grep blocks to return lists.',
28             default_string => $FALSE,
29             behavior => 'boolean',
30             },
31             );
32             }
33              
34 75     75 1 274 sub default_severity { return $SEVERITY_HIGH }
35 92     92 1 292 sub default_themes { return qw( core bugs pbp certrule ) }
36 33     33 1 103 sub applies_to { return 'PPI::Statement' }
37              
38             #-----------------------------------------------------------------------------
39              
40             sub violates {
41 296     296 1 444 my ( $self, $elem, undef ) = @_;
42              
43             # Grrr... PPI instantiates non-leaf nodes in its class hierarchy...
44 296 100       580 return if is_ppi_statement_subclass($elem);
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 100       226 return if _is_parent_a_constructor_or_list($elem);
50 85 50       249 return if _is_parent_a_for_loop($elem);
51              
52 85 50       307 if (
53             $self->{_allow_last_statement_to_be_comma_separated_in_map_and_grep}
54             ) {
55 0 0       0 return if not _is_direct_part_of_map_or_grep_block($elem);
56             }
57              
58 85         195 foreach my $child ( $elem->schildren() ) {
59 249 50 33     1624 if (
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 100       859 if ( $child->isa('PPI::Token::Word') ) {
    100          
64 43 100       76 return if _succeeding_commas_are_list_element_separators($child);
65             }
66             elsif ( $child->isa('PPI::Token::Operator') ) {
67 32 50       69 if ( $child->content() eq $COMMA ) {
68 0         0 return $self->violation($DESC, $EXPL, $elem);
69             }
70             }
71             }
72             }
73              
74 65         168 return;
75             }
76              
77             sub _is_parent_a_constructor_or_list {
78 91     91   129 my ($elem) = @_;
79              
80 91         204 my $parent = $elem->parent();
81              
82 91 50       369 return if not $parent;
83              
84             return (
85 91   66     462 $parent->isa('PPI::Structure::Constructor')
86             or $parent->isa('PPI::Structure::List')
87             );
88             }
89              
90             sub _is_parent_a_for_loop {
91 85     85   152 my ($elem) = @_;
92              
93 85         164 my $parent = $elem->parent();
94              
95 85 50       309 return if not $parent;
96              
97 85 100       370 return if not $parent->isa('PPI::Structure::For');
98              
99 6         37 return 1 == scalar $parent->schildren(); # Multiple means C-style loop.
100             }
101              
102             sub _is_direct_part_of_map_or_grep_block {
103 0     0   0 my ($elem) = @_;
104              
105 0         0 my $parent = $elem->parent();
106 0 0       0 return if not $parent;
107 0 0       0 return if not $parent->isa('PPI::Structure::Block');
108              
109 0         0 my $block_prior_sibling = $parent->sprevious_sibling();
110 0 0       0 return if not $block_prior_sibling;
111 0 0       0 return if not $block_prior_sibling->isa('PPI::Token::Word');
112              
113 0         0 state $is_map_or_grep = { hashify( qw( map grep ) ) };
114 0         0 return $is_map_or_grep->{$block_prior_sibling};
115             }
116              
117             sub _is_last_statement_in_a_block {
118 249     249   346 my ($elem) = @_;
119              
120 249         480 my $parent = $elem->parent();
121 249 50       863 return if not $parent;
122 249 50       839 return if not $parent->isa('PPI::Structure::Block');
123              
124 0         0 my $next_sibling = $elem->snext_sibling();
125 0 0       0 return if not $next_sibling;
126              
127 0         0 return 1;
128             }
129              
130             sub _succeeding_commas_are_list_element_separators {
131 43     43   59 my ($elem) = @_;
132              
133 43 100 100     87 if (
134             is_perl_builtin_with_zero_and_or_one_arguments($elem)
135             and not is_perl_builtin_with_multiple_arguments($elem)
136             ) {
137 8         90 return;
138             }
139              
140 35         514 my $sibling = $elem->snext_sibling();
141              
142 35 50       625 return 1 if not $sibling; # There won't be any succeeding commas.
143              
144 35         115 return not $sibling->isa('PPI::Structure::List');
145             }
146              
147             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 :