| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Perl::Lint::Policy::Subroutines::ProhibitUnusedPrivateSubroutines; |
|
2
|
133
|
|
|
133
|
|
71415
|
use strict; |
|
|
133
|
|
|
|
|
192
|
|
|
|
133
|
|
|
|
|
3071
|
|
|
3
|
133
|
|
|
133
|
|
441
|
use warnings; |
|
|
133
|
|
|
|
|
178
|
|
|
|
133
|
|
|
|
|
2586
|
|
|
4
|
133
|
|
|
133
|
|
1004
|
use Compiler::Lexer; |
|
|
133
|
|
|
|
|
7779
|
|
|
|
133
|
|
|
|
|
3876
|
|
|
5
|
133
|
|
|
133
|
|
1005
|
use Perl::Lint::Constants::Type; |
|
|
133
|
|
|
|
|
176
|
|
|
|
133
|
|
|
|
|
60787
|
|
|
6
|
133
|
|
|
133
|
|
610
|
use parent "Perl::Lint::Policy"; |
|
|
133
|
|
|
|
|
192
|
|
|
|
133
|
|
|
|
|
616
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use constant { |
|
9
|
133
|
|
|
|
|
78254
|
DESC => 'Private subroutine/method "%s" declared but not used', |
|
10
|
|
|
|
|
|
|
EXPL => 'Eliminate dead code', |
|
11
|
133
|
|
|
133
|
|
6602
|
}; |
|
|
133
|
|
|
|
|
185
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub evaluate { |
|
14
|
23
|
|
|
23
|
0
|
34
|
my ($class, $file, $tokens, $src, $args) = @_; |
|
15
|
|
|
|
|
|
|
|
|
16
|
23
|
|
|
|
|
20
|
my %allow; |
|
17
|
23
|
100
|
|
|
|
66
|
if (my $allow = $args->{prohibit_unused_private_subroutines}->{allow}) { |
|
18
|
1
|
|
|
|
|
6
|
$allow{$_} = 1 for split / /, $allow; |
|
19
|
|
|
|
|
|
|
} |
|
20
|
23
|
|
|
|
|
31
|
my $allow_regex = $args->{prohibit_unused_private_subroutines}->{private_name_regex}; |
|
21
|
|
|
|
|
|
|
|
|
22
|
23
|
|
|
|
|
23
|
my $lexer; |
|
23
|
|
|
|
|
|
|
my @violations; |
|
24
|
0
|
|
|
|
|
0
|
my @private_functions; |
|
25
|
0
|
|
|
|
|
0
|
my %ignores; |
|
26
|
0
|
|
|
|
|
0
|
my %called; |
|
27
|
23
|
|
|
|
|
57
|
for (my $i = 0; my $token = $tokens->[$i]; $i++) { |
|
28
|
153
|
|
|
|
|
114
|
my $token_type = $token->{type}; |
|
29
|
153
|
|
|
|
|
119
|
my $token_data = $token->{data}; |
|
30
|
|
|
|
|
|
|
|
|
31
|
153
|
100
|
100
|
|
|
918
|
if ($token_type == FUNCTION_DECL) { |
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
32
|
31
|
|
|
|
|
40
|
$token = $tokens->[++$i]; |
|
33
|
31
|
|
|
|
|
32
|
$token_data = $token->{data}; |
|
34
|
31
|
|
|
|
|
26
|
my $function_token = $token; |
|
35
|
31
|
100
|
100
|
|
|
125
|
if (substr($token_data, 0, 1) eq '_' && !$allow{$token_data}) { |
|
36
|
26
|
100
|
66
|
|
|
113
|
if (!$allow_regex || $token_data !~ /$allow_regex/) { |
|
37
|
22
|
|
|
|
|
22
|
my $declared_private_function = ''; |
|
38
|
22
|
|
|
|
|
41
|
for (; $token = $tokens->[$i]; $i++) { |
|
39
|
49
|
|
|
|
|
35
|
$token_type = $token->{type}; |
|
40
|
49
|
100
|
100
|
|
|
169
|
if ($token_type == NAMESPACE || $token_type == FUNCTION) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
41
|
22
|
|
|
|
|
48
|
$declared_private_function = $token->{data}; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
elsif ($token_type == NAMESPACE_RESOLVER) { |
|
44
|
1
|
|
|
|
|
4
|
last; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
elsif ($token_type == LEFT_BRACE) { |
|
47
|
20
|
|
|
|
|
23
|
push @private_functions, $function_token; |
|
48
|
|
|
|
|
|
|
|
|
49
|
20
|
|
|
|
|
20
|
my $left_brace_num = 1; |
|
50
|
20
|
|
|
|
|
34
|
for ($i++; $token = $tokens->[$i]; $i++) { |
|
51
|
132
|
|
|
|
|
98
|
$token_type = $token->{type}; |
|
52
|
132
|
50
|
66
|
|
|
558
|
if ($token_type == LEFT_BRACE) { |
|
|
|
100
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
0
|
$left_brace_num++; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
elsif ($token_type == RIGHT_BRACE) { |
|
56
|
20
|
50
|
|
|
|
40
|
last if --$left_brace_num <= 0; |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
elsif ($token_type == CALL || $token_type == KEY || $token_type == METHOD) { |
|
59
|
4
|
|
|
|
|
5
|
$token_data = $token->{data}; |
|
60
|
4
|
50
|
|
|
|
8
|
if ($token_data eq $declared_private_function) { |
|
61
|
4
|
|
|
|
|
6
|
next; |
|
62
|
|
|
|
|
|
|
} |
|
63
|
0
|
|
|
|
|
0
|
$called{$token_data} = 1; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
} |
|
66
|
20
|
|
|
|
|
44
|
last; |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
elsif ($token_type == SEMI_COLON) { |
|
69
|
1
|
|
|
|
|
4
|
last; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
} |
|
75
|
|
|
|
|
|
|
elsif ($token_type == CALL || $token_type == KEY || $token_type == METHOD) { |
|
76
|
11
|
|
|
|
|
26
|
$called{$token_data} = 1; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
elsif ($token_type == USED_NAME && $token_data eq 'overload') { |
|
79
|
3
|
|
|
|
|
3
|
my $is_value = 1; |
|
80
|
3
|
|
|
|
|
8
|
for ($i++; $token = $tokens->[$i]; $i++) { |
|
81
|
24
|
|
|
|
|
14
|
$token_type = $token->{type}; |
|
82
|
24
|
|
|
|
|
26
|
my $next_token = $tokens->[$i+1]; |
|
83
|
24
|
|
|
|
|
8
|
my $next_token_type = $next_token->{type}; |
|
84
|
24
|
100
|
|
|
|
55
|
if ($token_type == ARROW) { |
|
|
|
100
|
|
|
|
|
|
|
85
|
3
|
50
|
|
|
|
7
|
if ($is_value) { |
|
86
|
3
|
|
|
|
|
5
|
for ($i++; $token = $tokens->[$i]; $i++) { |
|
87
|
15
|
|
|
|
|
15
|
$token_type = $token->{type}; |
|
88
|
15
|
100
|
66
|
|
|
62
|
if ( |
|
|
|
100
|
100
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$token_type == KEY || |
|
90
|
|
|
|
|
|
|
$token_type == STRING || |
|
91
|
|
|
|
|
|
|
$token_type == RAW_STRING |
|
92
|
|
|
|
|
|
|
) { |
|
93
|
5
|
|
|
|
|
11
|
$ignores{$token->{data}} = 1; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
elsif ($token_type == SEMI_COLON) { |
|
96
|
3
|
|
|
|
|
3
|
last; # fail safe |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
3
|
|
|
|
|
8
|
$is_value = !$is_value; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
elsif ($token_type == SEMI_COLON) { |
|
103
|
3
|
|
|
|
|
7
|
last; |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
elsif ($token_type == REG_REPLACE || $token_type == REG_MATCH) { |
|
108
|
3
|
|
|
|
|
8
|
for ($i++; $token = $tokens->[$i]; $i++) { |
|
109
|
19
|
|
|
|
|
18
|
$token_type = $token->{type}; |
|
110
|
19
|
100
|
100
|
|
|
67
|
if ($token_type == REG_REPLACE_TO || $token_type == REG_EXP) { |
|
|
|
100
|
|
|
|
|
|
|
111
|
3
|
|
33
|
|
|
15
|
$lexer ||= Compiler::Lexer->new($file); |
|
112
|
3
|
|
|
|
|
217
|
my $replace_to_tokens = $lexer->tokenize($token->{data}); |
|
113
|
|
|
|
|
|
|
|
|
114
|
3
|
|
|
|
|
10
|
for (my $i = 0; $token = $replace_to_tokens->[$i]; $i++) { |
|
115
|
20
|
|
|
|
|
14
|
my $token_type = $token->{type}; |
|
116
|
20
|
100
|
66
|
|
|
97
|
if ($token_type == CALL || $token_type == KEY || $token_type == METHOD) { |
|
|
|
|
66
|
|
|
|
|
|
117
|
3
|
|
|
|
|
7
|
$called{$token->{data}} = 1; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
elsif ($token_type == SEMI_COLON) { |
|
122
|
3
|
|
|
|
|
8
|
last; # fail safe |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
} |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
|
|
128
|
23
|
|
|
|
|
28
|
for my $private_function (@private_functions) { |
|
129
|
20
|
|
|
|
|
25
|
my $private_function_name = $private_function->{data}; |
|
130
|
20
|
50
|
|
|
|
32
|
if ($ignores{$private_function_name}) { |
|
131
|
0
|
|
|
|
|
0
|
next; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
20
|
100
|
|
|
|
42
|
unless ($called{$private_function_name}) { |
|
135
|
|
|
|
|
|
|
push @violations, { |
|
136
|
|
|
|
|
|
|
filename => $file, |
|
137
|
|
|
|
|
|
|
line => $private_function->{line}, |
|
138
|
8
|
|
|
|
|
54
|
description => sprintf(DESC, $private_function_name), |
|
139
|
|
|
|
|
|
|
explanation => EXPL, |
|
140
|
|
|
|
|
|
|
policy => __PACKAGE__, |
|
141
|
|
|
|
|
|
|
}; |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
|
|
145
|
23
|
|
|
|
|
103
|
return \@violations; |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
|
149
|
|
|
|
|
|
|
|