« blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitMutatingListFunctions.pm
↑ Summary
blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitUnlessBlocks.pm »
| 1 | package Perl::Lint::Policy::ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions; | ||
| 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 | use constant { | ||
| 8 | DESC => 'Found "%s" in condition for an "%s"', | ||
| 9 | EXPL => [99], | ||
| 10 | }; | ||
| 11 | |||
| 12 | my %invalid_op_types = ( | ||
| 13 | &NOT => '!', | ||
| 14 | &ALPHABET_NOT => 'not', | ||
| 15 | &STRING_NOT_EQUAL => 'ne', | ||
| 16 | &NOT_EQUAL => '!=', | ||
| 17 | &LESS => '<', | ||
| 18 | &LESS_EQUAL => '<=', | ||
| 19 | &GREATER => '>', | ||
| 20 | &GREATER_EQUAL => '>=', | ||
| 21 | &COMPARE => '<=>', | ||
| 22 | &STRING_LESS => 'lt', | ||
| 23 | &STRING_GREATER => 'gt', | ||
| 24 | &STRING_LESS_EQUAL => 'le', | ||
| 25 | &STRING_GREATER_EQUAL => 'ge', | ||
| 26 | &STRING_COMPARE => 'cmp', | ||
| 27 | ®_NOT => '!~', | ||
| 28 | &STRING_NOT_EQUAL => 'ne', | ||
| 29 | &NOT_EQUAL => '!=', | ||
| 30 | ); | ||
| 31 | |||
| 32 | sub evaluate { | ||
| 33 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 34 | |||
| 35 | my @violations; | ||
| 36 | for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) { | ||
| 37 | $token_type = $token->{type}; | ||
| 38 | if ($token_type == UNLESS_STATEMENT || $token_type == UNTIL_STATEMENT) { | ||
| 39 | my $control_structure = $token->{data}; | ||
| 40 | |||
| 41 | $token = $tokens->[++$i] or last; | ||
| 42 | if ($token->{type} != LEFT_PAREN) { | ||
| 43 | for (; $token = $tokens->[$i]; $i++) { | ||
| 44 | $token_type = $token->{type}; | ||
| 45 | if ($token_type == SEMI_COLON) { | ||
| 46 | last; | ||
| 47 | } | ||
| 48 | elsif ($invalid_op_types{$token_type}) { | ||
| 49 | push @violations, { | ||
| 50 | filename => $file, | ||
| 51 | line => $token->{line}, | ||
| 52 | description => sprintf(DESC, $invalid_op_types{$token_type}, $control_structure), | ||
| 53 | explanation => EXPL, | ||
| 54 | policy => __PACKAGE__, | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | next; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $lpnum = 1; | ||
| 63 | for ($i++; $token = $tokens->[$i]; $i++) { | ||
| 64 | $token_type = $token->{type}; | ||
| 65 | |||
| 66 | if ($token_type == LEFT_PAREN) { | ||
| 67 | $lpnum++; | ||
| 68 | } | ||
| 69 | elsif ($token_type == RIGHT_PAREN) { | ||
| 70 | last if --$lpnum <= 0; | ||
| 71 | } | ||
| 72 | elsif ($invalid_op_types{$token_type}) { | ||
| 73 | push @violations, { | ||
| 74 | filename => $file, | ||
| 75 | line => $token->{line}, | ||
| 76 | description => sprintf(DESC, $invalid_op_types{$token_type}, $control_structure), | ||
| 77 | explanation => EXPL, | ||
| 78 | policy => __PACKAGE__, | ||
| 79 | }; | ||
| 80 | last; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | return \@violations; | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||
| 90 | |||