« Perl/Lint/Policy/InputOutput/ProhibitExplicitStdin.pm
↑ Summary
Perl/Lint/Policy/ValuesAndExpressions/ProhibitSpecialHeredocTerminator.pm »
| 1 | package Perl::Lint::Policy::RegularExpressions::ProhibitEscapedMetacharacters; | ||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Perl::Lint::Constants::Type; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use parent "Perl::Lint::Policy"; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use constant { | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 3 | DESC => 'Use character classes for literal metachars instead of escapes', | |||||||||||||||||||||||||||||||
| 9 | EXPL => [247], | ||||||||||||||||||||||||||||||||
| 10 | 3 | }; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | sub evaluate { | ||||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=15 SCAR=37.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 13 | 2 | my ($class, $file, $tokens, $src, $args) = @_; | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 2 | my @violations; | |||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | for (my $i = 0, my $token_type, my $is_reg_quote; my $token = $tokens->[$i]; $i++) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 17 | 51 | $token_type = $token->{type}; | |||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 51 | ▶ | if ($token_type == REG_EXP || $token_type == REG_REPLACE_FROM) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$token_type == 187 or $token_type == 156
MC/DC50%1/2
$token_type == 187 or $token_type == 156
$token_type == 187
$token_type == 156
Truth table66%2/3
A or B
A= $token_type == 187B= $token_type == 156
| |||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | if ($is_reg_quote) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 21 | 0 | $is_reg_quote = 0; | |||||||||||||||||||||||||||||||
| 22 | 0 | next; | |||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 2 | my $regexp = $token->{data}; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 2 | my $maybe_regopt; | |||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | if ($token_type == REG_EXP) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 2 | $maybe_regopt = $tokens->[$i+2]; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||||||
| 32 | 0 | $maybe_regopt = $tokens->[$i+4]; | |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ($maybe_regopt->{type} == REG_DELIM) { # if it use brackets as delimiter | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 0 | $maybe_regopt = $tokens->[$i+5]; | |||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 2 | my $regex_to_detect = qr/[{}().*+?|# ]/; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | 2 | ▶ | if ($maybe_regopt) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | if ($maybe_regopt->{type} == REG_OPT) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | if ($maybe_regopt->{data} =~ /x/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 0 | $regex_to_detect = qr/[{}().*+?| ]/; | |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | if (my @backslashes = $token->{data} =~ /(\\+)$regex_to_detect/g) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 0 | for my $backslash (@backslashes) { | |||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (length($backslash) % 2 != 0) { # not escaped | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | push @violations, { | ||||||||||||||||||||||||||||||||
| 52 | filename => $file, | ||||||||||||||||||||||||||||||||
| 53 | line => $token->{line}, | ||||||||||||||||||||||||||||||||
| 54 | 0 | description => DESC, | |||||||||||||||||||||||||||||||
| 55 | explanation => EXPL, | ||||||||||||||||||||||||||||||||
| 56 | policy => __PACKAGE__, | ||||||||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||||||
| 58 | 0 | last; | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | ▶ | elsif ($token_type == REG_QUOTE || $token_type == REG_DOUBLE_QUOTE) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$token_type == 145 or $token_type == 146
MC/DC0%0/2
$token_type == 145 or $token_type == 146
$token_type == 145
$token_type == 146
Truth table33%1/3
A or B
A= $token_type == 145B= $token_type == 146
| |||||||||||||||||||||||||||||||||
| 64 | 0 | $is_reg_quote = 1; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 2 | return \@violations; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 3 | 1; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||