line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Plicease::ProhibitUnicodeDigitInRegexp; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
2570
|
use strict; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
101
|
|
4
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
40
|
|
|
3
|
|
|
|
|
89
|
|
5
|
3
|
|
|
3
|
|
65
|
use 5.008001; |
|
3
|
|
|
|
|
13
|
|
6
|
3
|
|
|
3
|
|
18
|
use Perl::Critic::Utils qw( $SEVERITY_LOW ); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
330
|
|
7
|
3
|
|
|
3
|
|
1134
|
use PPIx::Regexp; |
|
3
|
|
|
|
|
288846
|
|
|
3
|
|
|
|
|
140
|
|
8
|
3
|
|
|
3
|
|
31
|
use base qw( Perl::Critic::Policy ); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
400
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# ABSTRACT: Prohibit non-ASCII \d in regular expressions |
11
|
|
|
|
|
|
|
our $VERSION = '0.04'; # VERSION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
3
|
|
|
3
|
|
24
|
use constant DESC => 'Using non-ASCII \d'; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
232
|
|
15
|
3
|
|
|
|
|
753
|
use constant EXPL => 'The character class \d matches non-ASCII unicode digits. ' . |
16
|
3
|
|
|
3
|
|
65
|
'Use [0-9] or the /a modifier (Perl 5.14+) instead.'; |
|
3
|
|
|
|
|
8
|
|
17
|
|
|
|
|
|
|
|
18
|
11
|
|
|
11
|
0
|
50613
|
sub supported_parameters { () } |
19
|
8
|
|
|
8
|
1
|
91
|
sub default_severity { $SEVERITY_LOW } |
20
|
0
|
|
|
0
|
1
|
0
|
sub default_themes { () } |
21
|
11
|
|
|
11
|
1
|
60804
|
sub applies_to { return ('PPI::Token::Regexp::Match', |
22
|
|
|
|
|
|
|
'PPI::Token::Regexp::Substitute', |
23
|
|
|
|
|
|
|
'PPI::Token::QuoteLike::Regexp') } |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub violates |
26
|
|
|
|
|
|
|
{ |
27
|
12
|
|
|
12
|
1
|
915
|
my($self, $elem) = @_; |
28
|
|
|
|
|
|
|
|
29
|
12
|
|
|
|
|
50
|
my %mods = $elem->get_modifiers(); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# if the whole expression uses /a then we are in the clear. |
32
|
12
|
100
|
|
|
|
222
|
return if $mods{'a'}; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# if the user has explicitly specified the /u modifier then |
35
|
|
|
|
|
|
|
# we should assume that they want unicode digits. Done. |
36
|
11
|
100
|
|
|
|
28
|
return if $mods{'u'}; |
37
|
|
|
|
|
|
|
|
38
|
10
|
|
|
|
|
31
|
my $re = PPIx::Regexp->new($elem->content); |
39
|
10
|
|
|
|
|
22298
|
my $ccs = $re->find('PPIx::Regexp::Token::CharClass'); |
40
|
10
|
100
|
|
|
|
1825
|
return unless $ccs; |
41
|
9
|
|
|
|
|
23
|
foreach my $cc (@$ccs) |
42
|
|
|
|
|
|
|
{ |
43
|
9
|
100
|
|
|
|
28
|
next if $cc->content ne '\\d'; |
44
|
8
|
|
|
|
|
82
|
return $self->violation( DESC, EXPL, $elem ); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
10
|
return; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
__END__ |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=pod |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=encoding UTF-8 |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Perl::Critic::Policy::Plicease::ProhibitUnicodeDigitInRegexp - Prohibit non-ASCII \d in regular expressions |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 VERSION |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
version 0.04 |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
The character class C<\d> in a regular expression matches all unicode digit character, which |
69
|
|
|
|
|
|
|
might not be what you expect if you are testing if a string can be used as a number in Perl. |
70
|
|
|
|
|
|
|
Instead use either C<[0-9]>, or if you are on Perl 5.14 or better you can use the C</a> |
71
|
|
|
|
|
|
|
modifier. This policy allows C<\d> in expressions with an explicit C</u> modifier (normally |
72
|
|
|
|
|
|
|
on by default), as it indicates that the code is expecting Unicode semantics, including Unicode |
73
|
|
|
|
|
|
|
digits. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
/\d/; # not ok |
76
|
|
|
|
|
|
|
/\d/a; # ok |
77
|
|
|
|
|
|
|
/\d/u; # ok |
78
|
|
|
|
|
|
|
/[0-9]/; # ok |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 AFFILIATION |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
None. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 CONFIGURATION |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This policy is not configurable except for the standard options. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 CAVEATS |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
This is not a general policy, and should not be applied toward all applications without |
91
|
|
|
|
|
|
|
some thought. This is frequently true for L<Perl::Critic> policies, but especially so |
92
|
|
|
|
|
|
|
for this policy. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
In the general the ability to match against unicode digits is a useful ability, and doesn't |
95
|
|
|
|
|
|
|
constitute bad code. On the other hand, some applications don't ever need to match non-ASCII |
96
|
|
|
|
|
|
|
digit characters, and incorrectly rely on C<\d> to validate as a number as Perl understands |
97
|
|
|
|
|
|
|
it (and Perl understands non-ASCII digits as zero regardless of what they mean in their |
98
|
|
|
|
|
|
|
respective languages). |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This policy doesn't take into account using the L<re> pragma. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
use re '/a'; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
/\d/; # (still) not ok |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 AUTHOR |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Author: Graham Ollis E<lt>plicease@cpan.orgE<gt> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Contributors: |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Ville Skyttä (SCOP) |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
This software is copyright (c) 2019 by Graham Ollis. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
119
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |