line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Lint::Policy::BuiltinFunctions::ProhibitVoidGrep; |
2
|
133
|
|
|
133
|
|
68639
|
use strict; |
|
133
|
|
|
|
|
204
|
|
|
133
|
|
|
|
|
3257
|
|
3
|
133
|
|
|
133
|
|
437
|
use warnings; |
|
133
|
|
|
|
|
170
|
|
|
133
|
|
|
|
|
2633
|
|
4
|
133
|
|
|
133
|
|
819
|
use Perl::Lint::Constants::Type; |
|
133
|
|
|
|
|
168
|
|
|
133
|
|
|
|
|
59308
|
|
5
|
133
|
|
|
133
|
|
638
|
use parent "Perl::Lint::Policy"; |
|
133
|
|
|
|
|
170
|
|
|
133
|
|
|
|
|
619
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# TODO REFACTOR!!! |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use constant { |
10
|
133
|
|
|
|
|
34020
|
DESC => '"grep" used in void context', |
11
|
|
|
|
|
|
|
EXPL => 'Use a "for" loop instead', |
12
|
133
|
|
|
133
|
|
6750
|
}; |
|
133
|
|
|
|
|
188
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub evaluate { |
15
|
6
|
|
|
6
|
0
|
11
|
my ($class, $file, $tokens, $args) = @_; |
16
|
|
|
|
|
|
|
|
17
|
6
|
|
|
|
|
6
|
my @violations; |
18
|
6
|
|
|
|
|
6
|
my $is_in_context = 0; |
19
|
6
|
|
|
|
|
5
|
my $is_in_grep = 0; |
20
|
6
|
|
|
|
|
7
|
my $is_in_ctrl_statement = 0; |
21
|
6
|
|
|
|
|
6
|
my $left_brace_num = 0; |
22
|
6
|
|
|
|
|
19
|
for (my $i = 0; my $token = $tokens->[$i]; $i++) { |
23
|
378
|
|
|
|
|
253
|
my $token_type = $token->{type}; |
24
|
378
|
|
|
|
|
240
|
my $token_data = $token->{data}; |
25
|
378
|
100
|
100
|
|
|
2101
|
if ($token_type == BUILTIN_FUNC) { |
|
|
100
|
100
|
|
|
|
|
|
|
100
|
66
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
26
|
50
|
100
|
|
|
|
62
|
if ($token_data eq 'grep') { |
27
|
42
|
100
|
|
|
|
70
|
next if $is_in_grep; |
28
|
|
|
|
|
|
|
|
29
|
24
|
100
|
|
|
|
39
|
if ($is_in_ctrl_statement) { |
|
|
100
|
|
|
|
|
|
30
|
5
|
100
|
|
|
|
10
|
if ($left_brace_num) { |
31
|
|
|
|
|
|
|
push @violations, { |
32
|
|
|
|
|
|
|
filename => $file, |
33
|
|
|
|
|
|
|
line => $token->{line}, |
34
|
3
|
|
|
|
|
10
|
description => DESC, |
35
|
|
|
|
|
|
|
explanation => EXPL, |
36
|
|
|
|
|
|
|
policy => __PACKAGE__, |
37
|
|
|
|
|
|
|
}; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
elsif (!$is_in_context) { |
41
|
|
|
|
|
|
|
push @violations, { |
42
|
|
|
|
|
|
|
filename => $file, |
43
|
|
|
|
|
|
|
line => $token->{line}, |
44
|
11
|
|
|
|
|
36
|
description => DESC, |
45
|
|
|
|
|
|
|
explanation => EXPL, |
46
|
|
|
|
|
|
|
policy => __PACKAGE__, |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
24
|
|
|
|
|
13
|
$is_in_grep = 1; |
51
|
24
|
|
|
|
|
44
|
next; |
52
|
|
|
|
|
|
|
} |
53
|
8
|
|
|
|
|
12
|
$is_in_context = 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
elsif ($token_type == ASSIGN) { |
56
|
3
|
|
|
|
|
5
|
$is_in_context = 1; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
elsif ( # NOTE enough? |
59
|
|
|
|
|
|
|
$token_type == IF_STATEMENT || |
60
|
|
|
|
|
|
|
$token_type == FOR_STATEMENT || |
61
|
|
|
|
|
|
|
$token_type == WHILE_STATEMENT || |
62
|
|
|
|
|
|
|
$token_type == UNLESS_STATEMENT |
63
|
|
|
|
|
|
|
) { |
64
|
5
|
|
|
|
|
8
|
$is_in_ctrl_statement = 1; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
elsif ($is_in_ctrl_statement) { |
67
|
50
|
100
|
|
|
|
90
|
if ($token_type == LEFT_BRACE) { |
|
|
100
|
|
|
|
|
|
68
|
8
|
|
|
|
|
15
|
$left_brace_num++; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
elsif ($token_type == RIGHT_BRACE) { |
71
|
8
|
|
|
|
|
6
|
$left_brace_num--; |
72
|
8
|
100
|
|
|
|
13
|
if ($left_brace_num <= 0) { |
73
|
5
|
|
|
|
|
5
|
$is_in_ctrl_statement = 0; |
74
|
5
|
|
|
|
|
7
|
$is_in_grep = 0; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
elsif ($token_type == SEMI_COLON) { |
79
|
20
|
|
|
|
|
13
|
$is_in_context = 0; |
80
|
20
|
|
|
|
|
31
|
$is_in_grep = 0; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
6
|
|
|
|
|
21
|
return \@violations; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
1; |
88
|
|
|
|
|
|
|
|