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 / ControlStructures / ProhibitUnreachableCode.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ControlStructures/ProhibitUnreachableCode.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::ControlStructures::ProhibitUnreachableCode;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 Readonly::Array my @TERMINALS => qw( die exit croak confess );
21 Readonly::Hash my %TERMINALS => hashify( @TERMINALS );
22
23 Readonly::Array my @CONDITIONALS => qw( if unless foreach while until for );
24 Readonly::Hash my %CONDITIONALS => hashify( @CONDITIONALS );
25
26 Readonly::Array my @OPERATORS => qw( && || // and or err ? );
27 Readonly::Hash my %OPERATORS => hashify( @OPERATORS );
28
29 #-----------------------------------------------------------------------------
30
31 Readonly::Scalar my $DESC => q{Unreachable code};
32 Readonly::Scalar my $EXPL => q{Consider removing it};
33
34 #-----------------------------------------------------------------------------
35
36 sub supported_parameters { return ()                 }
37 sub default_severity     { return $SEVERITY_HIGH     }
38 sub default_themes       { return qw( core bugs )    }
39 sub applies_to           { return 'PPI::Token::Word' }
40
41 #-----------------------------------------------------------------------------
42
43 sub violates {
44     my ( $self, $elem, undef ) = @_;
45     return if ! is_function_call($elem);
46
47     my $stmnt = $elem->statement();
48     return if !$stmnt;
49     return if ( !exists $TERMINALS{$elem} ) &&
50         ( !$stmnt->isa('PPI::Statement::Break') );
51
52     # Scan the enclosing statement for conditional keywords or logical
53     # operators.  If any are found, then this the folowing statements
54     # could _potentially_ be executed, so this policy is satisfied.
55
56     # NOTE: When the first operand in an boolean expression is
57     # C<croak> or C<die>, etc., the second operand is technically
58     # unreachable.  But this policy doesn't catch that situation.
59
60     for my $child ( $stmnt->schildren() ) {
61         return if $child->isa('PPI::Token::Operator') && exists $OPERATORS{$child};
62         return if $child->isa('PPI::Token::Word') && exists $CONDITIONALS{$child};
63     }
64
65     # If we get here, then the statement contained an unconditional
66     # die or exit or return.  Then all the subsequent sibling
67     # statements are unreachable, except for those that have labels,
68     # which could be reached from anywhere using C<goto>.  Subroutine
69     # declarations are also exempt for the same reason.  "use" and
70     # "our" statements are exempt because they happen at compile time.
71
72     my @viols = ();
73     while ( $stmnt = $stmnt->snext_sibling() ) {
74         my @children = $stmnt->schildren();
75         last if @children && $children[0]->isa('PPI::Token::Label');
76         next if $stmnt->isa('PPI::Statement::Sub');
77         next if $stmnt->isa('PPI::Statement::End');
78         next if $stmnt->isa('PPI::Statement::Data');
79
80         next if $stmnt->isa('PPI::Statement::Include') &&
81             $stmnt->type() ne 'require';
82
83         next if $stmnt->isa('PPI::Statement::Variable') &&
84             $stmnt->type() eq 'our';
85
86         push @viols, $self->violation( $DESC, $EXPL, $stmnt );
87     }
88
89     return @viols;
90 }
91
92 1;
93
94 __END__
95
96 =pod
97
98 =head1 NAME
99
100 Perl::Critic::Policy::ControlStructures::ProhibitUnreachableCode - Don't write code after an unconditional C<die, exit, or next>.
101
102 =head1 AFFILIATION
103
104 This Policy is part of the core L<Perl::Critic> distribution.
105
106
107 =head1 DESCRIPTION
108
109 This policy prohibits code following a statement which unconditionally alters
110 the program flow.  This includes calls to C<exit>, C<die>, C<return>, C<next>,
111 C<last> and C<goto>.  Due to common usage, C<croak> and C<confess> from
112 L<Carp> are also included.
113
114 Code is reachable if any of the following conditions are true:
115
116 =over 4
117
118 =item * Flow-altering statement has a conditional attached to it
119
120 =item * Statement is on the right side of an operator C<&&>, C<||>, C<//>, C<and>, C<or>, or C<err>.
121
122 =item * Code is prefixed with a label (can potentially be reached via C<goto>)
123
124 =item * Code is a subroutine
125
126 =back
127
128 =head1 EXAMPLES
129
130   # not ok
131
132   exit;
133   print "123\n";
134
135   # ok
136
137   exit if !$xyz;
138   print "123\n";
139
140   # not ok
141
142   for ( 1 .. 10 ) {
143       next;
144       print 1;
145   }
146
147   # ok
148
149   for ( 1 .. 10 ) {
150       next if $_ == 5;
151       print 1;
152   }
153
154   # not ok
155
156   sub foo {
157       my $bar = shift;
158       return;
159       print 1;
160   }
161
162   # ok
163
164   sub foo {
165       my $bar = shift;
166       return if $bar->baz();
167       print 1;
168   }
169
170
171   # not ok
172
173   die;
174   print "123\n";
175
176   # ok
177
178   die;
179   LABEL: print "123\n";
180
181   # not ok
182
183   croak;
184   do_something();
185
186   # ok
187
188   croak;
189   sub do_something {}
190
191
192 =head1 CONFIGURATION
193
194 This Policy is not configurable except for the standard options.
195
196
197 =head1 SEE ALSO
198
199 L<Perl::Critic::Policy::ControlStructures::ProhibitPostfixControls>
200
201 =head1 AUTHOR
202
203 Peter Guzis <pguzis@cpan.org>
204
205 =head1 COPYRIGHT
206
207 Copyright (c) 2006-2008 Peter Guzis.  All rights reserved.
208
209 This program is free software; you can redistribute it and/or modify
210 it under the same terms as Perl itself.  The full text of this license
211 can be found in the LICENSE file included with this module.
212
213 =cut
214
215 # Local Variables:
216 #   mode: cperl
217 #   cperl-indent-level: 4
218 #   fill-column: 78
219 #   indent-tabs-mode: nil
220 #   c-indentation-style: bsd
221 # End:
222 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :