File: ProhibitLeadingZeros.pm

package info (click to toggle)
libperl-critic-perl 1.156-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,544 kB
  • sloc: perl: 24,092; lisp: 341; makefile: 7
file content (278 lines) | stat: -rw-r--r-- 8,131 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros;

use 5.010001;
use strict;
use warnings;

use Readonly;

use Perl::Critic::Utils qw{ :characters :severities hashify };
use parent 'Perl::Critic::Policy';

our $VERSION = '1.156';

#-----------------------------------------------------------------------------

Readonly::Scalar my $LEADING_RX => qr<\A [+-]? (?: 0+ _* )+ [1-9]>xms;
Readonly::Scalar my $EXPL       => [ 58 ];

#-----------------------------------------------------------------------------

sub supported_parameters {
    return (
        {
            name           => 'strict',
            description    =>
                q<Don't allow any leading zeros at all.  Otherwise builtins that deal with Unix permissions, e.g. chmod, don't get flagged.>,
            default_string => '0',
            behavior       => 'boolean',
        },
    );
}

sub default_severity     { return $SEVERITY_HIGHEST           }
sub default_themes       { return qw< core pbp bugs certrec >         }
sub applies_to           { return 'PPI::Token::Number::Octal' }

#-----------------------------------------------------------------------------

sub violates {
    my ( $self, $elem, undef ) = @_;

    return if $elem !~ $LEADING_RX;
    return $self->_create_violation($elem) if $self->{_strict};
    return if _is_first_argument_of_chmod_or_umask($elem);
    return if _is_second_argument_of_mkdir($elem);
    return if _is_second_argument_of_mkfifo($elem);
    return if _is_third_argument_of_dbmopen($elem);
    return if _is_fourth_argument_of_sysopen($elem);
    return $self->_create_violation($elem);
}

sub _create_violation {
    my ($self, $elem) = @_;

    return $self->violation(
        qq<Integer with leading zeros: "$elem">,
        $EXPL,
        $elem
    );
}

sub _is_first_argument_of_chmod_or_umask {
    my ($elem) = @_;

    my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
    return if not $previous_token;

    state $is_chmod_or_umask = { hashify( qw( chmod umask ) ) };
    return $is_chmod_or_umask->{$previous_token->content()};
}

sub _is_second_argument_of_mkdir {
    my ($elem) = @_;

    # Preceding comma.
    my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
    return if not $previous_token;
    return if $previous_token->content() ne $COMMA;  # Don't know what it is.

    # Directory name.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    return $previous_token->content() eq 'mkdir';
}

sub _is_second_argument_of_mkfifo {
    my ($elem) = @_;

    # Preceding comma.
    my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
    return if not $previous_token;
    return if $previous_token->content() ne $COMMA;  # Don't know what it is.

    # FIFO name.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    state $is_mkfifo = { hashify( 'mkfifo', 'POSIX::mkfifo' ) };
    return $is_mkfifo->{$previous_token->content()};
}

sub _is_third_argument_of_dbmopen {
    my ($elem) = @_;

    # Preceding comma.
    my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
    return if not $previous_token;
    return if $previous_token->content() ne $COMMA;  # Don't know what it is.

    # File path.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    # Another comma.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;
    return if $previous_token->content() ne $COMMA;  # Don't know what it is.

    # Variable name.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    return $previous_token->content() eq 'dbmopen';
}

sub _is_fourth_argument_of_sysopen {
    my ($elem) = @_;

    # Preceding comma.
    my $previous_token = _previous_token_that_isnt_a_parenthesis($elem);
    return if not $previous_token;
    return if $previous_token->content() ne $COMMA;  # Don't know what it is.

    # Mode.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    while ($previous_token and $previous_token->content() ne $COMMA) {
        $previous_token =
            _previous_token_that_isnt_a_parenthesis($previous_token);
    }
    return if not $previous_token;
    return if $previous_token->content() ne $COMMA;  # Don't know what it is.

    # File name.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    # Yet another comma.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;
    return if $previous_token->content() ne $COMMA;  # Don't know what it is.

    # File handle.
    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    $previous_token =
        _previous_token_that_isnt_a_parenthesis($previous_token);
    return if not $previous_token;

    # GitHub #789
    if ( $previous_token->content() eq 'my' ) {
        $previous_token = _previous_token_that_isnt_a_parenthesis(
            $previous_token );
        return if not $previous_token;
    }

    return $previous_token->content() eq 'sysopen';
}

sub _previous_token_that_isnt_a_parenthesis {
    my ($elem) = @_;

    state $is_paren = { hashify( $LEFT_PAREN, $RIGHT_PAREN ) };

    my $previous_token = $elem->previous_token();
    while (
            $previous_token
        and (
                not $previous_token->significant()
            or  $is_paren->{$previous_token->content()}
        )
    ) {
        $previous_token = $previous_token->previous_token();
    }

    return $previous_token;
}

1;

__END__

#-----------------------------------------------------------------------------

=pod

=head1 NAME

Perl::Critic::Policy::ValuesAndExpressions::ProhibitLeadingZeros - Write C<oct(755)> instead of C<0755>.


=head1 AFFILIATION

This Policy is part of the core L<Perl::Critic|Perl::Critic>
distribution.


=head1 DESCRIPTION

Perl interprets numbers with leading zeros as octal.  If that's what
you really want, its better to use C<oct> and make it obvious.

    $var = 041;     # not ok, actually 33
    $var = oct(41); # ok

    chmod 0644, $file;                              # ok by default
    dbmopen %database, 'foo.db', 0600;              # ok by default
    mkdir $directory, 0755;                         # ok by default
    sysopen $filehandle, $filename, O_RDWR, 0666;   # ok by default
    umask 0002;                                     # ok by default

    use POSIX 'mkfifo';
    mkfifo $fifo, 0600;                             # ok by default
    POSIX::mkfifo $fifo, 0600;                      # ok by default

=head1 CONFIGURATION

If you want to ban all leading zeros, set C<strict> to a true value in
a F<.perlcriticrc> file.

    [ValuesAndExpressions::ProhibitLeadingZeros]
    strict = 1


=head1 AUTHOR

Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>

=head1 COPYRIGHT

Copyright (c) 2005-2023 Imaginative Software Systems.  All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.  The full text of this license
can be found in the LICENSE file included with this module.

=cut

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 78
#   indent-tabs-mode: nil
#   c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :