blib/lib/Perl/Critic/Policy/Subroutines/RequireFinalReturn.pm

filter: Statementstmt 52.7% 67 / 127 Branchbran 22.0% 15 / 68 Conditioncond 18.0% 9 / 50 MC/DCmcdc 3.2% 1 / 31 Subroutinesub 72.4% 21 / 29 Podpod 80.0% 4 / 5 totaltotal 37.7% 117 / 310 CC 55 SCAR 39.9
CC
55
Cov
37%
_is_given_when_return
192.0
_is_terminal_stmnt
72.0
_is_compound_return
50.9
CRAP 54.2
« blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitComplexMappings.pm ↑ Summary blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringyEval.pm »
1 package Perl::Critic::Policy::Subroutines::RequireFinalReturn;
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 List::SomeUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Perl::Critic::Utils qw{ :characters :severities :data_conversion };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.150';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 Readonly::Scalar my $EXPL => [ 197 ];
18  
19 40 Readonly::Hash my %CONDITIONALS => hashify( qw(if unless for foreach) );
20  
21 #-----------------------------------------------------------------------------
22  
23 sub supported_parameters {
24 return (
25 {
26 92 ▶ name => 'terminal_funcs',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
27 description => 'The additional subroutines to treat as terminal.',
28 default_string => $EMPTY,
29 behavior => 'string list',
30 list_always_present_values =>
31 [ qw< croak confess die exec exit throw Carp::confess Carp::croak ...> ],
32 },
33 {
34 name => 'terminal_methods',
35 description => 'The additional methods to treat as terminal.',
36 default_string => $EMPTY,
37 behavior => 'string list',
38 list_always_present_values => [],
39 },
40 );
41 }
42  
43 76 ▶sub default_severity { return $SEVERITY_HIGH }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
44 92 ▶sub default_themes { return qw( core bugs pbp certrec ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
45 33 ▶sub applies_to { return 'PPI::Statement::Sub' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
46  
47 #-----------------------------------------------------------------------------
48  
49 sub violates {
50 5 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=7 SCAR=21.0
Pod documented
51  
52 # skip BEGIN{} and INIT{} and END{} etc
53 5 ▶ return if $elem->isa('PPI::Statement::Scheduled');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Statement::Scheduled") 0 5 5
54  
55 5 my @blocks = grep {$_->isa('PPI::Structure::Block')} $elem->schildren();
56 5 ▶ if (@blocks > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@blocks > 1) { } 0 5 5
elsif (@blocks == 0) { } 0 5 5
57 # sanity check
58 0 throw_internal 'Subroutine should have no more than one block';
59 }
60 elsif (@blocks == 0) {
61 #Technically, subroutines don't have to have a block at all. In
62 # that case, its just a declaration so this policy doesn't really apply
63 0 return; # ok!
64 }
65  
66  
67 5 my ($block) = @blocks;
68 5 ▶ if ($self->_block_is_empty($block) || $self->_block_has_return($block)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_block_is_empty($block) or $self->_block_has_return($block)) 4 1 5
Condition66%2/3
$self->_block_is_empty($block) or $self->_block_has_return($block)
l!l&&r!l&&!r
041
MC/DC0%0/2
$self->_block_is_empty($block) or $self->_block_has_return($block)
$self->_block_is_empty($block) $self->_block_has_return($block)
Truth table33%1/3
A or B
A= $self->_block_is_empty($block)B= $self->_block_has_return($block)
ABresult
1X1
011
000
69 4 return; # OK
70 }
71  
72 # Must be a violation
73 1 my $desc;
74 1 ▶ if ( my $name = $elem->name() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $elem->name) { } 1 0 1
75 1 $desc = qq<Subroutine "$name" does not end with "return">;
76 }
77 else {
78 0 $desc = q<Subroutine does not end with "return">;
79 }
80  
81 1 return $self->violation( $desc, $EXPL, $elem );
82 }
83  
84 #-----------------------------------------------------------------------------
85  
86 sub _block_is_empty {
87 5 ▶ my ( $self, $block ) = @_;
Subroutine _block_is_empty: called CC=1 SCAR=0.0
88 5 return $block->schildren() == 0;
89 }
90  
91 #-----------------------------------------------------------------------------
92  
93 sub _block_has_return {
94 5 ▶ my ( $self, $block ) = @_;
Subroutine _block_has_return: called CC=4 SCAR=14.3
95 5 my @blockparts = $block->schildren();
96 5 my $final = $blockparts[-1]; # always defined because we call _block_is_empty first
97 5 ▶ return if !$final;
Branch: 1 branch
BranchTrueFalseTotal
unless $final 0 5 5
98 5 ▶ return $self->_is_explicit_return($final)
Condition66%4/6
$self->_is_explicit_return($final) || $self->_is_given_when_return($final) || $self->_is_compound_return($final)
l!l&&r!l&&!r
401
$self->_is_explicit_return($final) or $self->_is_given_when_return($final)
l!l&&r!l&&!r
401
MC/DC33%1/3
$self->_is_explicit_return($final) || $self->_is_given_when_return($final) || $self->_is_compound_return($final)
$self->_is_explicit_return($final) $self->_is_given_when_return($final) $self->_is_compound_return($final)
Truth table50%2/4
A or B || C
A= $self->_is_explicit_return($final)B= $self->_is_given_when_return($final)C= $self->_is_compound_return($final)
ABCresult
1XX1
01X1
0011
0000
99 || $self->_is_given_when_return($final)
100 || $self->_is_compound_return($final);
101 }
102  
103 #-----------------------------------------------------------------------------
104  
105 sub _is_explicit_return {
106 5 ▶ my ( $self, $final ) = @_;
Subroutine _is_explicit_return: called CC=3 SCAR=11.4
107  
108 5 ▶ return if $self->_is_conditional_stmnt( $final );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_conditional_stmnt($final) 1 4 5
109 4 ▶ return $self->_is_return_or_goto_stmnt( $final )
Condition33%1/3
$self->_is_return_or_goto_stmnt($final) || $self->_is_terminal_stmnt($final)
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->_is_return_or_goto_stmnt($final) || $self->_is_terminal_stmnt($final)
$self->_is_return_or_goto_stmnt($final) $self->_is_terminal_stmnt($final)
Truth table33%1/3
A || B
A= $self->_is_return_or_goto_stmnt($final)B= $self->_is_terminal_stmnt($final)
ABresult
1X1
011
000
110 || $self->_is_terminal_stmnt( $final );
111 }
112  
113 #-----------------------------------------------------------------------------
114  
115 sub _is_compound_return {
116 1 ▶ my ( $self, $final ) = @_;
Subroutine _is_compound_return: called CC=8 SCAR=39.3
117  
118 1 ▶ if (!$final->isa('PPI::Statement::Compound')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($final->isa("PPI::Statement::Compound")) 1 0 1
119 1 return; #fail
120 }
121  
122 0 my $begin = $final->schild(0);
123 0 ▶ return if !$begin; #fail
Branch: 1 branch
BranchTrueFalseTotal
unless $begin 0 0 0
124  
125 0 state $is_if_or_unless = { hashify( qw( if unless ) ) };
126 0 ▶ if (!($begin->isa('PPI::Token::Word') && $is_if_or_unless->{$begin->content()})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($begin->isa("PPI::Token::Word") and $$is_if_or_unless{$begin->content}) 0 0 0
Condition0%0/3
$begin->isa("PPI::Token::Word") and $$is_if_or_unless{$begin->content}
!ll&&!rl&&r
000
MC/DC0%0/2
$begin->isa("PPI::Token::Word") and $$is_if_or_unless{$begin->content}
$begin->isa("PPI::Token::Word") $$is_if_or_unless{$begin->content}
Truth table0%0/3
A and B
A= $begin->isa("PPI::Token::Word")B= $$is_if_or_unless{$begin->content}
ABresult
0X0
100
111
127 0 return; #fail
128 }
129  
130 0 ▶ my @blocks = grep {!$_->isa('PPI::Structure::Condition') &&
Condition0%0/3
$_->isa('PPI::Structure::Condition') || $_->isa('PPI::Token')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Structure::Condition') || $_->isa('PPI::Token')
$_->isa('PPI::Structure::Condition') $_->isa('PPI::Token')
Truth table0%0/3
A || B
A= $_->isa('PPI::Structure::Condition')B= $_->isa('PPI::Token')
ABresult
1X1
011
000
131 !$_->isa('PPI::Token')} $final->schildren();
132 # Sanity check:
133 0 ▶ if (any { !$_->isa('PPI::Structure::Block') } @blocks) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_->isa("PPI::Structure::Block"); } , @blocks)) 0 0 0
134 0 throw_internal
135 'Expected only conditions, blocks and tokens in the if statement';
136 }
137  
138 0 ▶ return if any { ! $self->_block_has_return($_) } @blocks;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not $self->_block_has_return($_); } , @blocks) 0 0 0
139  
140 0 return 1;
141 }
142  
143 #-----------------------------------------------------------------------------
144  
145 sub _is_given_when_return {
146 1 ▶ my ( $self, $final ) = @_;
Subroutine _is_given_when_return: called CC=15 SCAR=52.6
147  
148 1 ▶ if ( ! $final->isa( 'PPI::Statement::Given' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($final->isa("PPI::Statement::Given")) 1 0 1
149 1 return; #fail
150 }
151  
152 0 my $begin = $final->schild(0);
153 0 ▶ return if !$begin; #fail
Branch: 1 branch
BranchTrueFalseTotal
unless $begin 0 0 0
154 0 ▶ if ( ! ( $begin->isa( 'PPI::Token::Word' ) &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($begin->isa("PPI::Token::Word") and $begin->content eq "given") 0 0 0
Condition0%0/3
$begin->isa("PPI::Token::Word") and $begin->content eq "given"
!ll&&!rl&&r
000
MC/DC0%0/2
$begin->isa("PPI::Token::Word") and $begin->content eq "given"
$begin->isa("PPI::Token::Word") $begin->content eq "given"
Truth table0%0/3
A and B
A= $begin->isa("PPI::Token::Word")B= $begin->content eq "given"
ABresult
0X0
100
111
155 $begin->content() eq 'given' ) ) {
156 0 return; #fail
157 }
158  
159 0 ▶ my @blocks = grep {!$_->isa( 'PPI::Structure::Given' ) &&
Condition0%0/3
$_->isa('PPI::Structure::Given') || $_->isa('PPI::Token')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Structure::Given') || $_->isa('PPI::Token')
$_->isa('PPI::Structure::Given') $_->isa('PPI::Token')
Truth table0%0/3
A || B
A= $_->isa('PPI::Structure::Given')B= $_->isa('PPI::Token')
ABresult
1X1
011
000
160 !$_->isa( 'PPI::Token' )} $final->schildren();
161 # Sanity check:
162 0 ▶ if (any { !$_->isa('PPI::Structure::Block') } @blocks) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_->isa("PPI::Structure::Block"); } , @blocks)) 0 0 0
163 0 throw_internal
164 'Expected only givens, blocks and tokens in the given statement';
165 }
166 0 ▶ if (@blocks > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@blocks > 1) 0 0 0
167 # sanity check
168 0 throw_internal 'Given statement should have no more than one block';
169 }
170 0 ▶ @blocks or return; #fail
Branch: 1 branch
BranchTrueFalseTotal
@blocks or return 0 0 0
171  
172 0 my $have_default; # We have to fail unless a default block is present
173  
174 0 foreach my $stmnt ( $blocks[0]->schildren() ) {
175  
176 0 ▶ if ( $stmnt->isa( 'PPI::Statement::When' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmnt->isa('PPI::Statement::When')) { } 0 0 0
177  
178 # Check for the default block.
179 0 my $first_token;
180 0 ▶ $first_token = $stmnt->schild( 0 )
Branch: 1 branch
BranchTrueFalseTotal
$first_token = $stmnt->schild(0) and "default" eq $first_token->content and $have_default = 1 0 0 0
Condition0%0/3
$first_token = $stmnt->schild(0) and "default" eq $first_token->content
!ll&&!rl&&r
000
MC/DC0%0/2
$first_token = $stmnt->schild(0) and "default" eq $first_token->content
$first_token = $stmnt->schild(0) "default" eq $first_token->content
Truth table0%0/3
A and B
A= $first_token = $stmnt->schild(0)B= "default" eq $first_token->content
ABresult
0X0
100
111
181 and 'default' eq $first_token->content()
182 and $have_default = 1;
183  
184 0 ▶ $self->_is_when_stmnt_with_return( $stmnt )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_when_stmnt_with_return($stmnt) or return 0 0 0
185 or return; #fail
186  
187 } else {
188  
189 0 ▶ $self->_is_suffix_when_with_return( $stmnt )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_suffix_when_with_return($stmnt) or return 0 0 0
190 or return; #fail
191  
192 }
193  
194 }
195  
196 0 return $have_default;
197 }
198  
199 #-----------------------------------------------------------------------------
200  
201 sub _is_return_or_goto_stmnt {
202 4 ▶ my ( $self, $stmnt ) = @_;
Subroutine _is_return_or_goto_stmnt: called CC=4 SCAR=15.6
203 4 ▶ return if not $stmnt->isa('PPI::Statement::Break');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement::Break") 0 4 4
204 4 ▶ my $first_token = $stmnt->schild(0) || return;
Condition50%1/2
$stmnt->schild(0) || (return)
l!l
40
MC/DC0%0/1
$stmnt->schild(0) || (return)
$stmnt->schild(0)
Truth table50%1/2
A
A= $stmnt->schild(0)
Aresult
11
00
205 4 ▶ return $first_token->content() eq 'return'
Condition33%1/3
$first_token->content eq 'return' || $first_token->content eq 'goto'
l!l&&r!l&&!r
400
MC/DC0%0/2
$first_token->content eq 'return' || $first_token->content eq 'goto'
$first_token->content eq 'return' $first_token->content eq 'goto'
Truth table33%1/3
A || B
A= $first_token->content eq 'return'B= $first_token->content eq 'goto'
ABresult
1X1
011
000
206 || $first_token->content() eq 'goto';
207 }
208  
209 #-----------------------------------------------------------------------------
210  
211 sub _is_terminal_stmnt {
212 0 ▶ my ( $self, $stmnt ) = @_;
Subroutine _is_terminal_stmnt: not called CC=8 SCAR=42.8
213  
214 0 ▶ return if not $stmnt->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement") 0 0 0
215  
216 0 ▶ my $first_token = $stmnt->schild(0) || return;
Condition0%0/2
$stmnt->schild(0) || (return)
l!l
00
MC/DC0%0/1
$stmnt->schild(0) || (return)
$stmnt->schild(0)
Truth table0%0/2
A
A= $stmnt->schild(0)
Aresult
11
00
217 0 ▶ return 1 if exists $self->{_terminal_funcs}->{$first_token};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_terminal_funcs"}{$first_token} 0 0 0
218  
219 0 ▶ my $second_token = $stmnt->schild(1) || return;
Condition0%0/2
$stmnt->schild(1) || (return)
l!l
00
MC/DC0%0/1
$stmnt->schild(1) || (return)
$stmnt->schild(1)
Truth table0%0/2
A
A= $stmnt->schild(1)
Aresult
11
00
220 0 ▶ return if not ( $second_token->isa('PPI::Token::Operator') && ($second_token eq q{->}) );
Branch: 1 branch
BranchTrueFalseTotal
unless $second_token->isa("PPI::Token::Operator") and $second_token eq "->" 0 0 0
Condition0%0/3
$second_token->isa("PPI::Token::Operator") and $second_token eq "->"
!ll&&!rl&&r
000
MC/DC0%0/2
$second_token->isa("PPI::Token::Operator") and $second_token eq "->"
$second_token->isa("PPI::Token::Operator") $second_token eq "->"
Truth table0%0/3
A and B
A= $second_token->isa("PPI::Token::Operator")B= $second_token eq "->"
ABresult
0X0
100
111
221  
222 0 ▶ my $third_token = $stmnt->schild(2) || return;
Condition0%0/2
$stmnt->schild(2) || (return)
l!l
00
MC/DC0%0/1
$stmnt->schild(2) || (return)
$stmnt->schild(2)
Truth table0%0/2
A
A= $stmnt->schild(2)
Aresult
11
00
223 0 return exists $self->{_terminal_methods}->{$third_token};
224 }
225  
226 #-----------------------------------------------------------------------------
227  
228 sub _is_conditional_stmnt {
229 5 ▶ my ( $self, $stmnt ) = @_;
Subroutine _is_conditional_stmnt: called CC=2 SCAR=7.0
230 5 ▶ return if not $stmnt->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement") 0 5 5
231  
232 5 ▶ return any { exists $CONDITIONALS{$_} && $_->isa('PPI::Token::Word') } $stmnt->schildren();
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $CONDITIONALS{$_} and $_->isa("PPI::Token::Word") 1 14 15
233 }
234  
235 #-----------------------------------------------------------------------------
236  
237 sub _is_when_stmnt_with_return {
238 0 ▶ my ( $self, $stmnt ) = @_;
Subroutine _is_when_stmnt_with_return: not called CC=6 SCAR=37.4
239  
240 0 ▶ my @inner = grep { ! $_->isa( 'PPI::Token' ) &&
Condition0%0/3
$_->isa('PPI::Token') || $_->isa('PPI::Structure::When')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa('PPI::Token') || $_->isa('PPI::Structure::When')
$_->isa('PPI::Token') $_->isa('PPI::Structure::When')
Truth table0%0/3
A || B
A= $_->isa('PPI::Token')B= $_->isa('PPI::Structure::When')
ABresult
1X1
011
000
241 ! $_->isa( 'PPI::Structure::When' ) }
242 $stmnt->schildren();
243 0 ▶ if ( any { ! $_->isa( 'PPI::Structure::Block' ) } @inner ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not $_->isa("PPI::Structure::Block"); } , @inner)) 0 0 0
244 0 throw_internal 'When statement should contain only tokens, conditions, and blocks';
245 }
246 0 ▶ @inner > 1
Branch: 1 branch
BranchTrueFalseTotal
@inner > 1 and throw_internal("When statement should have no more than one block") 0 0 0
247 and throw_internal 'When statement should have no more than one block';
248 0 ▶ @inner or return; #fail
Branch: 1 branch
BranchTrueFalseTotal
@inner or return 0 0 0
249  
250 0 ▶ return if any { ! $self->_block_has_return( $_ ) } @inner;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { not $self->_block_has_return($_); } , @inner) 0 0 0
251  
252 0 return 1; #succeed
253 }
254  
255 #-----------------------------------------------------------------------------
256  
257 sub _is_suffix_when_with_return {
258 0 ▶ my ( $self, $stmnt ) = @_;
Subroutine _is_suffix_when_with_return: not called CC=6 SCAR=37.4
259 0 ▶ return if not $stmnt->isa('PPI::Statement');
Branch: 1 branch
BranchTrueFalseTotal
unless $stmnt->isa("PPI::Statement") 0 0 0
260 0 foreach my $elem ( $stmnt->schildren() ) {
261 0 ▶ return ( $self->_is_return_or_goto_stmnt( $stmnt ) ||
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("PPI::Token::Word") and "when" eq $elem->content 0 0 0
Condition0%0/6
$elem->isa("PPI::Token::Word") and "when" eq $elem->content
!ll&&!rl&&r
000
$self->_is_return_or_goto_stmnt($stmnt) || $self->_is_terminal_stmnt($stmnt)
l!l&&r!l&&!r
000
MC/DC0%0/2
$elem->isa("PPI::Token::Word") and "when" eq $elem->content
$elem->isa("PPI::Token::Word") "when" eq $elem->content
MC/DC0%0/2
$self->_is_return_or_goto_stmnt($stmnt) || $self->_is_terminal_stmnt($stmnt)
$self->_is_return_or_goto_stmnt($stmnt) $self->_is_terminal_stmnt($stmnt)
Truth table0%0/3
A and B
A= $elem->isa("PPI::Token::Word")B= "when" eq $elem->content
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $self->_is_return_or_goto_stmnt($stmnt)B= $self->_is_terminal_stmnt($stmnt)
ABresult
1X1
011
000
262 $self->_is_terminal_stmnt( $stmnt ) )
263 if $elem->isa( 'PPI::Token::Word' )
264 && 'when' eq $elem->content();
265 }
266 0 return;
267 }
268  
269 40 1;
270  
271 __END__
272  
273 #-----------------------------------------------------------------------------
274  
275 =pod
276  
277 =head1 NAME
278  
279 Perl::Critic::Policy::Subroutines::RequireFinalReturn - End every path through a subroutine with an explicit C<return> statement.
280  
281 =head1 AFFILIATION
282  
283 This Policy is part of the core L<Perl::Critic|Perl::Critic>
284 distribution.
285  
286  
287 =head1 DESCRIPTION
288  
289 Require all subroutines to terminate explicitly with one of the
290 following: C<return>, C<carp>, C<croak>, C<die>, C<exec>, C<exit>,
291 C<goto>, or C<throw>.
292  
293 Subroutines without explicit return statements at their ends can be
294 confusing. It can be challenging to deduce what the return value will
295 be.
296  
297 Furthermore, if the programmer did not mean for there to be a
298 significant return value, and omits a return statement, some of the
299 subroutine's inner data can leak to the outside. Consider this case:
300  
301 package Password;
302 # every time the user guesses the password wrong, its value
303 # is rotated by one character
304 my $password;
305 sub set_password {
306 $password = shift;
307 }
308 sub check_password {
309 my $guess = shift;
310 if ($guess eq $password) {
311 unlock_secrets();
312 } else {
313 $password = (substr $password, 1).(substr $password, 0, 1);
314 }
315 }
316 1;
317  
318 In this case, the last statement in check_password() is the
319 assignment. The result of that assignment is the implicit return
320 value, so a wrong guess returns the right password! Adding a
321 C<return;> at the end of that subroutine solves the problem.
322  
323 The only exception allowed is an empty subroutine.
324  
325 Be careful when fixing problems identified by this Policy; don't
326 blindly put a C<return;> statement at the end of every subroutine.
327  
328 =head1 CONFIGURATION
329  
330 If you've created your own terminal functions that behave like C<die>
331 or C<exit>, then you can configure Perl::Critic to recognize those
332 functions as well. Just put something like this in your
333 F<.perlcriticrc>:
334  
335 [Subroutines::RequireFinalReturn]
336 terminal_funcs = quit abort bailout
337  
338 If you've created your own terminal methods, then you can configure
339 Perl::Critic to recognize those methods as well, but the class won't
340 be considered. For example if you define throw_exception as terminal,
341 then any method of that name will be terminal, regardless of class.
342 Just put something like this in your
343 F<.perlcriticrc>:
344  
345 [Subroutines::RequireFinalReturn]
346 terminal_methods = throw_exception
347  
348 =head1 BUGS
349  
350 We do not look for returns inside ternary operators. That
351 construction is too complicated to analyze right now. Besides, a
352 better form is the return outside of the ternary like this: C<return
353 foo ? 1 : bar ? 2 : 3>
354  
355 =head1 AUTHOR
356  
357 Chris Dolan <cdolan@cpan.org>
358  
359 =head1 COPYRIGHT
360  
361 Copyright (c) 2005-2023 Chris Dolan.
362  
363 This program is free software; you can redistribute it and/or modify
364 it under the same terms as Perl itself. The full text of this license
365 can be found in the LICENSE file included with this module.
366  
367 =cut
368  
369 ##############################################################################
370 # Local Variables:
371 # mode: cperl
372 # cperl-indent-level: 4
373 # fill-column: 78
374 # indent-tabs-mode: nil
375 # c-indentation-style: bsd
376 # End:
377 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitComplexMappings.pm ↑ Summary blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringyEval.pm »