1package Perl::Lint::Policy::RegularExpressions::ProhibitUselessTopic;
2use strict;
3use warnings;
4use Perl::Lint::Constants::Type;
5use parent "Perl::Lint::Policy";
6
7use constant {
8    DESC => 'Useless use of $_',
9    EXPL => '$_ should be omitted when matching a regular expression',
10};
11
12sub evaluate {
13    my ($class, $file, $tokens, $src, $args) = @_;
14
15    my @violations;
16    for (my $i = 0, my $token_type; my $token = $tokens->[$i]; $i++) {
17        $token_type = $token->{type};
18
19        if ($token_type == SPECIFIC_VALUE) {
20            $token = $tokens->[++$i] or last;
21            $token_type = $token->{type};
22
23            if ($token_type == REG_OK || $token_type == REG_NOT) {
24                $token = $tokens->[++$i] or last;
25                $token_type = $token->{type};
26
27                if ($token_type != VAR && $token_type != GLOBAL_VAR) {
28                    push @violations, {
29                        filename => $file,
30                        line     => $token->{line},
31                        description => DESC,
32                        explanation => EXPL,
33                        policy => __PACKAGE__,
34                    };
35                }
36            }
37        }
38
39    }
40
41    return \@violations;
42}
43
441;
45
46