File Coverage

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


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements;
2              
3 40     40   26126 use 5.010001;
  40         157  
4 40     40   230 use strict;
  40         103  
  40         806  
5 40     40   249 use warnings;
  40         93  
  40         918  
6 40     40   205 use Readonly;
  40         94  
  40         1986  
7              
8              
9 40     40   257 use Perl::Critic::Utils qw{ :booleans :characters :severities :classification hashify };
  40         102  
  40         2147  
10 40     40   21598 use Perl::Critic::Utils::PPI qw{ is_ppi_statement_subclass };
  40         100  
  40         2053  
11              
12 40     40   251 use parent 'Perl::Critic::Policy';
  40         110  
  40         224  
13              
14             our $VERSION = '1.150';
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 90     90 0 2143 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 74     74 1 328 sub default_severity { return $SEVERITY_HIGH }
35 92     92 1 395 sub default_themes { return qw( core bugs pbp certrule ) }
36 32     32 1 127 sub applies_to { return 'PPI::Statement' }
37              
38             #-----------------------------------------------------------------------------
39              
40             sub violates {
41 294     294 1 598 my ( $self, $elem, undef ) = @_;
42              
43             # Grrr... PPI instantiates non-leaf nodes in its class hierarchy...
44 294 100       679 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 89 100       292 return if _is_parent_a_constructor_or_list($elem);
50 83 50       293 return if _is_parent_a_for_loop($elem);
51              
52 83 50       314 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 83         216 foreach my $child ( $elem->schildren() ) {
59 247 50 33     1976 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 247 100       987 if ( $child->isa('PPI::Token::Word') ) {
    100          
64 41 100       106 return if _succeeding_commas_are_list_element_separators($child);
65             }
66             elsif ( $child->isa('PPI::Token::Operator') ) {
67 32 50       89 if ( $child->content() eq $COMMA ) {
68 0         0 return $self->violation($DESC, $EXPL, $elem);
69             }
70             }
71             }
72             }
73              
74 65         220 return;
75             }
76              
77             sub _is_parent_a_constructor_or_list {
78 89     89   157 my ($elem) = @_;
79              
80 89         225 my $parent = $elem->parent();
81              
82 89 50       484 return if not $parent;
83              
84             return (
85 89   66     568 $parent->isa('PPI::Structure::Constructor')
86             or $parent->isa('PPI::Structure::List')
87             );
88             }
89              
90             sub _is_parent_a_for_loop {
91 83     83   173 my ($elem) = @_;
92              
93 83         175 my $parent = $elem->parent();
94              
95 83 50       368 return if not $parent;
96              
97 83 100       377 return if not $parent->isa('PPI::Structure::For');
98              
99 6         41 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 247     247   423 my ($elem) = @_;
119              
120 247         584 my $parent = $elem->parent();
121 247 50       1034 return if not $parent;
122 247 50       922 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 41     41   75 my ($elem) = @_;
132              
133 41 100 66     110 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         126 return;
138             }
139              
140 33         606 my $sibling = $elem->snext_sibling();
141              
142 33 50       653 return 1 if not $sibling; # There won't be any succeeding commas.
143              
144 33         146 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 :