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