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) $
6 ##############################################################################
8 package Perl::Critic::Policy::ControlStructures::ProhibitUnreachableCode;
15 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 Readonly::Array my @TERMINALS => qw( die exit croak confess );
21 Readonly::Hash my %TERMINALS => hashify( @TERMINALS );
23 Readonly::Array my @CONDITIONALS => qw( if unless foreach while until for );
24 Readonly::Hash my %CONDITIONALS => hashify( @CONDITIONALS );
26 Readonly::Array my @OPERATORS => qw( && || // and or err ? );
27 Readonly::Hash my %OPERATORS => hashify( @OPERATORS );
29 #-----------------------------------------------------------------------------
31 Readonly::Scalar my $DESC => q{Unreachable code};
32 Readonly::Scalar my $EXPL => q{Consider removing it};
34 #-----------------------------------------------------------------------------
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' }
41 #-----------------------------------------------------------------------------
44 my ( $self, $elem, undef ) = @_;
45 return if ! is_function_call($elem);
47 my $stmnt = $elem->statement();
49 return if ( !exists $TERMINALS{$elem} ) &&
50 ( !$stmnt->isa('PPI::Statement::Break') );
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.
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.
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};
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.
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');
80 next if $stmnt->isa('PPI::Statement::Include') &&
81 $stmnt->type() ne 'require';
83 next if $stmnt->isa('PPI::Statement::Variable') &&
84 $stmnt->type() eq 'our';
86 push @viols, $self->violation( $DESC, $EXPL, $stmnt );
100 Perl::Critic::Policy::ControlStructures::ProhibitUnreachableCode - Don't write code after an unconditional C<die, exit, or next>.
104 This Policy is part of the core L<Perl::Critic> distribution.
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.
114 Code is reachable if any of the following conditions are true:
118 =item * Flow-altering statement has a conditional attached to it
120 =item * Statement is on the right side of an operator C<&&>, C<||>, C<//>, C<and>, C<or>, or C<err>.
122 =item * Code is prefixed with a label (can potentially be reached via C<goto>)
124 =item * Code is a subroutine
166 return if $bar->baz();
179 LABEL: print "123\n";
194 This Policy is not configurable except for the standard options.
199 L<Perl::Critic::Policy::ControlStructures::ProhibitPostfixControls>
203 Peter Guzis <pguzis@cpan.org>
207 Copyright (c) 2006-2008 Peter Guzis. All rights reserved.
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.
217 # cperl-indent-level: 4
219 # indent-tabs-mode: nil
220 # c-indentation-style: bsd
222 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :