Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / ErrorHandling / RequireCheckingReturnValueOfEval.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ErrorHandling/RequireCheckingReturnValueOfEval.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13
14 use Readonly;
15
16 use Scalar::Util qw< refaddr >;
17
18 use Perl::Critic::Utils qw< :booleans :characters :severities hashify >;
19 use base 'Perl::Critic::Policy';
20
21 our $VERSION = '1.088';
22
23 #-----------------------------------------------------------------------------
24
25 Readonly::Scalar my $DESC => 'Return value of eval not tested.';
26 ## no critic (RequireInterpolationOfMetachars)
27 Readonly::Scalar my $EXPL =>
28     q<You can't depend upon the value of $@/$EVAL_ERROR to tell whether an eval failed.>;
29 ## use critic
30
31 Readonly::Hash my %BOOLEAN_OPERATORS => hashify qw< || && // or and >;
32 Readonly::Hash my %POSTFIX_OPERATORS =>
33     hashify qw< for foreach if unless while until >;
34
35 #-----------------------------------------------------------------------------
36
37 sub supported_parameters { return ()                 }
38 sub default_severity     { return $SEVERITY_MEDIUM   }
39 sub default_themes       { return qw( core bugs )    }
40 sub applies_to           { return 'PPI::Token::Word' }
41
42 #-----------------------------------------------------------------------------
43
44 sub violates {
45     my ( $self, $elem, undef ) = @_;
46
47     return if $elem->content() ne 'eval';
48
49     my $evaluated = $elem->snext_sibling() or return; # Nothing to eval!
50     my $following = $evaluated->snext_sibling();
51
52     return if _is_in_right_hand_side_of_assignment($elem);
53     return if _is_in_postfix_expression($elem);
54     return if
55         _is_in_correct_position_in_a_condition_or_foreach_loop_collection(
56             $elem,
57             $following,
58         );
59
60     if (
61             $following
62         and $following->isa('PPI::Token::Operator')
63         and $BOOLEAN_OPERATORS{ $following->content() }
64     ) {
65         return;
66     }
67
68     return $self->violation($DESC, $EXPL, $elem);
69 }
70
71 #-----------------------------------------------------------------------------
72
73 sub _is_in_right_hand_side_of_assignment {
74     my ($elem) = @_;
75
76     my $previous = $elem->sprevious_sibling();
77
78     if (not $previous) {
79         $previous =
80             _grandparent_for_is_in_right_hand_side_of_assignment($elem);
81     }
82
83     while ($previous) {
84         my $base_previous = $previous;
85
86         EQUALS_SCAN:
87         while ($previous) {
88             if ( $previous->isa('PPI::Token::Operator') ) {
89                 return $TRUE if $previous->content() eq q<=>;
90                 last EQUALS_SCAN if _is_effectively_a_comma($previous);
91             }
92             $previous = $previous->sprevious_sibling();
93         }
94
95         $previous =
96             _grandparent_for_is_in_right_hand_side_of_assignment($base_previous);
97     }
98
99     return;
100 }
101
102 sub _grandparent_for_is_in_right_hand_side_of_assignment {
103     my ($elem) = @_;
104
105     my $parent = $elem->parent() or return;
106     $parent->isa('PPI::Statement') or return;
107
108     my $grandparent = $parent->parent() or return;
109
110     if (
111             $grandparent->isa('PPI::Structure::Constructor')
112         or  $grandparent->isa('PPI::Structure::List')
113     ) {
114         return $grandparent;
115     }
116
117     return;
118 }
119
120 #-----------------------------------------------------------------------------
121
122 Readonly::Scalar my $CONDITION_POSITION_IN_C_STYLE_FOR_LOOP => 1;
123
124 sub _is_in_correct_position_in_a_condition_or_foreach_loop_collection {
125     my ($elem, $following) = @_;
126
127     my $parent = $elem->parent();
128     while ($parent) {
129         if ( $parent->isa('PPI::Structure::Condition') ) {
130             return
131                 _is_in_correct_position_in_a_structure_condition(
132                     $elem, $parent, $following,
133                 );
134         }
135
136         if ( $parent->isa('PPI::Structure::ForLoop') ) {
137             my @for_loop_components = $parent->schildren();
138
139             return $TRUE if 1 == @for_loop_components;
140             my $condition =
141                 $for_loop_components[$CONDITION_POSITION_IN_C_STYLE_FOR_LOOP]
142                 or return;
143
144             return _descendant_of($elem, $condition);
145         }
146
147         $parent = $parent->parent();
148     }
149
150     return;
151 }
152
153 sub _is_in_correct_position_in_a_structure_condition {
154     my ($elem, $parent, $following) = @_;
155
156     my $level = $elem;
157     while ($level and refaddr $level != $parent) {
158         my $cursor = refaddr $elem == refaddr $level ? $following : $level;
159
160         IS_FINAL_EXPRESSION_AT_DEPTH:
161         while ($cursor) {
162             if ( _is_effectively_a_comma($cursor) ) {
163                 $cursor = $cursor->snext_sibling();
164                 while ( _is_effectively_a_comma($cursor) ) {
165                     $cursor = $cursor->snext_sibling();
166                 }
167
168                 # Semicolon would be a syntax error here.
169                 return if $cursor;
170                 last IS_FINAL_EXPRESSION_AT_DEPTH;
171             }
172
173             $cursor = $cursor->snext_sibling();
174         }
175
176         my $statement = $level->parent();
177         return $TRUE if not $statement; # Shouldn't happen.
178         return $TRUE if not $statement->isa('PPI::Statement'); # Shouldn't happen.
179
180         $level = $statement->parent();
181         if (
182                 not $level
183             or  (
184                     not $level->isa('PPI::Structure::List')
185                 and not $level->isa('PPI::Structure::Condition')
186             )
187         ) {
188             # Shouldn't happen.
189             return $TRUE;
190         }
191     }
192
193     return $TRUE;
194 }
195
196 # Replace with PPI implementation once it is released.
197 sub _descendant_of {
198     my ($cursor, $potential_ancestor) = @_;
199
200     return $EMPTY if not $potential_ancestor;
201
202     while ( refaddr $cursor != refaddr $potential_ancestor ) {
203         $cursor = $cursor->parent() or return $EMPTY;
204     }
205
206     return 1;
207 }
208
209 #-----------------------------------------------------------------------------
210
211 sub _is_in_postfix_expression {
212     my ($elem) = @_;
213
214     my $previous = $elem->sprevious_sibling();
215     while ($previous) {
216         if (
217                 $previous->isa('PPI::Token::Word')
218             and $POSTFIX_OPERATORS{ $previous->content() }
219         ) {
220             return $TRUE
221         }
222         $previous = $previous->sprevious_sibling();
223     }
224
225     return;
226 }
227
228 #-----------------------------------------------------------------------------
229
230 sub _is_effectively_a_comma {
231     my ($elem) = @_;
232
233     return if not $elem;
234
235     return
236             $elem->isa('PPI::Token::Operator')
237         &&  (
238                 $elem->content() eq $COMMA
239             ||  $elem->content() eq $FATCOMMA
240         );
241 }
242
243 #-----------------------------------------------------------------------------
244
245 1;
246
247 __END__
248
249 #-----------------------------------------------------------------------------
250
251 =pod
252
253 =for stopwords destructors
254
255 =head1 NAME
256
257 Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval - You can't depend upon the value of C<$@>/C<$EVAL_ERROR> to tell whether an C<eval> failed.
258
259 =head1 AFFILIATION
260
261 This Policy is part of the core L<Perl::Critic> distribution.
262
263
264 =head1 DESCRIPTION
265
266 A common idiom in perl for dealing with possible errors is to use
267 C<eval> followed by a check of C<$@>/C<$EVAL_ERROR>:
268
269     eval {
270         ...
271     };
272     if ($EVAL_ERROR) {
273         ...
274     }
275
276 There's a problem with this: the value of C<$EVAL_ERROR> can change
277 between the end of the C<eval> and the C<if> statement.  The issue is
278 object destructors:
279
280     package Foo;
281
282     ...
283
284     sub DESTROY {
285         ...
286         eval { ... };
287         ...
288     }
289
290     package main;
291
292     eval {
293         my $foo = Foo->new();
294         ...
295     };
296     if ($EVAL_ERROR) {
297         ...
298     }
299
300 Assuming there are no other references to C<$foo> created, when the
301 C<eval> block in C<main> is exited, C<Foo::DESTROY()> will be invoked,
302 regardless of whether the C<eval> finished normally or not.  If the
303 C<eval> in C<main> fails, but the C<eval> in C<Foo::DESTROY()>
304 succeeds, then C<$EVAL_ERROR> will be empty by the time that the C<if>
305 is executed.  Additional issues arise if you depend upon the exact
306 contents of C<$EVAL_ERROR> and both C<eval>s fail, because the
307 messages from both will be concatenated.
308
309 Even if there isn't an C<eval> directly in the C<DESTROY()> method
310 code, it may invoke code that does use C<eval> or otherwise affects
311 C<$EVAL_ERROR>.
312
313 The solution is to ensure that, upon normal exit, an C<eval> returns a
314 true value and to test that value:
315
316     # Constructors are no problem.
317     my $object = eval { Class->new() };
318
319     # To cover the possiblity that an operation may correctly return a
320     # false value, end the block with "1":
321     if ( eval { something(); 1 } ) {
322         ...
323     }
324
325     eval {
326         ...
327         1;
328     }
329         or do {
330             # Error handling here
331         };
332
333 Unfortunately, you can't use the C<defined> function to test the
334 result; C<eval> returns an empty string on failure.
335
336 "But we don't use DESTROY() anywhere in our code!" you say.  That may
337 be the case, but do any of the third-party modules you use have them?
338 What about any you may use in the future or updated versions of the
339 ones you already use?
340
341
342 =head1 CONFIGURATION
343
344 This Policy is not configurable except for the standard options.
345
346
347 =head1 SEE ALSO
348
349 See thread on perl5-porters starting here:
350 L<http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-06/msg00537.html>.
351
352
353 =head1 AUTHOR
354
355 Elliot Shank C<< <perl@galumph.com> >>
356
357 =head1 COPYRIGHT
358
359 Copyright (c) 2008 Elliot Shank.  All rights reserved.
360
361 This program is free software; you can redistribute it and/or modify
362 it under the same terms as Perl itself.  The full text of this license
363 can be found in the LICENSE file included with this module.
364
365 =cut
366
367 # Local Variables:
368 #   mode: cperl
369 #   cperl-indent-level: 4
370 #   fill-column: 78
371 #   indent-tabs-mode: nil
372 #   c-indentation-style: bsd
373 # End:
374 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :