| 1 | # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Perl-Critic-Policy-Plicease-ProhibitArrayAssignAref 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-Policy-Plicease-ProhibitArrayAssignAref 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-Policy-Plicease-ProhibitArrayAssignAref. 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::Plicease::ProhibitArrayAssignAref; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use base 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use Perl::Critic::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 30 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # ABSTRACT: Don't assign an anonymous arrayref to an array | ||||||||||||||||||||||||||||
| 33 | 2 | our $VERSION = '100.00'; # VERSION | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use constant supported_parameters => (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | use constant default_severity => $Perl::Critic::Utils::SEVERITY_MEDIUM; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | use constant default_themes => (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | use constant applies_to => ('PPI::Token::Symbol', | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 2 | 'PPI::Token::Cast'); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 24 | ▶ | sub violates { | ||||||||||||||||||||||||||
|
Subroutine
violates: called CC=13 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 24 | my ($self, $elem, $document) = @_; | |||||||||||||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||