blib/lib/Perl/Lint/Policy/ErrorHandling/RequireCheckingReturnValueOfEval.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/ControlStructures/ProhibitUntilBlocks.pm ↑ Summary blib/lib/Perl/Lint/Policy/InputOutput/ProhibitBacktickOperators.pm »
1 package Perl::Lint::Policy::ErrorHandling::RequireCheckingReturnValueOfEval;
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 parent "Perl::Lint::Policy";
6  
7 use constant {
8 DESC => 'Return value of eval not tested.',
9 EXPL => q{You can't depend upon the value of $@/$EVAL_ERROR to tell whether an eval failed.},
10 };
11  
12 sub evaluate {
13 my ($class, $file, $tokens, $args) = @_;
14  
15 my @violations;
16 my $token_num = scalar @$tokens;
17 my $assigned = 0;
18 my $is_in_grep = 0;
19 my $left_paren_num = 0;
20 my $left_brace_num = 0;
21 my $left_bracket_num = 0;
22 for (my $i = 0; $i < $token_num; $i++) {
23 my $token = $tokens->[$i];
24 my $token_type = $token->{type};
25 my $token_data = $token->{data};
26  
27 if ($token_type == ASSIGN) {
28 $assigned = 1;
29 }
30 elsif ($token_type == COMMA) {
31 if (
32 $left_paren_num <= 0 &&
33 $left_brace_num <= 0 &&
34 $left_bracket_num <= 0
35 ) {
36 $assigned = 0;
37 }
38 }
39 elsif ($token_type == LEFT_PAREN) {
40 $left_paren_num++;
41 }
42 elsif ($token_type == LEFT_BRACE) {
43 $left_brace_num++;
44 }
45 elsif ($token_type == LEFT_BRACKET) {
46 $left_bracket_num++;
47 }
48 elsif ($token_type == RIGHT_PAREN) {
49 $left_paren_num--;
50 }
51 elsif ($token_type == RIGHT_BRACE) {
52 $left_brace_num--;
53 }
54 elsif ($token_type == RIGHT_BRACKET) {
55 $left_bracket_num--;
56 }
57 elsif ($token_type == BUILTIN_FUNC && $token_data eq 'grep') {
58 $is_in_grep = 1;
59 }
60 elsif (
61 !$assigned &&
62 !$is_in_grep &&
63 $token_type == BUILTIN_FUNC &&
64 $token_data eq 'eval'
65 ) {
66 my $is_ternary = 0;
67 my $is_proper = 0;
68 my $left_brace_num = 0;
69  
70 # XXX not good cuz run twice
71 for (my $j = $i + 1; $j < $token_num; $j++) {
72 my $token = $tokens->[$j];
73 my $token_type = $token->{type};
74  
75 if ($token_type == LEFT_BRACE) {
76 $left_brace_num++;
77 }
78 elsif ($token_type == RIGHT_BRACE) {
79 $left_brace_num--;
80 }
81 elsif ($token_type == THREE_TERM_OP) {
82 $is_ternary = 1;
83 }
84 elsif (
85 $left_brace_num <= 0 &&
86 (
87 $token_type == AND ||
88 $token_type == OR ||
89 $token_type == ALPHABET_AND ||
90 $token_type == ALPHABET_OR
91 )
92 ) {
93 $is_proper = 1;
94 }
95 elsif (
96 $left_brace_num <= 0 &&
97 $token_type == SEMI_COLON ||
98 !$tokens->[$j+1]
99 ) {
100 last;
101 }
102 }
103  
104 if (!$is_ternary && !$is_proper) {
105 push @violations, {
106 filename => $file,
107 line => $token->{line},
108 description => DESC,
109 explanation => EXPL,
110 policy => __PACKAGE__,
111 };
112 }
113 }
114 elsif ($token_type == IF_STATEMENT) {
115 my $left_paren_num = 0;
116 my $left_paren_found = 0;
117 my $exists_eval = 0;
118 my $exists_eval_global = 0;
119 my $exists_comma = 0;
120 my $last_token = {type => -1, data => ''};
121 for ($i++; $i < $token_num; $i++) {
122 my $token = $tokens->[$i];
123 my $token_type = $token->{type};
124  
125 if ($token_type == BUILTIN_FUNC && $token->{data} eq 'eval') {
126 $exists_eval = 1;
127 $exists_eval_global = 1;
128 }
129 elsif ($token_type == COMMA) {
130 $exists_comma = 1;
131  
132 my $next_token = $tokens->[$i + 1];
133 my $next_token_type = $next_token->{type};
134 if (
135 $next_token_type != COMMA &&
136 $next_token_type != RIGHT_PAREN
137 ) {
138 $last_token = $next_token;
139 }
140 }
141 elsif ($token_type == LEFT_PAREN) {
142 $left_paren_num++;
143 $left_paren_found++;
144 $exists_eval = 0;
145 $exists_comma = 0;
146 }
147 elsif ($token_type == RIGHT_PAREN) {
148 if (
149 $last_token->{type} != BUILTIN_FUNC &&
150 $last_token->{data} ne 'eval' &&
151 ($exists_eval_global || $exists_eval) && $exists_comma
152 ) {
153 push @violations, {
154 filename => $file,
155 line => $token->{line},
156 description => DESC,
157 explanation => EXPL,
158 policy => __PACKAGE__,
159 };
160 }
161 $left_paren_num--;
162 $exists_eval = 0;
163 $exists_comma = 0;
164 }
165  
166 if (
167 ($left_paren_num <= 0 && $left_paren_found) ||
168 $token == SEMI_COLON ||
169 !$tokens->[$i+1]
170 ) {
171 last;
172 }
173 }
174 }
175 elsif (
176 $token_type == FOREACH_STATEMENT ||
177 $token_type == WHILE_STATEMENT
178 ) {
179 my $left_paren_num = 0;
180 my $left_paren_found = 0;
181 for ($i++; $i < $token_num; $i++) {
182 my $token = $tokens->[$i];
183 my $token_type = $token->{type};
184  
185 if ($token_type == LEFT_PAREN) {
186 $left_paren_num++;
187 $left_paren_found++;
188 }
189 elsif ($token_type == RIGHT_PAREN) {
190 $left_paren_num--;
191 }
192  
193 if (
194 ($left_paren_num <= 0 && $left_paren_found) ||
195 $token == SEMI_COLON ||
196 !$tokens->[$i+1]
197 ) {
198 last;
199 }
200 }
201 }
202 elsif ($token_type == FOR_STATEMENT) {
203 my $left_paren_num = 0;
204 my $left_paren_found = 0;
205 my $assigned = 0;
206 my $block = 1;
207 for ($i++; $i < $token_num; $i++) {
208 my $token = $tokens->[$i];
209 my $token_type = $token->{type};
210  
211 if ($token_type == ASSIGN) {
212 $assigned = 1;
213 }
214 elsif ($token_type == LEFT_PAREN) {
215 $left_paren_num++;
216 $left_paren_found++;
217 }
218 elsif ($token_type == RIGHT_PAREN) {
219 $left_paren_num--;
220 }
221 elsif ($token_type == SEMI_COLON) {
222 $block++;
223 $assigned = 0;
224 }
225 elsif (
226 !$assigned &&
227 $block & 1 &&
228 $token_type == BUILTIN_FUNC &&
229 $token->{data} eq 'eval'
230 ) {
231 push @violations, {
232 filename => $file,
233 line => $token->{line},
234 description => DESC,
235 explanation => EXPL,
236 policy => __PACKAGE__,
237 };
238 }
239  
240 if ($left_paren_num <= 0 && $left_paren_found) {
241 last;
242 }
243 }
244 }
245 elsif ($token_type == SEMI_COLON || !$tokens->[$i+1]) {
246 $assigned = 0;
247 $is_in_grep = 0;
248 }
249 }
250 return \@violations;
251 }
252  
253 1;
254  
« blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitUntilBlocks.pm ↑ Summary blib/lib/Perl/Lint/Policy/InputOutput/ProhibitBacktickOperators.pm »