Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treats POSIX character class [:digit:] #7

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/Perl/Critic/Policy/Plicease/ProhibitUnicodeDigitInRegexp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ code:

=head1 DESCRIPTION

The character class C<\d> in a regular expression matches all unicode digit character, which
The character class C<\d> (also the POSIX character class C<[:digit:]>) in a regular expression matches all unicode digit character, which
might not be what you expect if you are testing if a string can be used as a number in Perl.
Instead use either C<[0-9]>, or if you are on Perl 5.14 or better you can use the C</a>
modifier. This policy allows C<\d> in expressions with an explicit C</u> modifier (normally
on by default), as it indicates that the code is expecting Unicode semantics, including Unicode
digits.

/\d/; # not ok
/\d/a; # ok
/\d/u; # ok
/\d/; # not ok
/[[:digit:]]/; # not ok
/\d/a; # ok
/\d/u; # ok
/[[:digit:]]/a; # ok
/[[:digit:]]/u; # ok
/[0-9]/; # ok

=head1 AFFILIATION
Expand Down Expand Up @@ -64,7 +67,7 @@ This policy doesn't take into account using the L<re> pragma.
=cut

use constant DESC => 'Using non-ASCII \d';
use constant EXPL => 'The character class \d matches non-ASCII unicode digits. ' .
use constant EXPL => 'The character class \d (also the POSIX character class [:digit:]) matches non-ASCII unicode digits. ' .
'Use [0-9] or the /a modifier (Perl 5.14+) instead.';

sub supported_parameters { () }
Expand Down Expand Up @@ -92,7 +95,7 @@ sub violates
return unless $ccs;
foreach my $cc (@$ccs)
{
next if $cc->content ne '\\d';
next if ($cc->content ne '\\d' && $cc->content ne '[:digit:]');
return $self->violation( DESC, EXPL, $elem );
}

Expand Down
31 changes: 30 additions & 1 deletion t/Plicease/ProhibitUnicodeDigitInRegexp.run
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

/\d/;

## name RexExpWithPosixDigitClass
## failures 1
## cut

/[[:digit:]]/;

## name RexExpSubWithSlashD
## failures 1
## cut
Expand All @@ -18,12 +24,27 @@ use re '/a';

/\d/;

## name GlobalDoesntSaveYouAgain
## failures 1
## cut

use re '/a';

/[[:digit:]]/;


## name QuoteLikeRegexpWithSlashD
## failures 1
## cut

qr/\d//;

## name QuoteLikeRegexpWithPosixDigitClass
## failures 1
## cut

qr/[[:digit:]]//;

## name RegExpWithDifferentDelim
## failures 3
## cut
Expand All @@ -32,6 +53,14 @@ m{\d};
s!\d!!;
qr#\d#;

## name RegExpWithDifferentDelim
## failures 3
## cut

m{[[:digit:]]};
s![[:digit:]]!!;
qr#[[:digit:]]#;

## name Transliterate
## failures 0
## cut
Expand Down Expand Up @@ -66,4 +95,4 @@ tr/\d//;
## failures 0
## cut

/foo/;
/foo/;
Loading