| 1 | package Perl::Critic::Policy::ProhibitLeadingZeros; | ||
| 2 | $Perl::Critic::Policy::ProhibitLeadingZeros::VERSION = '1.000'; | ||
| 3 | # ABSTRACT: A leading zero is octal, which is right for a file mode and a bug anywhere else. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Perl::Critic::Utils qw{ :severities :classification }; | ||
| 15 | use parent qw{Perl::Critic::Policy}; | ||
| 16 | |||
| 17 | |||
| 18 | Readonly::Scalar my $DESC => q{Integer with leading zeros}; | ||
| 19 | Readonly::Scalar my $EXPL => q{Perl reads it as octal; only a mode handed straight to a function that takes one should look like that}; | ||
| 20 | |||
| 21 | Readonly::Array my @DEFAULT_ALLOW => qw{ chmod dbmopen mkdir mkpath make_path sysopen umask }; | ||
| 22 | |||
| 23 | # Zeros, and then a significant digit: 0600, 0_644, -007. Not 0, 00, 0.5, 0x1f | ||
| 24 | # or 0b101, none of which reads as a decimal that is not one. | ||
| 25 | Readonly::Scalar my $LEADING_ZERO_RX => qr/\A [+-]? (?: 0+ _* )+ [1-9]/xms; | ||
| 26 | |||
| 27 | # Operators that sit between arguments rather than inside one. The | ||
| 28 | # low-precedence ones end a list operator's arguments: mkdir $d, 0700 or die. | ||
| 29 | Readonly::Hash my %LOW_PRECEDENCE => map { $_ => 1 } qw{ or and xor }; | ||
| 30 | Readonly::Hash my %SEPARATOR => ( q{,} => 1, q{=>} => 1, %LOW_PRECEDENCE ); | ||
| 31 | |||
| 32 | # Words is_function_call() counts as calls that declare rather than call. | ||
| 33 | Readonly::Hash my %DECLARATOR => map { $_ => 1 } qw{ my our local state }; | ||
| 34 | |||
| 35 | |||
| 36 | sub supported_parameters { | ||
| 37 | return ( | ||
| 38 | { | ||
| 39 | name => 'allow', | ||
| 40 | description => 'Functions and methods that may take a literal with leading zeros, in addition to the built-in list.', | ||
| 41 | default_string => join( q{ }, @DEFAULT_ALLOW ), | ||
| 42 | behavior => 'string list', | ||
| 43 | } | ||
| 44 | ); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub initialize_if_enabled { | ||
| 49 | my ( $self, $config ) = @_; | ||
| 50 | |||
| 51 | # 'string list' hands us the configured value in place of the default, and | ||
| 52 | # somebody naming one function of their own did not mean to start reporting | ||
| 53 | # chmod. | ||
| 54 | $self->{_allow}{$_} = 1 for @DEFAULT_ALLOW; | ||
| 55 | |||
| 56 | return $self->SUPER::initialize_if_enabled($config); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub default_severity { return $SEVERITY_MEDIUM } | ||
| 61 | sub default_themes { return () } | ||
| 62 | sub applies_to { return 'PPI::Token::Number' } | ||
| 63 | |||
| 64 | |||
| 65 | sub violates { | ||
| 66 | my ( $self, $elem, undef ) = @_; | ||
| 67 | |||
| 68 | return if $elem->content() !~ $LEADING_ZERO_RX; | ||
| 69 | |||
| 70 | my $call = _enclosing_call($elem); | ||
| 71 | return if $call && $self->_allowed($call); | ||
| 72 | |||
| 73 | return $self->violation( $DESC, $EXPL, $elem ); | ||
| 74 | } | ||
| 75 | |||
| 76 | # The word naming the call $node is the whole of an argument to, or nothing. | ||
| 77 | # Climbs out through parentheses and anonymous hashes and arrays, which is where | ||
| 78 | # named arguments live, and stops at anything else: a block, a subscript, a | ||
| 79 | # condition, the statement's end. | ||
| 80 | sub _enclosing_call { | ||
| 81 | my ($node) = @_; | ||
| 82 | |||
| 83 | return if !_is_whole_argument($node); | ||
| 84 | |||
| 85 | my $list_operator = _list_operator_before($node); | ||
| 86 | return $list_operator if $list_operator; | ||
| 87 | |||
| 88 | my $structure = $node->parent()->parent(); | ||
| 89 | return if !$structure; | ||
| 90 | |||
| 91 | if ( $structure->isa('PPI::Structure::List') ) { | ||
| 92 | my $before = $structure->sprevious_sibling(); | ||
| 93 | return $before if $before && $before->isa('PPI::Token::Word'); | ||
| 94 | return _enclosing_call($structure); | ||
| 95 | } | ||
| 96 | |||
| 97 | return _enclosing_call($structure) if $structure->isa('PPI::Structure::Constructor'); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _is_whole_argument { | ||
| 102 | my ($node) = @_; | ||
| 103 | |||
| 104 | foreach my $neighbour ( $node->sprevious_sibling(), $node->snext_sibling() ) { | ||
| 105 | next if !$neighbour || !$neighbour->isa('PPI::Token::Operator'); | ||
| 106 | return 0 if !$SEPARATOR{ $neighbour->content() }; | ||
| 107 | } | ||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | # The parenless call whose arguments $node is among, `chmod 0600, $file`: the | ||
| 112 | # nearest word before it that is called and is not already done taking | ||
| 113 | # arguments, either by having its own parentheses or by being followed straight | ||
| 114 | # away by an operator, as O_CREAT is in `O_CREAT, 0600`. | ||
| 115 | sub _list_operator_before { | ||
| 116 | my ($node) = @_; | ||
| 117 | |||
| 118 | for ( my $sibling = $node->sprevious_sibling(); $sibling; $sibling = $sibling->sprevious_sibling() ) { | ||
| 119 | return if $sibling->isa('PPI::Token::Operator') && $LOW_PRECEDENCE{ $sibling->content() }; | ||
| 120 | next if !$sibling->isa('PPI::Token::Word'); | ||
| 121 | next if $DECLARATOR{ $sibling->content() } || !is_function_call($sibling); | ||
| 122 | |||
| 123 | my $after = $sibling->snext_sibling(); | ||
| 124 | next if $after->isa('PPI::Structure::List') || $after->isa('PPI::Token::Operator'); | ||
| 125 | |||
| 126 | return $sibling; | ||
| 127 | } | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | # An entry with no package matches the name however it is reached; one with a | ||
| 132 | # package matches only a call that names that package, as Pkg::name or | ||
| 133 | # Pkg->name. | ||
| 134 | sub _allowed { | ||
| 135 | my ( $self, $word ) = @_; | ||
| 136 | |||
| 137 | my $name = $word->content(); | ||
| 138 | my $arrow = $word->sprevious_sibling(); | ||
| 139 | if ( $arrow && $arrow->content() eq '->' ) { | ||
| 140 | my $invocant = $arrow->sprevious_sibling(); | ||
| 141 | return $self->{_allow}{"${invocant}::$name"} || $self->{_allow}{$name} | ||
| 142 | if $invocant && $invocant->isa('PPI::Token::Word'); | ||
| 143 | return $self->{_allow}{$name}; | ||
| 144 | } | ||
| 145 | |||
| 146 | my $short = $name =~ s/\A.*:://r; | ||
| 147 | return $self->{_allow}{$name} || $self->{_allow}{$short}; | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ | ||