| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Perl::Lint::Policy::BuiltinFunctions::ProhibitBooleanGrep; |
|
2
|
134
|
|
|
134
|
|
70404
|
use strict; |
|
|
134
|
|
|
|
|
195
|
|
|
|
134
|
|
|
|
|
3176
|
|
|
3
|
134
|
|
|
134
|
|
465
|
use warnings; |
|
|
134
|
|
|
|
|
168
|
|
|
|
134
|
|
|
|
|
2468
|
|
|
4
|
134
|
|
|
134
|
|
802
|
use Perl::Lint::Constants::Type; |
|
|
134
|
|
|
|
|
163
|
|
|
|
134
|
|
|
|
|
59323
|
|
|
5
|
134
|
|
|
134
|
|
592
|
use parent "Perl::Lint::Policy"; |
|
|
134
|
|
|
|
|
198
|
|
|
|
134
|
|
|
|
|
601
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use constant { |
|
8
|
134
|
|
|
|
|
45391
|
DESC => '"grep" used in boolean context', |
|
9
|
|
|
|
|
|
|
EXPL => [71, 72], |
|
10
|
134
|
|
|
134
|
|
7255
|
}; |
|
|
134
|
|
|
|
|
211
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub evaluate { |
|
13
|
17
|
|
|
17
|
0
|
27
|
my ($class, $file, $tokens, $src, $args) = @_; |
|
14
|
|
|
|
|
|
|
|
|
15
|
17
|
|
|
|
|
15
|
my @violations; |
|
16
|
17
|
|
|
|
|
14
|
my $is_grep_called = 0; |
|
17
|
17
|
|
|
|
|
14
|
my $is_in_boolean_context = 0; |
|
18
|
17
|
|
|
|
|
15
|
my $is_in_numeric_comparison_context = 0; |
|
19
|
17
|
|
|
|
|
47
|
for (my $i = 0; my $token = $tokens->[$i]; $i++) { |
|
20
|
307
|
|
|
|
|
216
|
my $token_type = $token->{type}; |
|
21
|
|
|
|
|
|
|
|
|
22
|
307
|
100
|
100
|
|
|
1438
|
if ( |
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$token_type == IF_STATEMENT || |
|
24
|
|
|
|
|
|
|
$token_type == UNLESS_STATEMENT || |
|
25
|
|
|
|
|
|
|
$token_type == WHILE_STATEMENT || |
|
26
|
|
|
|
|
|
|
$token_type == UNTIL_STATEMENT |
|
27
|
|
|
|
|
|
|
) { |
|
28
|
10
|
|
|
|
|
11
|
$token = $tokens->[++$i]; |
|
29
|
10
|
|
|
|
|
9
|
$token_type = $token->{type}; |
|
30
|
10
|
100
|
33
|
|
|
27
|
if ($token_type == LEFT_PAREN) { |
|
|
|
50
|
|
|
|
|
|
|
31
|
6
|
|
|
|
|
6
|
my $is_grep_called = 0; |
|
32
|
6
|
|
|
|
|
4
|
my $is_in_numeric_comparison_context = 0; |
|
33
|
|
|
|
|
|
|
|
|
34
|
6
|
|
|
|
|
5
|
my $left_paren_num = 1; |
|
35
|
6
|
|
|
|
|
9
|
for ($i++; $token = $tokens->[$i]; $i++) { |
|
36
|
59
|
|
|
|
|
36
|
$token_type = $token->{type}; |
|
37
|
59
|
100
|
66
|
|
|
173
|
if ($token_type == EQUAL_EQUAL) { |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
2
|
$is_in_numeric_comparison_context = 1; |
|
39
|
|
|
|
|
|
|
} |
|
40
|
|
|
|
|
|
|
elsif ($token_type == LEFT_PAREN) { |
|
41
|
5
|
|
|
|
|
9
|
$left_paren_num++; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
elsif ($token_type == RIGHT_PAREN) { |
|
44
|
11
|
100
|
|
|
|
17
|
if (--$left_paren_num <= 0) { |
|
45
|
6
|
100
|
66
|
|
|
19
|
if ($is_grep_called && !$is_in_numeric_comparison_context) { |
|
46
|
|
|
|
|
|
|
push @violations, { |
|
47
|
|
|
|
|
|
|
filename => $file, |
|
48
|
|
|
|
|
|
|
line => $token->{line}, |
|
49
|
5
|
|
|
|
|
18
|
description => DESC, |
|
50
|
|
|
|
|
|
|
explanation => EXPL, |
|
51
|
|
|
|
|
|
|
policy => __PACKAGE__, |
|
52
|
|
|
|
|
|
|
}; |
|
53
|
|
|
|
|
|
|
} |
|
54
|
6
|
|
|
|
|
8
|
last; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') { |
|
58
|
6
|
|
|
|
|
9
|
$is_grep_called = 1; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') { |
|
63
|
|
|
|
|
|
|
push @violations, { |
|
64
|
|
|
|
|
|
|
filename => $file, |
|
65
|
|
|
|
|
|
|
line => $token->{line}, |
|
66
|
4
|
|
|
|
|
10
|
description => DESC, |
|
67
|
|
|
|
|
|
|
explanation => EXPL, |
|
68
|
|
|
|
|
|
|
policy => __PACKAGE__, |
|
69
|
|
|
|
|
|
|
}; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
10
|
|
|
|
|
18
|
next; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
297
|
100
|
100
|
|
|
430
|
if ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') { |
|
76
|
19
|
|
|
|
|
17
|
$is_grep_called = 1; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
297
|
50
|
66
|
|
|
1338
|
if ( |
|
|
|
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
80
|
|
|
|
|
|
|
$token_type == OR || |
|
81
|
|
|
|
|
|
|
$token_type == AND || |
|
82
|
|
|
|
|
|
|
$token_type == ALPHABET_OR || |
|
83
|
|
|
|
|
|
|
$token_type == ALPHABET_AND |
|
84
|
|
|
|
|
|
|
) { |
|
85
|
12
|
|
|
|
|
13
|
$is_in_boolean_context = 1; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
297
|
100
|
|
|
|
307
|
if ($token_type == EQUAL_EQUAL) { |
|
89
|
1
|
|
|
|
|
4
|
$is_in_numeric_comparison_context = 1; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
297
|
100
|
|
|
|
522
|
if ($token_type == SEMI_COLON) { |
|
93
|
32
|
100
|
100
|
|
|
83
|
if ($is_grep_called && $is_in_boolean_context && !$is_in_numeric_comparison_context) { |
|
|
|
|
100
|
|
|
|
|
|
94
|
|
|
|
|
|
|
push @violations, { |
|
95
|
|
|
|
|
|
|
filename => $file, |
|
96
|
|
|
|
|
|
|
line => $token->{line}, |
|
97
|
10
|
|
|
|
|
31
|
description => DESC, |
|
98
|
|
|
|
|
|
|
explanation => EXPL, |
|
99
|
|
|
|
|
|
|
policy => __PACKAGE__, |
|
100
|
|
|
|
|
|
|
}; |
|
101
|
|
|
|
|
|
|
} |
|
102
|
32
|
|
|
|
|
28
|
$is_grep_called = 0; |
|
103
|
32
|
|
|
|
|
23
|
$is_in_boolean_context = 0; |
|
104
|
32
|
|
|
|
|
22
|
$is_in_numeric_comparison_context = 0; |
|
105
|
32
|
|
|
|
|
58
|
next; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
17
|
|
|
|
|
58
|
return \@violations; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
|
113
|
|
|
|
|
|
|
|