blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitUselessTopic.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 SCAR 0.1
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitUniversalIsa.pm ↑ Summary blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitVoidGrep.pm »
1 package Perl::Lint::Policy::BuiltinFunctions::ProhibitUselessTopic;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Perl::Lint::Constants::Kind;
6 use parent "Perl::Lint::Policy";
7  
8 use constant {
9 DESC => 'Useless use of $_',
10 EXPL_FILETEST => '$_ should be omitted when using a filetest operator',
11 EXPL_FUNCTION => '$_ should be omitted when calling "%s"',
12 EXPL_FUNCTION_SPLIT => '$_ should be omitted when calling "split" with two arguments',
13 };
14  
15 use constant {
16 FILETEST_OPERATORS => {
17 -r => 1,
18 -w => 1,
19 -x => 1,
20 -o => 1,
21 -R => 1,
22 -W => 1,
23 -X => 1,
24 -O => 1,
25 -e => 1,
26 -z => 1,
27 -s => 1,
28 -f => 1,
29 -d => 1,
30 -l => 1,
31 -p => 1,
32 -S => 1,
33 -b => 1,
34 -c => 1,
35 -u => 1,
36 -g => 1,
37 -k => 1,
38 -T => 1,
39 -B => 1,
40 -M => 1,
41 -A => 1,
42 -C => 1,
43 },
44 TOPICAL_FUNCS => {
45 abs => 1,
46 alarm => 1,
47 chomp => 1,
48 chop => 1,
49 chr => 1,
50 chroot => 1,
51 cos => 1,
52 defined => 1,
53 eval => 1,
54 exp => 1,
55 glob => 1,
56 hex => 1,
57 int => 1,
58 lc => 1,
59 lcfirst => 1,
60 length => 1,
61 log => 1,
62 lstat => 1,
63 mkdir => 1,
64 oct => 1,
65 ord => 1,
66 pos => 1,
67 print => 1,
68 quotemeta => 1,
69 readlink => 1,
70 readpipe => 1,
71 ref => 1,
72 require => 1,
73 reverse => 1,
74 rmdir => 1,
75 sin => 1,
76 split => 1,
77 sqrt => 1,
78 stat => 1,
79 study => 1,
80 uc => 1,
81 ucfirst => 1,
82 unlink => 1,
83 unpack => 1,
84 },
85 };
86  
87 sub evaluate {
88 my ($class, $file, $tokens, $src, $args) = @_;
89  
90 my @violations;
91 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
92 my $token_type = $token->{type};
93 my $token_data = $token->{data};
94  
95 if ($token_type == HANDLE && FILETEST_OPERATORS->{$token_data}) {
96 $token = $tokens->[++$i];
97 if ($token->{type} == SPECIFIC_VALUE && $token->{data} eq '$_') {
98 push @violations, {
99 filename => $file,
100 line => $token->{line},
101 description => DESC,
102 explanation => EXPL_FILETEST,
103 policy => __PACKAGE__,
104 };
105 }
106 }
107 elsif ($token_type == BUILTIN_FUNC && TOPICAL_FUNCS->{$token_data}) {
108 # Ignore when reverse() called in context of assigning into array
109 my $function_name = $token_data;
110 if ($function_name eq 'reverse') {
111 my $two_before_token_type = $tokens->[$i-2]->{type};
112 if (
113 $tokens->[$i-1]->{type} == ASSIGN &&
114 (
115 $two_before_token_type == ARRAY_VAR ||
116 $two_before_token_type == LOCAL_ARRAY_VAR ||
117 $two_before_token_type == GLOBAL_ARRAY_VAR
118 )
119 ) {
120 next;
121 }
122 }
123  
124 my $expl = $function_name eq 'split' ? EXPL_FUNCTION_SPLIT
125 : sprintf EXPL_FUNCTION, $function_name;
126  
127 $token = $tokens->[++$i];
128  
129 if ($token->{type} == LEFT_PAREN) {
130 my $left_paren_num = 1;
131 for ($i++; $token = $tokens->[$i]; $i++) {
132 $token_type = $token->{type};
133 if ($token_type == LEFT_PAREN) {
134 $left_paren_num++;
135 }
136 elsif ($token_type == RIGHT_PAREN) {
137 if (--$left_paren_num <= 0) {
138 my $previous_token = $tokens->[$i-1];
139 if (
140 $tokens->[$i-2]->{kind} != KIND_OP &&
141 $previous_token->{type} == SPECIFIC_VALUE &&
142 $previous_token->{data} eq '$_'
143 ) {
144 push @violations, {
145 filename => $file,
146 line => $token->{line},
147 description => DESC,
148 explanation => $expl,
149 policy => __PACKAGE__,
150 };
151 }
152 }
153 last;
154 }
155 }
156 }
157 else {
158 for (; $token = $tokens->[$i]; $i++) {
159 $token_type = $token->{type};
160 if ($token_type == SEMI_COLON) {
161 my $previous_token = $tokens->[$i-1];
162 if (
163 $tokens->[$i-2]->{kind} != KIND_OP &&
164 $previous_token->{type} == SPECIFIC_VALUE &&
165 $previous_token->{data} eq '$_'
166 ) {
167 push @violations, {
168 filename => $file,
169 line => $token->{line},
170 description => DESC,
171 explanation => $expl,
172 policy => __PACKAGE__,
173 };
174 }
175 last;
176 }
177 }
178 }
179 }
180 }
181  
182 return \@violations;
183 }
184  
185 1;
186  
« blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitUniversalIsa.pm ↑ Summary blib/lib/Perl/Lint/Policy/BuiltinFunctions/ProhibitVoidGrep.pm »