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