« blib/lib/Perl/Lint/Policy/CodeLayout/RequireTrailingCommas.pm
↑ Summary
blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitCascadingIfElse.pm »
| 1 | package Perl::Lint::Policy::ControlStructures::ProhibitCStyleForLoops; | ||
| 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 Perl::Lint::Constants::Kind; | ||
| 6 | use parent "Perl::Lint::Policy"; | ||
| 7 | |||
| 8 | use constant { | ||
| 9 | DESC => 'C-style "for" loop used', | ||
| 10 | EXPL => [100], | ||
| 11 | }; | ||
| 12 | |||
| 13 | sub evaluate { | ||
| 14 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 15 | |||
| 16 | my @violations; | ||
| 17 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 18 | my $token_type = $token->{type}; | ||
| 19 | |||
| 20 | if ($token_type == FOR_STATEMENT || $token_type == FOREACH_STATEMENT) { | ||
| 21 | my $token = $tokens->[++$i]; | ||
| 22 | if ($token->{type} == LEFT_PAREN) { | ||
| 23 | my $semi_colon_count = 0; | ||
| 24 | my $left_paren_num = 1; | ||
| 25 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 26 | my $token_type = $token->{type}; | ||
| 27 | if ($token_type == LEFT_PAREN) { | ||
| 28 | $left_paren_num++; | ||
| 29 | } | ||
| 30 | elsif ($token_type == RIGHT_PAREN) { | ||
| 31 | last if --$left_paren_num <= 0; | ||
| 32 | } | ||
| 33 | elsif ($token_type == SEMI_COLON) { | ||
| 34 | $semi_colon_count++; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | if ($semi_colon_count == 2) { | ||
| 39 | push @violations, { | ||
| 40 | filename => $file, | ||
| 41 | line => $token->{line}, | ||
| 42 | description => DESC, | ||
| 43 | explanation => EXPL, | ||
| 44 | policy => __PACKAGE__, | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | return \@violations; | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||