| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Perl::Lint::Policy::ValuesAndExpressions::ProhibitLeadingZeros; | 
| 2 | 133 |  |  | 133 |  | 93598 | use strict; | 
|  | 133 |  |  |  |  | 276 |  | 
|  | 133 |  |  |  |  | 4782 |  | 
| 3 | 133 |  |  | 133 |  | 730 | use warnings; | 
|  | 133 |  |  |  |  | 204 |  | 
|  | 133 |  |  |  |  | 3381 |  | 
| 4 | 133 |  |  | 133 |  | 932 | use Perl::Lint::Constants::Type; | 
|  | 133 |  |  |  |  | 209 |  | 
|  | 133 |  |  |  |  | 78083 |  | 
| 5 | 133 |  |  | 133 |  | 862 | use parent "Perl::Lint::Policy"; | 
|  | 133 |  |  |  |  | 259 |  | 
|  | 133 |  |  |  |  | 759 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | use constant { | 
| 8 | 133 |  |  |  |  | 66902 | DESC => q{Don't allow any leading zeros at all.  Otherwise builtins that deal with Unix permissions, e.g. chmod, don't get flagged.}, | 
| 9 |  |  |  |  |  |  | EXPL => [58], | 
| 10 | 133 |  |  | 133 |  | 9835 | }; | 
|  | 133 |  |  |  |  | 325 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub evaluate { | 
| 13 | 13 |  |  | 13 | 0 | 37 | my ($class, $file, $tokens, $src, $args) = @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 13 |  |  |  |  | 19 | my $is_strict; | 
| 16 | 13 | 100 |  |  |  | 45 | if (my $this_policies_arg = $args->{prohibit_leading_zeros}) { | 
| 17 | 5 |  |  |  |  | 13 | $is_strict = $this_policies_arg->{strict}; | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 13 |  |  |  |  | 16 | my @violations; | 
| 21 | 13 |  |  |  |  | 65 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | 
| 22 | 336 |  |  |  |  | 270 | my $token_type = $token->{type}; | 
| 23 | 336 |  |  |  |  | 357 | my $token_data = $token->{data}; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 336 | 100 | 100 |  |  | 780 | if (!$is_strict && $token_type == BUILTIN_FUNC) { | 
| 26 |  |  |  |  |  |  | # skip the first argument of chmod() | 
| 27 | 10 | 100 |  |  |  | 20 | if ($token_data eq 'chmod') { | 
| 28 | 4 | 100 |  |  |  | 9 | if ($tokens->[++$i]->{type} == LEFT_PAREN) { | 
| 29 | 2 |  |  |  |  | 2 | $i++; | 
| 30 |  |  |  |  |  |  | } | 
| 31 | 4 |  |  |  |  | 9 | next; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # skip third argument of dbmopen() | 
| 35 | 6 | 100 |  |  |  | 15 | if ($token_data eq 'dbmopen') { | 
| 36 | 2 | 100 |  |  |  | 7 | if ($tokens->[++$i]->{type} == LEFT_PAREN) { | 
| 37 | 1 |  |  |  |  | 2 | $i++; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 2 |  |  |  |  | 2 | my $comma_num = 0; | 
| 41 | 2 |  |  |  |  | 7 | for ($i++; $token = $tokens->[$i]; $i++) { | 
| 42 | 6 |  |  |  |  | 5 | $token_type = $token->{type}; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 6 | 50 |  |  |  | 11 | if ($token_type == SEMI_COLON) { | 
| 45 | 0 |  |  |  |  | 0 | last; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 6 | 100 |  |  |  | 9 | if ($token_type == COMMA) { | 
| 49 | 4 |  |  |  |  | 3 | $comma_num++; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 6 | 100 |  |  |  | 12 | if ($comma_num == 2) { | 
| 53 | 2 |  |  |  |  | 4 | $i++; | 
| 54 | 2 |  |  |  |  | 4 | last; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | } | 
| 57 | 2 |  |  |  |  | 3 | next; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | # skip second argument of mkdir() | 
| 61 | 4 | 100 |  |  |  | 11 | if ($token_data eq 'mkdir') { | 
| 62 | 2 | 100 |  |  |  | 8 | if ($tokens->[++$i]->{type} == LEFT_PAREN) { | 
| 63 | 1 |  |  |  |  | 4 | $i++; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 2 |  |  |  |  | 2 | my $comma_num = 0; | 
| 67 | 2 |  |  |  |  | 9 | for ($i++; $token = $tokens->[$i]; $i++) { | 
| 68 | 2 |  |  |  |  | 3 | $token_type = $token->{type}; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 2 | 50 |  |  |  | 5 | if ($token_type == SEMI_COLON) { | 
| 71 | 0 |  |  |  |  | 0 | last; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 2 | 50 |  |  |  | 6 | if ($token_type == COMMA) { | 
| 75 | 2 |  |  |  |  | 2 | $comma_num++; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 2 | 50 |  |  |  | 6 | if ($comma_num == 1) { | 
| 79 | 2 |  |  |  |  | 2 | $i++; | 
| 80 | 2 |  |  |  |  | 4 | last; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } | 
| 83 | 2 |  |  |  |  | 4 | next; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | # skip the first argument of umask() | 
| 87 | 2 | 50 |  |  |  | 6 | if ($token_data eq 'umask') { | 
| 88 | 2 | 100 |  |  |  | 5 | if ($tokens->[++$i]->{type} == LEFT_PAREN) { | 
| 89 | 1 |  |  |  |  | 3 | $i++; | 
| 90 |  |  |  |  |  |  | } | 
| 91 | 2 |  |  |  |  | 5 | next; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 326 | 100 | 100 |  |  | 694 | if (!$is_strict && $token_type == KEY) { | 
| 96 |  |  |  |  |  |  | # skip the fourth argument of sysopen() | 
| 97 | 3 | 50 |  |  |  | 7 | if ($token_data eq 'sysopen') { | 
| 98 | 3 | 100 |  |  |  | 9 | if ($tokens->[++$i]->{type} == LEFT_PAREN) { | 
| 99 | 2 |  |  |  |  | 2 | $i++; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 3 |  |  |  |  | 3 | my $comma_num = 0; | 
| 103 | 3 |  |  |  |  | 7 | for ($i++; $token = $tokens->[$i]; $i++) { | 
| 104 | 25 |  |  |  |  | 18 | $token_type = $token->{type}; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 25 | 50 |  |  |  | 31 | if ($token_type == SEMI_COLON) { | 
| 107 | 0 |  |  |  |  | 0 | last; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 25 | 100 |  |  |  | 31 | if ($token_type == COMMA) { | 
| 111 | 9 |  |  |  |  | 5 | $comma_num++; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 25 | 100 |  |  |  | 49 | if ($comma_num == 3) { | 
| 115 | 3 |  |  |  |  | 4 | $i++; | 
| 116 | 3 |  |  |  |  | 3 | last; | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  | } | 
| 119 | 3 |  |  |  |  | 7 | next; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 323 | 100 |  |  |  | 678 | if ($token_type == INT) { | 
| 124 | 37 |  |  |  |  | 45 | my $int = $token_data; | 
| 125 | 37 |  |  |  |  | 47 | $int =~ s/_//g; | 
| 126 | 37 | 100 | 50 |  |  | 1661 | if ($int =~ /\A-?0/ && (eval($int) // 0) != 0) { ## no critic: to accept bin, oct and hex decimal | 
|  |  |  | 100 |  |  |  |  | 
| 127 | 30 |  |  |  |  | 147 | push @violations, { | 
| 128 |  |  |  |  |  |  | filename => $file, | 
| 129 |  |  |  |  |  |  | line     => $token->{line}, | 
| 130 |  |  |  |  |  |  | description => DESC, | 
| 131 |  |  |  |  |  |  | explanation => EXPL, | 
| 132 |  |  |  |  |  |  | policy => __PACKAGE__, | 
| 133 |  |  |  |  |  |  | }; | 
| 134 |  |  |  |  |  |  | } | 
| 135 | 37 |  |  |  |  | 118 | next; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 13 |  |  |  |  | 68 | return \@violations; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | 1; | 
| 143 |  |  |  |  |  |  |  |