« Policy/Documentation/ProhibitLinkToSelf.pm
↑ Summary
Policy/ValuesAndExpressions/ProhibitArrayAssignAref.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::ProhibitEmptyCommas; | ||||||||||||||||||||||||||||||||||||||||||
| 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::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 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 40 | ▶ | use constant supported_parameters => (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 40 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 40 | ▶ | use constant default_themes => qw(pulp cosmetic); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 40 | ▶ | use constant applies_to => ('PPI::Token::Operator'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | sub violates { | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 72 | ▶ | my ($self, $elem, $document) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=13 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 72 | ▶ | $Perl::Critic::Pulp::Utils::COMMA{$elem} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 54 | my $prev = $elem->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 54 | ▶ | if ($prev && ! ($prev->isa('PPI::Token::Operator') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$prev and not $prev->isa("PPI::Token::Operator") && $Perl::Critic::Pulp::Utils::COMMA{$prev}
$prev->isa('PPI::Token::Operator') && $Perl::Critic::Pulp::Utils::COMMA{$prev}
MC/DC100%3/3
$prev and not $prev->isa("PPI::Token::Operator") && $Perl::Critic::Pulp::Utils::COMMA{$prev}
$prev
$prev->isa('PPI::Token::Operator')
$Perl::Critic::Pulp::Utils::COMMA{$prev}
Truth table100%4/4
A and not(B && C)
A= $prevB= $prev->isa('PPI::Token::Operator')C= $Perl::Critic::Pulp::Utils::COMMA{$prev}
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | && $Perl::Critic::Pulp::Utils::COMMA{$prev})) { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # have a previous element and it's not a comma operator | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 30 | return; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # A statement like | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # return bless({@_}, $class) | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||||||||||||||||
| 54 | # is parsed by PPI as | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||||||||||||||||
| 56 | # PPI::Structure::List ( ... ) | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # PPI::Statement::Compound | ||||||||||||||||||||||||||||||||||||||||||
| 58 | # PPI::Structure::Block { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # PPI::Statement | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # PPI::Token::Magic '@_' | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # PPI::Statement::Expression | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # PPI::Token::Operator ',' | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # PPI::Token::Symbol '$class' | ||||||||||||||||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # so the "{@_}" bit is not an immediate predecessor of the "," operator. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # If our $elem has no $prev then also look outwards to see if it's at the | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # start of an expression which is in a list and there's something | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # preceding in the list. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 24 | ▶ | if (! $prev) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 11 | my $parent = $elem->parent; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 11 | ▶ | if ($parent->isa('PPI::Statement::Expression') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$parent->isa("PPI::Statement::Expression") and $parent->parent->isa("PPI::Structure::List") and $parent->sprevious_sibling
$parent->isa("PPI::Statement::Expression") and $parent->parent->isa("PPI::Structure::List")
MC/DC0%0/3
$parent->isa("PPI::Statement::Expression") and $parent->parent->isa("PPI::Structure::List") and $parent->sprevious_sibling
$parent->isa("PPI::Statement::Expression")
$parent->parent->isa("PPI::Structure::List")
$parent->sprevious_sibling
Truth table50%2/4
A and B and C
A= $parent->isa("PPI::Statement::Expression")B= $parent->parent->isa("PPI::Structure::List")C= $parent->sprevious_sibling
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | && $parent->parent->isa('PPI::Structure::List') | ||||||||||||||||||||||||||||||||||||||||||
| 74 | && $parent->sprevious_sibling) { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | # An expression like | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # [{%a},{}] | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # is parsed by PPI 1.215 as | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # PPI::Statement | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # PPI::Structure::Constructor [ ... ] | ||||||||||||||||||||||||||||||||||||||||||
| 87 | # PPI::Statement::Compound | ||||||||||||||||||||||||||||||||||||||||||
| 88 | # PPI::Structure::Block { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # PPI::Statement | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # PPI::Token::Symbol '%a' | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # PPI::Statement | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # PPI::Token::Operator ',' | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # PPI::Structure::Constructor { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||||||||||||||||
| 95 | # so the "{%a}" bit is not an immediate predecessor of the "," operator. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # If our $elem has no $prev then also look upwards to see if it's at the | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # start of an statement which is in a constructor and there's something | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # preceding in that constructor. | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 24 | ▶ | if (! $prev) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 11 | my $parent = $elem->parent; | |||||||||||||||||||||||||||||||||||||||||
| 102 | ### parent: ref $parent | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 11 | ▶ | if ($parent->isa('PPI::Statement') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$parent->isa("PPI::Statement") and $parent->parent->isa("PPI::Structure::Constructor") and $parent->sprevious_sibling
$parent->isa("PPI::Statement") and $parent->parent->isa("PPI::Structure::Constructor")
MC/DC33%1/3
$parent->isa("PPI::Statement") and $parent->parent->isa("PPI::Structure::Constructor") and $parent->sprevious_sibling
$parent->isa("PPI::Statement")
$parent->parent->isa("PPI::Structure::Constructor")
$parent->sprevious_sibling
Truth table50%2/4
A and B and C
A= $parent->isa("PPI::Statement")B= $parent->parent->isa("PPI::Structure::Constructor")C= $parent->sprevious_sibling
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | && $parent->parent->isa('PPI::Structure::Constructor') | ||||||||||||||||||||||||||||||||||||||||||
| 105 | && $parent->sprevious_sibling) { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # $prev is either nothing or a comma operator | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 19 | return $self->violation ('Empty comma operator', | |||||||||||||||||||||||||||||||||||||||||
| 112 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 113 | $elem); | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 40 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =for stopwords Ryde | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | Perl::Critic::Policy::ValuesAndExpressions::ProhibitEmptyCommas - disallow empty consecutive commas | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp> | ||||||||||||||||||||||||||||||||||||||||||
| 128 | add-on. It prohibits empty comma operators C<,> or C<=E<gt>> meaning either | ||||||||||||||||||||||||||||||||||||||||||
| 129 | consecutive commas or a comma at the start of a list or expression. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | print 'foo',,'bar'; # bad | ||||||||||||||||||||||||||||||||||||||||||
| 132 | @a = (,1,2); # bad | ||||||||||||||||||||||||||||||||||||||||||
| 133 | foo (x, => 123); # bad | ||||||||||||||||||||||||||||||||||||||||||
| 134 | a =>=> 456; # bad | ||||||||||||||||||||||||||||||||||||||||||
| 135 | for (; $i++<10; $i++,,) # bad | ||||||||||||||||||||||||||||||||||||||||||
| 136 | func (,) # bad | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | Extra commas like this are harmless and simply collapse out when the program | ||||||||||||||||||||||||||||||||||||||||||
| 139 | runs (see L<perldata/List value constructors>), so this policy is only under | ||||||||||||||||||||||||||||||||||||||||||
| 140 | the "cosmetic" theme (see L<Perl::Critic/POLICY THEMES>). Usually this sort | ||||||||||||||||||||||||||||||||||||||||||
| 141 | of thing is just a stray, or leftover from cut and paste, or perhaps some | ||||||||||||||||||||||||||||||||||||||||||
| 142 | over-enthusiastic column layout. Occasionally it can be something more | ||||||||||||||||||||||||||||||||||||||||||
| 143 | dubious, | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # did you mean 1..6 range operator? | ||||||||||||||||||||||||||||||||||||||||||
| 146 | @a = (1,,6); # bad | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # this is two args, did you want three? | ||||||||||||||||||||||||||||||||||||||||||
| 149 | foo (1, , 2); # bad | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | # this is three args, probably you forgot a value | ||||||||||||||||||||||||||||||||||||||||||
| 152 | bar (abc => , # bad | ||||||||||||||||||||||||||||||||||||||||||
| 153 | def => 20); | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | A comma at the end of a list or call is allowed. That's quite usual and can | ||||||||||||||||||||||||||||||||||||||||||
| 156 | be a good thing when cutting and pasting lines (see C<RequireTrailingCommas> | ||||||||||||||||||||||||||||||||||||||||||
| 157 | to mandate them!). | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | @b = ("foo", | ||||||||||||||||||||||||||||||||||||||||||
| 160 | "bar", # ok | ||||||||||||||||||||||||||||||||||||||||||
| 161 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | If you use multiple commas in some systematic way for code layout you can | ||||||||||||||||||||||||||||||||||||||||||
| 164 | always disable C<ProhibitEmptyCommas> from your F<.perlcriticrc> file in the | ||||||||||||||||||||||||||||||||||||||||||
| 165 | usual way (see L<Perl::Critic/CONFIGURATION>), | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | [-ValuesAndExpressions::ProhibitEmptyCommas] | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | L<Perl::Critic::Pulp>, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | L<Perl::Critic>, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | L<Perl::Critic::Policy::CodeLayout::RequireTrailingCommas>, | ||||||||||||||||||||||||||||||||||||||||||
| 174 | L<Perl::Critic::Policy::ValuesAndExpressions::ProhibitCommaSeparatedStatements>, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | L<Perl::Critic::Policy::Tics::ProhibitManyArrows> | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =head1 HOME PAGE | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | http://user42.tuxfamily.org/perl-critic-pulp/index.html | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | Perl-Critic-Pulp is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 186 | under the terms of the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||||||||||||||||
| 187 | Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||||||||||||||||||||
| 188 | version. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | Perl-Critic-Pulp is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||||||||||||||||
| 191 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||||||||||||||||
| 192 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||||||||||||||||||||||||||||||||||||||||||
| 193 | more details. | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | You should have received a copy of the GNU General Public License along with | ||||||||||||||||||||||||||||||||||||||||||
| 196 | Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =cut | ||||||||||||||||||||||||||||||||||||||||||