File Coverage

blib/lib/Perl/Critic/Policy/Community/EmptyReturn.pm
Criterion Covered Total %
statement 41 42 97.6
branch 8 8 100.0
condition 6 8 75.0
subroutine 14 15 93.3
pod 4 5 80.0
total 73 78 93.5


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   351 use warnings;
  1         2  
  1         22  
4 1     1   3  
  1         2  
  1         22  
5             use Perl::Critic::Utils qw(:severities :classification :ppi);
6 1     1   4 use parent 'Perl::Critic::Policy';
  1         2  
  1         41  
7 1     1   303  
  1         2  
  1         5  
8             use List::Util 'any';
9 1     1   44 use Perl::Critic::Community::Utils qw(is_empty_return is_structural_block);
  1         2  
  1         57  
10 1     1   349  
  1         2  
  1         60  
11             our $VERSION = 'v1.0.3';
12              
13             use constant DESC => 'return called with no arguments';
14 1     1   13 use constant EXPL => 'return with no arguments may return either undef or an empty list depending on context. This can be surprising for the same reason as other context-sensitive returns. Return undef or the empty list explicitly.';
  1         2  
  1         43  
15 1     1   5  
  1         3  
  1         266  
16              
17 11     11 0 48026 my ($self, $elem) = @_;
18 5     5 1 48
19 0     0 1 0 my $block = $elem->block || return ();
20 11     11 1 78907 my $returns = $block->find(sub {
21             my ($elem, $child) = @_;
22             # Don't search in blocks unless we know they are structural
23 11     11 1 197 if ($child->isa('PPI::Structure::Block')) {
24             return undef unless is_structural_block($child);
25 11   50     32 }
26             return 1 if $child->isa('PPI::Token::Word') and $child eq 'return';
27 211     211   2074 return 0;
28             });
29 211 100       481
30 3 100       10 # Return a violation for each empty return, if any non-empty return is present
31             if ($returns and any { !is_empty_return($_) } @$returns) {
32 210 100 100     548 return map { $self->violation(DESC, EXPL, $_) } grep { is_empty_return($_) } @$returns;
33 189         487 }
34 11         243
35             return ();
36             }
37 11 100 66 17   170  
  17         78  
38 8         24 1;
  5         49  
  16         53  
39              
40             =head1 NAME
41 3         44  
42             Perl::Critic::Policy::Community::EmptyReturn - Don't use return with no
43             arguments
44              
45             =head1 DESCRIPTION
46              
47             Context-sensitive functions, while one way to write functions that DWIM (Do
48             What I Mean), tend to instead lead to unexpected behavior when the function is
49             accidentally used in a different context, especially if the function's behavior
50             changes significantly based on context. This also can lead to vulnerabilities
51             when a function is intended to be used as a scalar, but is used in a list, such
52             as a hash constructor or function parameter list. C<return> with no arguments
53             will return either C<undef> or an empty list depending on context. Instead,
54             return the appropriate value explicitly.
55              
56             return; # not ok
57             return (); # ok
58             return undef; # ok
59              
60             sub get_stuff {
61             return unless @things;
62             return join(' ', @things);
63             }
64             my %stuff = (
65             one => 1,
66             two => get_stuff(), # oops! function returns empty list if @things is empty
67             three => 3,
68             );
69              
70             Empty returns are permitted by this policy if the subroutine contains no
71             explicit return values, indicating it is intended to be used in void context.
72              
73             =head1 CAVEATS
74              
75             This policy currently only checks return statements in named subroutines,
76             anonymous subroutines are not checked. Also, return statements within blocks,
77             other than compound statements like C<if> and C<foreach>, are not considered
78             when determining if a function is intended to be used in void context.
79              
80             Any non-empty return will cause empty returns within the same subroutine to
81             report violations, even though in list context, C<return> and C<return ()> are
82             functionally equivalent. It is recommended to explicitly specify an empty list
83             return with C<return ()> in a function that intends to return list context.
84              
85             =head1 AFFILIATION
86              
87             This policy is part of L<Perl::Critic::Community>.
88              
89             =head1 CONFIGURATION
90              
91             This policy is not configurable except for the standard options.
92              
93             =head1 AUTHOR
94              
95             Dan Book, C<dbook@cpan.org>
96              
97             =head1 COPYRIGHT AND LICENSE
98              
99             Copyright 2015, Dan Book.
100              
101             This library is free software; you may redistribute it and/or modify it under
102             the terms of the Artistic License version 2.0.
103              
104             =head1 SEE ALSO
105              
106             L<Perl::Critic>