File Coverage

blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitBooleanGrep.pm
Criterion Covered Total %
statement 55 55 100.0
branch 26 28 92.8
condition 29 36 80.5
subroutine 6 6 100.0
pod 0 1 0.0
total 116 126 92.0


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::BuiltinFunctions::ProhibitBooleanGrep;
2 134     134   70883 use strict;
  134         185  
  134         3056  
3 134     134   428 use warnings;
  134         177  
  134         2425  
4 134     134   784 use Perl::Lint::Constants::Type;
  134         153  
  134         59739  
5 134     134   575 use parent "Perl::Lint::Policy";
  134         184  
  134         576  
6              
7             use constant {
8 134         45687 DESC => '"grep" used in boolean context',
9             EXPL => [71, 72],
10 134     134   6907 };
  134         192  
11              
12             sub evaluate {
13 17     17 0 20 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 17         19 my @violations;
16 17         12 my $is_grep_called = 0;
17 17         12 my $is_in_boolean_context = 0;
18 17         15 my $is_in_numeric_comparison_context = 0;
19 17         45 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
20 307         205 my $token_type = $token->{type};
21              
22 307 100 100     1442 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         10 $token = $tokens->[++$i];
29 10         7 $token_type = $token->{type};
30 10 100 33     28 if ($token_type == LEFT_PAREN) {
    50          
31 6         3 my $is_grep_called = 0;
32 6         6 my $is_in_numeric_comparison_context = 0;
33              
34 6         5 my $left_paren_num = 1;
35 6         10 for ($i++; $token = $tokens->[$i]; $i++) {
36 59         31 $token_type = $token->{type};
37 59 100 66     178 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       18 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         15 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         11 $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         12 description => DESC,
67             explanation => EXPL,
68             policy => __PACKAGE__,
69             };
70             }
71              
72 10         16 next;
73             }
74              
75 297 100 100     403 if ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') {
76 19         15 $is_grep_called = 1;
77             }
78              
79 297 50 66     1339 if (
      66        
      66        
80             $token_type == OR ||
81             $token_type == AND ||
82             $token_type == ALPHABET_OR ||
83             $token_type == ALPHABET_AND
84             ) {
85 12         12 $is_in_boolean_context = 1;
86             }
87              
88 297 100       302 if ($token_type == EQUAL_EQUAL) {
89 1         2 $is_in_numeric_comparison_context = 1;
90             }
91              
92 297 100       524 if ($token_type == SEMI_COLON) {
93 32 100 100     84 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         33 description => DESC,
98             explanation => EXPL,
99             policy => __PACKAGE__,
100             };
101             }
102 32         21 $is_grep_called = 0;
103 32         27 $is_in_boolean_context = 0;
104 32         20 $is_in_numeric_comparison_context = 0;
105 32         52 next;
106             }
107             }
108              
109 17         59 return \@violations;
110             }
111              
112             1;
113