« Policy/ValuesAndExpressions/ProhibitEmptyCommas.pm
↑ Summary
Policy/ValuesAndExpressions/UnexpandedSpecialLiteral.pm »
| 1 | # Copyright 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 | |||||||||||||||||||||||||||||
| 17 | # eg. | ||||||||||||||||||||||||||||
| 18 | # perlcritic -s ProhibitArrayAssignAref /usr/lib/perl5/Template/Test.pm | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | package Perl::Critic::Policy::ValuesAndExpressions::ProhibitArrayAssignAref; | ||||||||||||||||||||||||||||
| 22 | 40 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 40 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 40 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 40 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 40 | ▶ | use Perl::Critic::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 40 | ▶ | use Perl::Critic::Pulp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 31 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 40 | our $VERSION = 99; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 40 | ▶ | use constant supported_parameters => (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 40 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 40 | ▶ | use constant default_themes => qw(pulp bugs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 40 | use constant applies_to => ('PPI::Token::Symbol', | |||||||||||||||||||||||||||
| 39 | 40 | ▶ | 'PPI::Token::Cast'); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub violates { | ||||||||||||||||||||||||||||
| 42 | 24 | ▶ | my ($self, $elem, $document) = @_; | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=13 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 24 | ▶ | ($elem->isa('PPI::Token::Cast') ? $elem->content : $elem->raw_type) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | eq '@' or return; | ||||||||||||||||||||||||||||
| 46 | ### ProhibitArrayAssignAref: $elem->content | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 18 | my $thing = 'Array'; | |||||||||||||||||||||||||||
| 49 | 18 | for (;;) { | |||||||||||||||||||||||||||
| 50 | 39 | ▶ | $elem = $elem->snext_sibling || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
$elem->snext_sibling || (return)
MC/DC0%0/1
$elem->snext_sibling || (return)
$elem->snext_sibling
Truth table50%1/2
A
A= $elem->snext_sibling
| |||||||||||||||||||||||||||||
| 51 | 39 | ▶ | last if $elem->isa('PPI::Token::Operator'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | ### skip: ref $elem | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # @foo[1,2] gives the [1,2] as a PPI::Structure::Subscript | ||||||||||||||||||||||||||||
| 55 | # @{foo()}[1,2] gives the [1,2] as a PPI::Structure::Constructor | ||||||||||||||||||||||||||||
| 56 | # the latter is probably wrong (as of PPI 1.215) | ||||||||||||||||||||||||||||
| 57 | 21 | ▶ | if ($elem->isa('PPI::Structure::Subscript') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$elem->isa("PPI::Structure::Subscript") or $elem->isa("PPI::Structure::Constructor")
MC/DC100%2/2
$elem->isa("PPI::Structure::Subscript") or $elem->isa("PPI::Structure::Constructor")
$elem->isa("PPI::Structure::Subscript")
$elem->isa("PPI::Structure::Constructor")
Truth table100%3/3
A or B
A= $elem->isa("PPI::Structure::Subscript")B= $elem->isa("PPI::Structure::Constructor")
| |||||||||||||||||||||||||||||
| 58 | || $elem->isa('PPI::Structure::Constructor')) { | ||||||||||||||||||||||||||||
| 59 | 11 | ▶ | if ($elem->start eq '[') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 60 | 5 | $thing = 'Array slice'; | |||||||||||||||||||||||||||
| 61 | } elsif ($elem->start eq '{') { | ||||||||||||||||||||||||||||
| 62 | 6 | $thing = 'Hash slice'; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | ### $thing | ||||||||||||||||||||||||||||
| 67 | ### operator: $elem->content | ||||||||||||||||||||||||||||
| 68 | 18 | ▶ | $elem eq '=' or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 18 | ▶ | $elem = $elem->snext_sibling || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
$elem->snext_sibling || (return)
MC/DC0%0/1
$elem->snext_sibling || (return)
$elem->snext_sibling
Truth table50%1/2
A
A= $elem->snext_sibling
| |||||||||||||||||||||||||||||
| 71 | 18 | ▶ | ($elem->isa('PPI::Structure::Constructor') && $elem->start eq '[') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$elem->isa("PPI::Structure::Constructor") and $elem->start eq "["
MC/DC50%1/2
$elem->isa("PPI::Structure::Constructor") and $elem->start eq "["
$elem->isa("PPI::Structure::Constructor")
$elem->start eq "["
Truth table66%2/3
A and B
A= $elem->isa("PPI::Structure::Constructor")B= $elem->start eq "["
| |||||||||||||||||||||||||||||
| 72 | or return; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 11 | return $self->violation | |||||||||||||||||||||||||||
| 75 | ("$thing assigned a [] arrayref, should it be a () list ?", | ||||||||||||||||||||||||||||
| 76 | '', $elem); | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 40 | 1; | |||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =for stopwords Ryde arrayref parens Derefs Dereferences | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Perl::Critic::Policy::ValuesAndExpressions::ProhibitArrayAssignAref - don't assign an anonymous arrayref to an array | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp> | ||||||||||||||||||||||||||||
| 91 | add-on. It asks you not to assign an anonymous arrayref to an array | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | @array = [ 1, 2, 3 ]; # bad | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | The idea is that it's rather unclear whether an arrayref is intended, or | ||||||||||||||||||||||||||||
| 96 | might have meant to be a list like | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | @array = ( 1, 2, 3 ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | This policy is under the "bugs" theme (see L<Perl::Critic/POLICY THEMES>) | ||||||||||||||||||||||||||||
| 101 | for the chance C<[]> is a mistake, and since even if it's correct it will | ||||||||||||||||||||||||||||
| 102 | likely make anyone reading it wonder. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | A single arrayref can still be assigned to an array, but with parens to make | ||||||||||||||||||||||||||||
| 105 | it clear, | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | @array = ( [1,2,3] ); # ok | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Dereferences or array and hash slices (see L<perldata/Slices>) are | ||||||||||||||||||||||||||||
| 110 | recognised as an array target and treated similarly, | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | @$ref = [1,2,3]; # bad assign to deref | ||||||||||||||||||||||||||||
| 113 | @{$ref} = [1,2,3]; # bad assign to deref | ||||||||||||||||||||||||||||
| 114 | @x[1,2,3] = ['a','b','c']; # bad assign to array slice | ||||||||||||||||||||||||||||
| 115 | @x{'a','b'} = [1,2]; # bad assign to hash slice | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head2 List Assignment Parens | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | This policy is not a blanket requirement for C<()> parens on array | ||||||||||||||||||||||||||||
| 120 | assignments. It's normal and unambiguous to have a function call or C<grep> | ||||||||||||||||||||||||||||
| 121 | etc without parens. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | @array = foo(); # ok | ||||||||||||||||||||||||||||
| 124 | @array = grep {/\.txt$/} @array; # ok | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | The only likely problem from lack of parens in such cases is that the C<,> | ||||||||||||||||||||||||||||
| 127 | comma operator has lower precedence than C<=> (see L<perlop>), so something | ||||||||||||||||||||||||||||
| 128 | like | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | @array = 1,2,3; # oops, not a list | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | means | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | @array = (1); | ||||||||||||||||||||||||||||
| 135 | 2; | ||||||||||||||||||||||||||||
| 136 | 3; | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Normally the remaining literals in void context provoke a warning from Perl | ||||||||||||||||||||||||||||
| 139 | itself. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | An intentional single element assignment is quite common as a statement, for | ||||||||||||||||||||||||||||
| 142 | instance | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | @ISA = 'My::Parent::Class'; # ok | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | And for reference the range operator precedence is high enough, | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | @array = 1..10; # ok | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | But of course parens are needed if concatenating some disjoint ranges with | ||||||||||||||||||||||||||||
| 151 | the comma operator, | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | @array = (1..5, 10..15); # parens needed | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | The C<qw> form gives a list too | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | @array = qw(a b c); # ok | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | L<Perl::Critic>, | ||||||||||||||||||||||||||||
| 162 | L<Perl::Critic::Pulp> | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 HOME PAGE | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | http://user42.tuxfamily.org/perl-critic-pulp/index.html | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Perl-Critic-Pulp is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 173 | under the terms of the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||
| 174 | Software Foundation; either version 3, or (at your option) any later | ||||||||||||||||||||||||||||
| 175 | version. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Perl-Critic-Pulp is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||
| 178 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||||||||||||||||||||||||||||
| 179 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||||||||||||||||||||||||||||
| 180 | more details. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | You should have received a copy of the GNU General Public License along with | ||||||||||||||||||||||||||||
| 183 | Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses>. | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||||||||||