48a0f631400d788651564585123ffc6a2ec54dc1
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / t / Subroutines / RequireFinalReturn.run
1 ##############################################################################
2 #     $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/t/Subroutines/RequireFinalReturn.run $
3 #    $Date: 2008-03-16 17:40:45 -0500 (Sun, 16 Mar 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2187 $
6 ##############################################################################
7
8
9 #-----------------------------------------------------------------------------
10
11 ## name basic passes
12 ## failures 0
13 ## cut
14 sub foo { }
15 sub bar;
16 sub baz { return; }
17 sub quux { return {some => [qw(complicated data)], q{ } => /structure/}; }
18
19 #-----------------------------------------------------------------------------
20
21 ## name complex passes
22 ## failures 0
23 ## cut
24 sub foo { if ($bool) { return; } else { return; } }
25 sub bar { unless ($bool) { return; } else { return; } }
26 sub baz { if ($bool) { return; } elsif ($bool2) { return; } else { return; } }
27 sub quuz { unless ($bool) { return; } elsif ($bool2) { return; } else { return; } }
28
29 #-----------------------------------------------------------------------------
30
31 ## name ternary returns
32 ## failures 0
33 ## TODO We are not yet detecting ternaries
34 ## cut
35 sub foo { 1 ? return : 2 ? return : return; }
36
37 #-----------------------------------------------------------------------------
38
39 ## name returning ternaries
40 ## failures 0
41 ## cut
42 sub foo { return 1 ? 1 : 2 ? 2 : 3; }
43
44 #-----------------------------------------------------------------------------
45
46 ## name implicit returns fail
47 ## failures 2
48 ## cut
49 sub foo { 1 }
50 sub foo { 'Club sandwich'; }
51
52 #-----------------------------------------------------------------------------
53
54 ## name return in a constant loop
55 ## failures 1
56 ## cut
57 sub foo { while (1==1) { return; } }
58
59 #-----------------------------------------------------------------------------
60
61 ## name not all code paths returns
62 ## failures 3
63 ## cut
64 sub foo { if ($bool) { } else { } }
65 sub foo { if ($bool) { $foo = 'bar'; } else { return; } }
66 sub foo { unless ($bool) { $foo = 'bar'; } else { return; } }
67
68 #-----------------------------------------------------------------------------
69
70 ## name special blocks exemption
71 ## failures 0
72 ## cut
73 BEGIN {
74   print 'this should not need a return';
75 }
76 INIT {
77   print 'nor this';
78 }
79 CHECK {
80   print 'nor this';
81 }
82 END {
83   print 'nor this';
84 }
85
86 #-----------------------------------------------------------------------------
87
88 ## name goto is equivalent to return
89 ## failures 0
90 ## cut
91 sub foo { goto &bar; }
92 END_PERL
93
94 #-----------------------------------------------------------------------------
95
96 ## name next and last are not equivalent to return (and are invalid Perl)
97 ## failures 2
98 ## cut
99 sub foo { next; }
100 sub bar { last; }
101
102 #-----------------------------------------------------------------------------
103
104 ## name abnormal termination is allowed
105 ## failures 0
106 ## cut
107 sub foo   { die; }
108 sub bar   { croak; }
109 sub baz   { confess; }
110 sub bar_C { Carp::croak; }
111 sub baz_C { Carp::confess; }
112 sub quux  { exit; }
113 sub quux2 { throw 'nuts'; }
114
115 #-----------------------------------------------------------------------------
116
117 ## name Final return is present, but conditional
118 ## failures 4
119 ## cut
120 sub foo   { die if $condition }
121 sub bar   { croak unless $condition }
122 sub baz   { exit for @condition }
123 sub quux  { throw 'nuts'if not $condition }
124
125 #-----------------------------------------------------------------------------
126
127 ## name Compound final return is present, but conditional
128 ## failures 1
129 ## cut
130 sub foo {
131
132     if( $condition ) {
133         return if $today_is_tuesday;
134     }
135     else {
136         exit unless $today_is_wednesday;
137     }
138 }
139
140 #-----------------------------------------------------------------------------
141
142 ## name Custom terminals
143 ## parms { terminal_funcs => 'bailout abort quit' };
144 ## failures 0
145 ## cut
146 sub foo  { if ($condition) { return 1; }else{ abort } }
147 sub bar  { if ($condition) { bailout }else{ return 1 } }
148 sub baz  { quit }
149
150 ##############################################################################
151 # Local Variables:
152 #   mode: cperl
153 #   cperl-indent-level: 4
154 #   fill-column: 78
155 #   indent-tabs-mode: nil
156 #   c-indentation-style: bsd
157 # End:
158 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :