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   95594 use strict;
  134         273  
  134         5014  
3 134     134   651 use warnings;
  134         237  
  134         3370  
4 134     134   1293 use Perl::Lint::Constants::Type;
  134         239  
  134         82018  
5 134     134   896 use parent "Perl::Lint::Policy";
  134         261  
  134         823  
6              
7             use constant {
8 134         61772 DESC => '"grep" used in boolean context',
9             EXPL => [71, 72],
10 134     134   9583 };
  134         259  
11              
12             sub evaluate {
13 17     17 0 39 my ($class, $file, $tokens, $src, $args) = @_;
14              
15 17         18 my @violations;
16 17         22 my $is_grep_called = 0;
17 17         29 my $is_in_boolean_context = 0;
18 17         22 my $is_in_numeric_comparison_context = 0;
19 17         72 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
20 307         287 my $token_type = $token->{type};
21              
22 307 100 100     1790 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         12 $token = $tokens->[++$i];
29 10         11 $token_type = $token->{type};
30 10 100 33     30 if ($token_type == LEFT_PAREN) {
    50          
31 6         8 my $is_grep_called = 0;
32 6         5 my $is_in_numeric_comparison_context = 0;
33              
34 6         10 my $left_paren_num = 1;
35 6         22 for ($i++; $token = $tokens->[$i]; $i++) {
36 59         54 $token_type = $token->{type};
37 59 100 66     214 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         10 $left_paren_num++;
42             }
43             elsif ($token_type == RIGHT_PAREN) {
44 11 100       28 if (--$left_paren_num <= 0) {
45 6 100 66     25 if ($is_grep_called && !$is_in_numeric_comparison_context) {
46 5         29 push @violations, {
47             filename => $file,
48             line => $token->{line},
49             description => DESC,
50             explanation => EXPL,
51             policy => __PACKAGE__,
52             };
53             }
54 6         11 last;
55             }
56             }
57             elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') {
58 6         15 $is_grep_called = 1;
59             }
60             }
61             }
62             elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') {
63 4         16 push @violations, {
64             filename => $file,
65             line => $token->{line},
66             description => DESC,
67             explanation => EXPL,
68             policy => __PACKAGE__,
69             };
70             }
71              
72 10         19 next;
73             }
74              
75 297 100 100     505 if ($token_type == BUILTIN_FUNC && $token->{data} eq 'grep') {
76 19         19 $is_grep_called = 1;
77             }
78              
79 297 50 66     1701 if (
      66        
      66        
80             $token_type == OR ||
81             $token_type == AND ||
82             $token_type == ALPHABET_OR ||
83             $token_type == ALPHABET_AND
84             ) {
85 12         14 $is_in_boolean_context = 1;
86             }
87              
88 297 100       354 if ($token_type == EQUAL_EQUAL) {
89 1         2 $is_in_numeric_comparison_context = 1;
90             }
91              
92 297 100       660 if ($token_type == SEMI_COLON) {
93 32 100 100     106 if ($is_grep_called && $is_in_boolean_context && !$is_in_numeric_comparison_context) {
      100        
94 10         62 push @violations, {
95             filename => $file,
96             line => $token->{line},
97             description => DESC,
98             explanation => EXPL,
99             policy => __PACKAGE__,
100             };
101             }
102 32         36 $is_grep_called = 0;
103 32         30 $is_in_boolean_context = 0;
104 32         24 $is_in_numeric_comparison_context = 0;
105 32         72 next;
106             }
107             }
108              
109 17         91 return \@violations;
110             }
111              
112             1;
113