line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Lint::Policy::BuiltinFunctions::ProhibitVoidMap; |
2
|
133
|
|
|
133
|
|
68252
|
use strict; |
|
133
|
|
|
|
|
194
|
|
|
133
|
|
|
|
|
3168
|
|
3
|
133
|
|
|
133
|
|
415
|
use warnings; |
|
133
|
|
|
|
|
169
|
|
|
133
|
|
|
|
|
2733
|
|
4
|
133
|
|
|
133
|
|
807
|
use Perl::Lint::Constants::Type; |
|
133
|
|
|
|
|
155
|
|
|
133
|
|
|
|
|
58675
|
|
5
|
133
|
|
|
133
|
|
565
|
use parent "Perl::Lint::Policy"; |
|
133
|
|
|
|
|
199
|
|
|
133
|
|
|
|
|
589
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# TODO REFACTOR!!! |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use constant { |
10
|
133
|
|
|
|
|
36564
|
DESC => '"map" used in void context', |
11
|
|
|
|
|
|
|
EXPL => 'Use a "for" loop instead', |
12
|
133
|
|
|
133
|
|
6562
|
}; |
|
133
|
|
|
|
|
178
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub evaluate { |
15
|
6
|
|
|
6
|
0
|
10
|
my ($class, $file, $tokens, $args) = @_; |
16
|
|
|
|
|
|
|
|
17
|
6
|
|
|
|
|
6
|
my @violations; |
18
|
6
|
|
|
|
|
7
|
my $is_in_context = 0; |
19
|
6
|
|
|
|
|
4
|
my $is_before_comma = 0; # XXX to decide is in context or not |
20
|
6
|
|
|
|
|
6
|
my $is_in_map = 0; |
21
|
6
|
|
|
|
|
4
|
my $is_in_ctrl_statement = 0; |
22
|
6
|
|
|
|
|
4
|
my $left_brace_num = 0; |
23
|
6
|
|
|
|
|
18
|
for (my $i = 0; my $token = $tokens->[$i]; $i++) { |
24
|
352
|
|
|
|
|
223
|
my $token_type = $token->{type}; |
25
|
352
|
|
|
|
|
214
|
my $token_data = $token->{data}; |
26
|
352
|
100
|
100
|
|
|
1759
|
if ($token_type == BUILTIN_FUNC) { |
|
|
100
|
100
|
|
|
|
|
|
|
100
|
100
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
27
|
48
|
100
|
|
|
|
61
|
if ($token_data eq 'map') { |
28
|
37
|
100
|
|
|
|
58
|
next if $is_in_map; |
29
|
|
|
|
|
|
|
|
30
|
25
|
100
|
100
|
|
|
59
|
if ($is_in_ctrl_statement) { |
|
|
100
|
|
|
|
|
|
31
|
6
|
100
|
|
|
|
13
|
if ($left_brace_num) { |
32
|
|
|
|
|
|
|
push @violations, { |
33
|
|
|
|
|
|
|
filename => $file, |
34
|
|
|
|
|
|
|
line => $token->{line}, |
35
|
4
|
|
|
|
|
12
|
description => DESC, |
36
|
|
|
|
|
|
|
explanation => EXPL, |
37
|
|
|
|
|
|
|
policy => __PACKAGE__, |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
elsif (!$is_in_context && !$is_before_comma) { |
42
|
|
|
|
|
|
|
push @violations, { |
43
|
|
|
|
|
|
|
filename => $file, |
44
|
|
|
|
|
|
|
line => $token->{line}, |
45
|
10
|
|
|
|
|
31
|
description => DESC, |
46
|
|
|
|
|
|
|
explanation => EXPL, |
47
|
|
|
|
|
|
|
policy => __PACKAGE__, |
48
|
|
|
|
|
|
|
}; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
25
|
|
|
|
|
18
|
$is_in_map = 1; |
52
|
25
|
|
|
|
|
39
|
next; |
53
|
|
|
|
|
|
|
} |
54
|
11
|
|
|
|
|
16
|
$is_in_context = 1; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
elsif ($token_type == ASSIGN) { |
57
|
3
|
|
|
|
|
9
|
$is_in_context = 1; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
elsif ( # NOTE enough? |
60
|
|
|
|
|
|
|
$token_type == IF_STATEMENT || |
61
|
|
|
|
|
|
|
$token_type == FOR_STATEMENT || |
62
|
|
|
|
|
|
|
$token_type == WHILE_STATEMENT || |
63
|
|
|
|
|
|
|
$token_type == UNLESS_STATEMENT |
64
|
|
|
|
|
|
|
) { |
65
|
6
|
|
|
|
|
9
|
$is_in_ctrl_statement = 1; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
elsif ($is_in_ctrl_statement) { |
68
|
62
|
100
|
|
|
|
118
|
if ($token_type == LEFT_BRACE) { |
|
|
100
|
|
|
|
|
|
69
|
10
|
|
|
|
|
15
|
$left_brace_num++; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
elsif ($token_type == RIGHT_BRACE) { |
72
|
10
|
|
|
|
|
6
|
$left_brace_num--; |
73
|
10
|
100
|
|
|
|
17
|
if ($left_brace_num <= 0) { |
74
|
6
|
|
|
|
|
3
|
$is_in_ctrl_statement = 0; |
75
|
6
|
|
|
|
|
11
|
$is_in_map = 0; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
elsif ($token_type == SEMI_COLON) { |
80
|
21
|
|
|
|
|
10
|
$is_in_context = 0; |
81
|
21
|
|
|
|
|
38
|
$is_in_map = 0; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
elsif ($token_type == COMMA) { |
84
|
11
|
|
|
|
|
16
|
$is_before_comma = 1; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
else { |
87
|
201
|
|
|
|
|
267
|
$is_before_comma = 0; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
6
|
|
|
|
|
21
|
return \@violations; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|