« blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitVoidGrep.pm
↑ Summary
blib/lib/Perl/Lint/Policy/BuiltinFunctions/RequireBlockGrep.pm »
| 1 | package Perl::Lint::Policy::BuiltinFunctions::ProhibitVoidMap; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use parent "Perl::Lint::Policy"; | ||
| 6 | |||
| 7 | # TODO REFACTOR!!! | ||
| 8 | |||
| 9 | use constant { | ||
| 10 | DESC => '"map" used in void context', | ||
| 11 | EXPL => 'Use a "for" loop instead', | ||
| 12 | }; | ||
| 13 | |||
| 14 | sub evaluate { | ||
| 15 | my ($class, $file, $tokens, $args) = @_; | ||
| 16 | |||
| 17 | my @violations; | ||
| 18 | my $is_in_context = 0; | ||
| 19 | my $is_before_comma = 0; # XXX to decide is in context or not | ||
| 20 | my $is_in_map = 0; | ||
| 21 | my $is_in_ctrl_statement = 0; | ||
| 22 | my $left_brace_num = 0; | ||
| 23 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 24 | my $token_type = $token->{type}; | ||
| 25 | my $token_data = $token->{data}; | ||
| 26 | if ($token_type == BUILTIN_FUNC) { | ||
| 27 | if ($token_data eq 'map') { | ||
| 28 | next if $is_in_map; | ||
| 29 | |||
| 30 | if ($is_in_ctrl_statement) { | ||
| 31 | if ($left_brace_num) { | ||
| 32 | push @violations, { | ||
| 33 | filename => $file, | ||
| 34 | line => $token->{line}, | ||
| 35 | 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 | description => DESC, | ||
| 46 | explanation => EXPL, | ||
| 47 | policy => __PACKAGE__, | ||
| 48 | }; | ||
| 49 | } | ||
| 50 | |||
| 51 | $is_in_map = 1; | ||
| 52 | next; | ||
| 53 | } | ||
| 54 | $is_in_context = 1; | ||
| 55 | } | ||
| 56 | elsif ($token_type == ASSIGN) { | ||
| 57 | $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 | $is_in_ctrl_statement = 1; | ||
| 66 | } | ||
| 67 | elsif ($is_in_ctrl_statement) { | ||
| 68 | if ($token_type == LEFT_BRACE) { | ||
| 69 | $left_brace_num++; | ||
| 70 | } | ||
| 71 | elsif ($token_type == RIGHT_BRACE) { | ||
| 72 | $left_brace_num--; | ||
| 73 | if ($left_brace_num <= 0) { | ||
| 74 | $is_in_ctrl_statement = 0; | ||
| 75 | $is_in_map = 0; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | elsif ($token_type == SEMI_COLON) { | ||
| 80 | $is_in_context = 0; | ||
| 81 | $is_in_map = 0; | ||
| 82 | } | ||
| 83 | elsif ($token_type == COMMA) { | ||
| 84 | $is_before_comma = 1; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | $is_before_comma = 0; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | return \@violations; | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||