blib/lib/Perl/Critic/Policy/ErrorHandling/RequireCheckingReturnValueOfEval.pm

filter: Statementstmt 26.6% 36 / 135 Branchbran 0.9% 1 / 104 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 57.1% 12 / 21 Podpod 80.0% 4 / 5 totaltotal 15.9% 53 / 332 CC 67 SCAR 44.4
CC
67
Cov
13%
_is_in_correct_position_in_a_structure_condition
182.0
_is_in_postfix_expression
132.0
violates
122.9
CRAP 84.4
« blib/lib/Perl/Critic/Policy/Subroutines/ProhibitUnusedPrivateSubroutines.pm ↑ Summary blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitReverseSortBlock.pm »
1 package Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval;
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  
7 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Scalar::Util qw< refaddr >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 use Perl::Critic::Utils qw< :booleans :characters :severities hashify
12 40 ▶ precedence_of >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 40 our $VERSION = '1.150';
16  
17 #-----------------------------------------------------------------------------
18  
19 40 Readonly::Scalar my $DESC => 'Return value of eval not tested.';
20 ## no critic (RequireInterpolationOfMetachars)
21 40 Readonly::Scalar my $EXPL =>
22 q<You can't depend upon the value of $@/$EVAL_ERROR to tell whether an eval failed.>;
23 ## use critic
24  
25 40 Readonly::Hash my %BOOLEAN_OPERATORS => hashify qw< || && // or and >;
26 40 Readonly::Hash my %POSTFIX_OPERATORS =>
27 hashify qw< for foreach if unless while until >;
28  
29 40 Readonly::Scalar my $PRECEDENCE_OF_EQUALS => precedence_of( q{=} );
30  
31 #-----------------------------------------------------------------------------
32  
33 90 ▶sub supported_parameters { return () }
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
34 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
35 74 ▶sub default_themes { return qw( core bugs ) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
36 2 ▶sub applies_to { return 'PPI::Token::Word' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
37  
38 #-----------------------------------------------------------------------------
39  
40 sub violates {
41 12 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=12 SCAR=48.1
Pod documented
42  
43 12 ▶ return if $elem->content() ne 'eval';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "eval" 12 0 12
44  
45 0 ▶ my $evaluated = $elem->snext_sibling() or return; # Nothing to eval!
Branch: 1 branch
BranchTrueFalseTotal
my $evaluated = $elem->snext_sibling or return 0 0 0
46 0 my $following = $evaluated->snext_sibling();
47  
48 0 ▶ return if _is_returned( $elem ); # GitHub #324
Branch: 1 branch
BranchTrueFalseTotal
if _is_returned($elem) 0 0 0
49 0 ▶ return if _is_in_right_hand_side_of_assignment($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_in_right_hand_side_of_assignment($elem) 0 0 0
50 0 ▶ return if _is_in_postfix_expression($elem);
Branch: 1 branch
BranchTrueFalseTotal
if _is_in_postfix_expression($elem) 0 0 0
51 return if
52 0 ▶ _is_in_correct_position_in_a_condition_or_foreach_loop_collection(
Branch: 1 branch
BranchTrueFalseTotal
if _is_in_correct_position_in_a_condition_or_foreach_loop_collection($elem, $following) 0 0 0
53 $elem,
54 $following,
55 );
56  
57 0 ▶ return if _scan_backwards_for_grep( $elem ); # RT 69489
Branch: 1 branch
BranchTrueFalseTotal
if _scan_backwards_for_grep($elem) 0 0 0
58  
59 0 ▶ if ( $following and $following->isa('PPI::Token::Operator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($following and $following->isa("PPI::Token::Operator")) 0 0 0
Condition0%0/3
$following and $following->isa("PPI::Token::Operator")
!ll&&!rl&&r
000
MC/DC0%0/2
$following and $following->isa("PPI::Token::Operator")
$following $following->isa("PPI::Token::Operator")
Truth table0%0/3
A and B
A= $followingB= $following->isa("PPI::Token::Operator")
ABresult
0X0
100
111
60 0 ▶ return if $BOOLEAN_OPERATORS{ $following->content() };
Branch: 1 branch
BranchTrueFalseTotal
if $BOOLEAN_OPERATORS{$following->content} 0 0 0
61 0 ▶ return if q{?} eq $following->content;
Branch: 1 branch
BranchTrueFalseTotal
if "?" eq $following->content 0 0 0
62 }
63  
64 0 return $self->violation($DESC, $EXPL, $elem);
65 }
66  
67 #-----------------------------------------------------------------------------
68  
69 sub _is_in_right_hand_side_of_assignment {
70 0 ▶ my ($elem) = @_;
Subroutine _is_in_right_hand_side_of_assignment: not called CC=7 SCAR=40.3
71  
72 0 my $previous = $elem->sprevious_sibling();
73  
74 0 ▶ if (not $previous) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($previous) 0 0 0
75 0 $previous =
76 _grandparent_for_is_in_right_hand_side_of_assignment($elem);
77 }
78  
79 0 ▶ while ($previous) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous) 0 0 0
80 0 my $base_previous = $previous;
81  
82 EQUALS_SCAN:
83 0 ▶ while ($previous) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous) 0 0 0
84 0 ▶ if ( $previous->isa('PPI::Token::Operator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous->isa("PPI::Token::Operator")) 0 0 0
85 0 ▶ return $TRUE if $previous->content() =~ m/= \Z/xms;
Branch: 1 branch
BranchTrueFalseTotal
if $previous->content =~ /= \Z/msx 0 0 0
86 0 ▶ last EQUALS_SCAN if _is_effectively_a_comma($previous);
Branch: 1 branch
BranchTrueFalseTotal
if _is_effectively_a_comma($previous) 0 0 0
87 }
88 0 $previous = $previous->sprevious_sibling();
89 }
90  
91 $previous =
92 0 _grandparent_for_is_in_right_hand_side_of_assignment($base_previous);
93 }
94  
95 0 return;
96 }
97  
98 sub _grandparent_for_is_in_right_hand_side_of_assignment {
99 0 ▶ my ($elem) = @_;
Subroutine _grandparent_for_is_in_right_hand_side_of_assignment: not called CC=6 SCAR=37.4
100  
101 0 ▶ my $parent = $elem->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $elem->parent or return 0 0 0
102 0 ▶ $parent->isa('PPI::Statement') or return;
Branch: 1 branch
BranchTrueFalseTotal
$parent->isa("PPI::Statement") or return 0 0 0
103  
104 0 ▶ my $grandparent = $parent->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $grandparent = $parent->parent or return 0 0 0
105  
106 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($grandparent->isa("PPI::Structure::Constructor") or $grandparent->isa("PPI::Structure::List")) 0 0 0
Condition0%0/3
$grandparent->isa("PPI::Structure::Constructor") or $grandparent->isa("PPI::Structure::List")
l!l&&r!l&&!r
000
MC/DC0%0/2
$grandparent->isa("PPI::Structure::Constructor") or $grandparent->isa("PPI::Structure::List")
$grandparent->isa("PPI::Structure::Constructor") $grandparent->isa("PPI::Structure::List")
Truth table0%0/3
A or B
A= $grandparent->isa("PPI::Structure::Constructor")B= $grandparent->isa("PPI::Structure::List")
ABresult
1X1
011
000
107 $grandparent->isa('PPI::Structure::Constructor')
108 or $grandparent->isa('PPI::Structure::List')
109 ) {
110 0 return $grandparent;
111 }
112  
113 0 return;
114 }
115  
116 #-----------------------------------------------------------------------------
117  
118 40 Readonly::Scalar my $CONDITION_POSITION_IN_C_STYLE_FOR_LOOP => 1;
119  
120 sub _is_in_correct_position_in_a_condition_or_foreach_loop_collection {
121 0 ▶ my ($elem, $following) = @_;
Subroutine _is_in_correct_position_in_a_condition_or_foreach_loop_collection: not called CC=9 SCAR=45.0
122  
123 0 my $parent = $elem->parent();
124 0 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 0 0 0
125 0 ▶ if ( $parent->isa('PPI::Structure::Condition') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Structure::Condition")) 0 0 0
126 return
127 0 _is_in_correct_position_in_a_structure_condition(
128 $elem, $parent, $following,
129 );
130 }
131  
132 # TECHNICAL DEBT: This code is basically shared with
133 # ProhibitUnusedCapture. I don't want to put this code
134 # into Perl::Critic::Utils::*, but I don't have time to sort out
135 # PPIx::Utilities::Structure::List yet.
136 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Structure::List") and my $parent_statement = $parent->statement) 0 0 0
Condition0%0/3
$parent->isa("PPI::Structure::List") and my $parent_statement = $parent->statement
!ll&&!rl&&r
000
MC/DC0%0/2
$parent->isa("PPI::Structure::List") and my $parent_statement = $parent->statement
$parent->isa("PPI::Structure::List") my $parent_statement = $parent->statement
Truth table0%0/3
A and B
A= $parent->isa("PPI::Structure::List")B= my $parent_statement = $parent->statement
ABresult
0X0
100
111
137 $parent->isa('PPI::Structure::List')
138 and my $parent_statement = $parent->statement()
139 ) {
140 0 ▶ return $TRUE if
Branch: 1 branch
BranchTrueFalseTotal
if $parent_statement->isa("PPI::Statement::Compound") and $parent_statement->type eq "foreach" 0 0 0
Condition0%0/3
$parent_statement->isa("PPI::Statement::Compound") and $parent_statement->type eq "foreach"
!ll&&!rl&&r
000
MC/DC0%0/2
$parent_statement->isa("PPI::Statement::Compound") and $parent_statement->type eq "foreach"
$parent_statement->isa("PPI::Statement::Compound") $parent_statement->type eq "foreach"
Truth table0%0/3
A and B
A= $parent_statement->isa("PPI::Statement::Compound")B= $parent_statement->type eq "foreach"
ABresult
0X0
100
111
141 $parent_statement->isa('PPI::Statement::Compound')
142 and $parent_statement->type() eq 'foreach';
143 }
144  
145 0 ▶ if ( $parent->isa('PPI::Structure::For') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("PPI::Structure::For")) 0 0 0
146 0 my @for_loop_components = $parent->schildren();
147  
148 0 ▶ my $condition =
Branch: 1 branch
BranchTrueFalseTotal
my $condition = $for_loop_components[$CONDITION_POSITION_IN_C_STYLE_FOR_LOOP] or return 0 0 0
149 $for_loop_components[$CONDITION_POSITION_IN_C_STYLE_FOR_LOOP]
150 or return;
151  
152 0 return _descendant_of($elem, $condition);
153 }
154  
155 0 $parent = $parent->parent();
156 }
157  
158 0 return;
159 }
160  
161 sub _is_in_correct_position_in_a_structure_condition {
162 0 ▶ my ($elem, $parent, $following) = @_;
Subroutine _is_in_correct_position_in_a_structure_condition: not called CC=13 SCAR=52.0
163  
164 0 my $level = $elem;
165 0 ▶ while ($level and refaddr $level != $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level and builtin::refaddr($level) != $parent) 0 0 0
Condition0%0/3
$level and builtin::refaddr($level) != $parent
!ll&&!rl&&r
000
MC/DC0%0/2
$level and builtin::refaddr($level) != $parent
$level builtin::refaddr($level) != $parent
Truth table0%0/3
A and B
A= $levelB= builtin::refaddr($level) != $parent
ABresult
0X0
100
111
166 0 ▶ my $cursor = refaddr $elem == refaddr $level ? $following : $level;
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($elem) == builtin::refaddr($level) ? : 0 0 0
167  
168 IS_FINAL_EXPRESSION_AT_DEPTH:
169 0 ▶ while ($cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor) 0 0 0
170 0 ▶ if ( _is_effectively_a_comma($cursor) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_effectively_a_comma($cursor)) 0 0 0
171 0 $cursor = $cursor->snext_sibling();
172 0 ▶ while ( _is_effectively_a_comma($cursor) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_effectively_a_comma($cursor)) 0 0 0
173 0 $cursor = $cursor->snext_sibling();
174 }
175  
176 # Semicolon would be a syntax error here.
177 0 ▶ return if $cursor;
Branch: 1 branch
BranchTrueFalseTotal
if $cursor 0 0 0
178 0 last IS_FINAL_EXPRESSION_AT_DEPTH;
179 }
180  
181 0 $cursor = $cursor->snext_sibling();
182 }
183  
184 0 my $statement = $level->parent();
185 0 ▶ return $TRUE if not $statement; # Shouldn't happen.
Branch: 1 branch
BranchTrueFalseTotal
unless $statement 0 0 0
186 0 ▶ return $TRUE if not $statement->isa('PPI::Statement'); # Shouldn't happen.
Branch: 1 branch
BranchTrueFalseTotal
unless $statement->isa("PPI::Statement") 0 0 0
187  
188 0 $level = $statement->parent();
189 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($level and $level->isa("PPI::Structure::List") || $level->isa("PPI::Structure::Condition")) 0 0 0
Condition0%0/6
$level and $level->isa("PPI::Structure::List") || $level->isa("PPI::Structure::Condition")
!ll&&!rl&&r
000
$level->isa("PPI::Structure::List") or $level->isa("PPI::Structure::Condition")
l!l&&r!l&&!r
000
MC/DC0%0/3
$level and $level->isa("PPI::Structure::List") || $level->isa("PPI::Structure::Condition")
$level $level->isa("PPI::Structure::List") $level->isa("PPI::Structure::Condition")
Truth table0%0/4
A and B or C
A= $levelB= $level->isa("PPI::Structure::List")C= $level->isa("PPI::Structure::Condition")
ABCresult
0XX0
1000
11X1
1011
190 not $level
191 or (
192 not $level->isa('PPI::Structure::List')
193 and not $level->isa('PPI::Structure::Condition')
194 )
195 ) {
196 # Shouldn't happen.
197 0 return $TRUE;
198 }
199 }
200  
201 0 return $TRUE;
202 }
203  
204 # Replace with PPI implementation once it is released.
205 sub _descendant_of {
206 0 ▶ my ($cursor, $potential_ancestor) = @_;
Subroutine _descendant_of: not called CC=4 SCAR=30.0
207  
208 0 ▶ return $EMPTY if not $potential_ancestor;
Branch: 1 branch
BranchTrueFalseTotal
unless $potential_ancestor 0 0 0
209  
210 0 ▶ while ( refaddr $cursor != refaddr $potential_ancestor ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($cursor) != builtin::refaddr($potential_ancestor)) 0 0 0
211 0 ▶ $cursor = $cursor->parent() or return $EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
$cursor = $cursor->parent or return $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::EMPTY 0 0 0
212 }
213  
214 0 return 1;
215 }
216  
217 #-----------------------------------------------------------------------------
218  
219 sub _is_in_postfix_expression {
220 0 ▶ my ($elem) = @_;
Subroutine _is_in_postfix_expression: not called CC=11 SCAR=48.8
221  
222 0 my $current_base = $elem;
223 0 ▶ while ($TRUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::TRUE) 0 0 0
224 0 my $previous = $current_base->sprevious_sibling();
225 0 ▶ while ($previous) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous) 0 0 0
226 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($previous->isa("PPI::Token::Word") and $POSTFIX_OPERATORS{$previous->content}) 0 0 0
Condition0%0/3
$previous->isa("PPI::Token::Word") and $POSTFIX_OPERATORS{$previous->content}
!ll&&!rl&&r
000
MC/DC0%0/2
$previous->isa("PPI::Token::Word") and $POSTFIX_OPERATORS{$previous->content}
$previous->isa("PPI::Token::Word") $POSTFIX_OPERATORS{$previous->content}
Truth table0%0/3
A and B
A= $previous->isa("PPI::Token::Word")B= $POSTFIX_OPERATORS{$previous->content}
ABresult
0X0
100
111
227 $previous->isa('PPI::Token::Word')
228 and $POSTFIX_OPERATORS{ $previous->content() }
229 ) {
230 0 return $TRUE;
231 }
232 0 $previous = $previous->sprevious_sibling();
233 } # end while
234  
235 0 ▶ my $parent = $current_base->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $current_base->parent or return 0 0 0
236 0 ▶ if ( $parent->isa('PPI::Statement') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa('PPI::Statement')) { } 0 0 0
237 0 ▶ return if $parent->specialized();
Branch: 1 branch
BranchTrueFalseTotal
if $parent->specialized 0 0 0
238  
239 0 ▶ my $grandparent = $parent->parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $grandparent = $parent->parent or return 0 0 0
240 0 ▶ return if not $grandparent->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $grandparent->isa("PPI::Structure::List") 0 0 0
241  
242 0 $current_base = $grandparent;
243 } else {
244 0 $current_base = $parent;
245 }
246  
247 0 ▶ return if not $current_base->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $current_base->isa("PPI::Structure::List") 0 0 0
248 }
249  
250 0 return;
251 }
252  
253 #-----------------------------------------------------------------------------
254  
255 sub _scan_backwards_for_grep {
256 0 ▶ my ( $elem ) = @_;
Subroutine _scan_backwards_for_grep: not called CC=7 SCAR=40.3
257  
258 0 ▶ while ( $elem ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem) 0 0 0
259  
260 0 my $parent = $elem->parent();
261  
262 0 ▶ while ( $elem = $elem->sprevious_sibling() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem = $elem->sprevious_sibling) 0 0 0
263 0 ▶ $elem->isa( 'PPI::Token::Word' )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Word") and "grep" eq $elem->content and return $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::TRUE 0 0 0
Condition0%0/3
$elem->isa("PPI::Token::Word") and "grep" eq $elem->content
!ll&&!rl&&r
000
MC/DC0%0/2
$elem->isa("PPI::Token::Word") and "grep" eq $elem->content
$elem->isa("PPI::Token::Word") "grep" eq $elem->content
Truth table0%0/3
A and B
A= $elem->isa("PPI::Token::Word")B= "grep" eq $elem->content
ABresult
0X0
100
111
264 and 'grep' eq $elem->content()
265 and return $TRUE;
266 0 ▶ $elem->isa( 'PPI::Token::Operator' )
Branch: 1 branch
BranchTrueFalseTotal
$elem->isa("PPI::Token::Operator") and precedence_of($elem) >= $PRECEDENCE_OF_EQUALS and return $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::FALSE 0 0 0
Condition0%0/3
$elem->isa("PPI::Token::Operator") and precedence_of($elem) >= $PRECEDENCE_OF_EQUALS
!ll&&!rl&&r
000
MC/DC0%0/2
$elem->isa("PPI::Token::Operator") and precedence_of($elem) >= $PRECEDENCE_OF_EQUALS
$elem->isa("PPI::Token::Operator") precedence_of($elem) >= $PRECEDENCE_OF_EQUALS
Truth table0%0/3
A and B
A= $elem->isa("PPI::Token::Operator")B= precedence_of($elem) >= $PRECEDENCE_OF_EQUALS
ABresult
0X0
100
111
267 and precedence_of( $elem ) >= $PRECEDENCE_OF_EQUALS
268 and return $FALSE;
269 }
270  
271 0 $elem = $parent;
272 }
273  
274 0 return $FALSE;
275  
276 }
277  
278 #-----------------------------------------------------------------------------
279  
280 sub _is_effectively_a_comma {
281 0 ▶ my ($elem) = @_;
Subroutine _is_effectively_a_comma: not called CC=4 SCAR=30.0
282  
283 0 ▶ return if not $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless $elem 0 0 0
284  
285 return
286 0 ▶ $elem->isa('PPI::Token::Operator')
Condition0%0/6
$elem->isa('PPI::Token::Operator') && ($elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::COMMA || $elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::FATCOMMA)
!ll&&!rl&&r
000
$elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::COMMA or $elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::FATCOMMA
l!l&&r!l&&!r
000
MC/DC0%0/3
$elem->isa('PPI::Token::Operator') && ($elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::COMMA || $elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::FATCOMMA)
$elem->isa('PPI::Token::Operator') $elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::COMMA $elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::FATCOMMA
Truth table0%0/4
A && B or C
A= $elem->isa('PPI::Token::Operator')B= $elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::COMMAC= $elem->content eq $Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval::FATCOMMA
ABCresult
0XX0
1000
11X1
1011
287 && (
288 $elem->content() eq $COMMA
289 || $elem->content() eq $FATCOMMA
290 );
291 }
292  
293 #-----------------------------------------------------------------------------
294 # GitHub #324 (https://github.com/Perl-Critic/Perl-Critic/issues/324)
295 {
296 40 Readonly::Scalar my $RETURN => 'return';
297  
298 sub _is_returned {
299 0 ▶ my ( $elem ) = @_;
Subroutine _is_returned: not called CC=3 SCAR=24.8
300 0 my $prev = $elem->sprevious_sibling();
301 return
302 0 ▶ $prev
Condition0%0/6
$prev && $prev->isa('PPI::Token::Word') && $RETURN eq $prev->content
!ll&&!rl&&r
000
$prev and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
000
MC/DC0%0/3
$prev && $prev->isa('PPI::Token::Word') && $RETURN eq $prev->content
$prev $prev->isa("PPI::Token::Word") $RETURN eq $prev->content
Truth table0%0/4
A and B && C
A= $prevB= $prev->isa("PPI::Token::Word")C= $RETURN eq $prev->content
ABCresult
0XX0
10X0
1100
1111
303 &&
304 $prev->isa( 'PPI::Token::Word' )
305 &&
306 $RETURN eq $prev->content();
307 }
308 }
309  
310 #-----------------------------------------------------------------------------
311  
312 40 1;
313  
314 __END__
315  
316 #-----------------------------------------------------------------------------
317  
318 =pod
319  
320 =for stopwords destructors
321  
322 =head1 NAME
323  
324 Perl::Critic::Policy::ErrorHandling::RequireCheckingReturnValueOfEval - You can't depend upon the value of C<$@>/C<$EVAL_ERROR> to tell whether an C<eval> failed.
325  
326 =head1 AFFILIATION
327  
328 This Policy is part of the core L<Perl::Critic|Perl::Critic>
329 distribution.
330  
331  
332 =head1 DESCRIPTION
333  
334 A common idiom in perl for dealing with possible errors is to use
335 C<eval> followed by a check of C<$@>/C<$EVAL_ERROR>:
336  
337 eval {
338 ...
339 };
340 if ($EVAL_ERROR) {
341 ...
342 }
343  
344 There's a problem with this: the value of C<$EVAL_ERROR> can change
345 between the end of the C<eval> and the C<if> statement. The issue is
346 object destructors:
347  
348 package Foo;
349  
350 ...
351  
352 sub DESTROY {
353 ...
354 eval { ... };
355 ...
356 }
357  
358 package main;
359  
360 eval {
361 my $foo = Foo->new();
362 ...
363 };
364 if ($EVAL_ERROR) {
365 ...
366 }
367  
368 Assuming there are no other references to C<$foo> created, when the
369 C<eval> block in C<main> is exited, C<Foo::DESTROY()> will be invoked,
370 regardless of whether the C<eval> finished normally or not. If the
371 C<eval> in C<main> fails, but the C<eval> in C<Foo::DESTROY()>
372 succeeds, then C<$EVAL_ERROR> will be empty by the time that the C<if>
373 is executed. Additional issues arise if you depend upon the exact
374 contents of C<$EVAL_ERROR> and both C<eval>s fail, because the
375 messages from both will be concatenated.
376  
377 Even if there isn't an C<eval> directly in the C<DESTROY()> method
378 code, it may invoke code that does use C<eval> or otherwise affects
379 C<$EVAL_ERROR>.
380  
381 The solution is to ensure that, upon normal exit, an C<eval> returns a
382 true value and to test that value:
383  
384 # Constructors are no problem.
385 my $object = eval { Class->new() };
386  
387 # To cover the possibility that an operation may correctly return a
388 # false value, end the block with "1":
389 if ( eval { something(); 1 } ) {
390 ...
391 }
392  
393 eval {
394 ...
395 1;
396 }
397 or do {
398 # Error handling here
399 };
400  
401 Unfortunately, you can't use the C<defined> function to test the
402 result; C<eval> returns an empty string on failure.
403  
404 Various modules have been written to take some of the pain out of
405 properly localizing and checking C<$@>/C<$EVAL_ERROR>. For example:
406  
407 use Try::Tiny;
408 try {
409 ...
410 } catch {
411 # Error handling here;
412 # The exception is in $_/$ARG, not $@/$EVAL_ERROR.
413 }; # Note semicolon.
414  
415 "But we don't use DESTROY() anywhere in our code!" you say. That may
416 be the case, but do any of the third-party modules you use have them?
417 What about any you may use in the future or updated versions of the
418 ones you already use?
419  
420  
421 =head1 CONFIGURATION
422  
423 This Policy is not configurable except for the standard options.
424  
425  
426 =head1 SEE ALSO
427  
428 For a nice, easy, non-magical way of properly handling exceptions, see
429 L<Try::Tiny|Try::Tiny>.
430  
431  
432 =head1 AUTHOR
433  
434 Elliot Shank C<< <perl@galumph.com> >>
435  
436 =head1 COPYRIGHT
437  
438 Copyright (c) 2008-2023 Elliot Shank.
439  
440 This program is free software; you can redistribute it and/or modify
441 it under the same terms as Perl itself. The full text of this license
442 can be found in the LICENSE file included with this module.
443  
444 =cut
445  
446 # Local Variables:
447 # mode: cperl
448 # cperl-indent-level: 4
449 # fill-column: 78
450 # indent-tabs-mode: nil
451 # c-indentation-style: bsd
452 # End:
453 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
« blib/lib/Perl/Critic/Policy/Subroutines/ProhibitUnusedPrivateSubroutines.pm ↑ Summary blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitReverseSortBlock.pm »