« Perl/Lint/Policy/Variables/ProhibitEvilVariables.pm
↑ Summary
Perl/Lint/Policy/NamingConventions/ProhibitAmbiguousNames.pm »
| 1 | package Perl::Lint::Policy::Variables::RequireNegativeIndices; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Perl::Lint::Constants::Type; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use parent "Perl::Lint::Policy"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | DESC => 'Negative array index should be used', | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | EXPL => [88], | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | my %var_token_types = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | &VAR => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | &GLOBAL_VAR => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | my %array_dereference_token_types = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | &ARRAY_DEREFERENCE => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | &ARRAY_SIZE_DEREFERENCE => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 2 | my %array_var_token_types = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | &ARRAY_VAR => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | &GLOBAL_ARRAY_VAR => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub evaluate { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
evaluate: not called CC=31 SCAR=69.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | my ($class, $file, $tokens, $src, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my @violations; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | for (my $i = 0, my $token_type, my $token_data; my $token = $tokens->[$i]; $i++) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | $token_type = $token->{type}; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | $token_data = $token->{data}; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $is_reference = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ($var_token_types{$token_type} || $token_type == SHORT_SCALAR_DEREFERENCE) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$var_token_types{$token_type} or $token_type == 117
MC/DC0%0/2
$var_token_types{$token_type} or $token_type == 117
$var_token_types{$token_type}
$token_type == 117
Truth table0%0/3
A or B
A= $var_token_types{$token_type}B= $token_type == 117
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my $var_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($token_type == SHORT_SCALAR_DEREFERENCE) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $token = $tokens->[++$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | last if !$token; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | $var_name = $token->{data}; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $is_reference = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $var_name = substr($token->{data}, 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $token = $tokens->[++$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | last if !$token; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ($token->{type} == POINTER) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $is_reference = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | $token = $tokens->[++$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | last if !$token; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($token->{type} == LEFT_BRACKET) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $nlbracket = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | for ($i++; $token = $tokens->[$i]; $i++) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $token_type = $token->{type}; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($token_type == LEFT_BRACKET) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $nlbracket++; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($token_type == RIGHT_BRACKET) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | last if --$nlbracket <= 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$token_type == 66 or $is_reference and $token_type == 118
$is_reference and $token_type == 118
MC/DC0%0/3
$token_type == 66 or $is_reference and $token_type == 118
$token_type == 66
$is_reference
$token_type == 118
Truth table0%0/4
A or B and C
A= $token_type == 66B= $is_referenceC= $token_type == 118
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | $token_type == ARRAY_SIZE || | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | ($is_reference && $token_type == SHORT_ARRAY_DEREFERENCE) | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $token = $tokens->[++$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | last if !$token; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $token_type = $token->{type}; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $array_size_data; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ($is_reference && $var_token_types{$token_type}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$is_reference and $var_token_types{$token_type}
MC/DC0%0/2
$is_reference and $var_token_types{$token_type}
$is_reference
$var_token_types{$token_type}
Truth table0%0/3
A and B
A= $is_referenceB= $var_token_types{$token_type}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $array_size_data = substr $token->{data}, 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | elsif ($token->{type} == KEY) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $array_size_data = $token->{data}; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ($array_size_data) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $array_size_data =~ s/\W.*\Z//; # XXX workaround | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | # ref: https://github.com/goccy/p5-Compiler-Lexer/issues/48 | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($array_size_data eq $var_name) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | push @violations, { | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | filename => $file, | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | line => $token->{line}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | description => DESC, | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | explanation => EXPL, | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | policy => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$token_type == 113 or $is_reference and $token_type == 121
$is_reference and $token_type == 121
MC/DC0%0/3
$token_type == 113 or $is_reference and $token_type == 121
$token_type == 113
$is_reference
$token_type == 121
Truth table0%0/4
A or B and C
A= $token_type == 113B= $is_referenceC= $token_type == 121
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | $token_type == ARRAY_DEREFERENCE || | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ($is_reference && $token_type == ARRAY_SIZE_DEREFERENCE) | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $token = $tokens->[++$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | last if !$token; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } # fall through | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $is_reference and $array_var_token_types{$token_type} or $array_dereference_token_types{$token_type}
not $is_reference and $array_var_token_types{$token_type}
MC/DC0%0/3
not $is_reference and $array_var_token_types{$token_type} or $array_dereference_token_types{$token_type}
not $is_reference
$array_var_token_types{$token_type}
$array_dereference_token_types{$token_type}
Truth table0%0/5
not(A) and B or C
A= not $is_referenceB= $array_var_token_types{$token_type}C= $array_dereference_token_types{$token_type}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | (!$is_reference && $array_var_token_types{$token_type}) || | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | $array_dereference_token_types{$token_type} | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ($token_data = substr $token->{data}, 1) =~ s/\W.*\Z//; # XXX workaround | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | # ref: https://github.com/goccy/p5-Compiler-Lexer/issues/48 | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($token_data eq $var_name) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | push @violations, { | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | filename => $file, | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | line => $token->{line}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | description => DESC, | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | explanation => EXPL, | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | policy => __PACKAGE__, | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | return \@violations; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||