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