| 1 | package Perl::Critic::Policy::Community::ConditionalImplicitReturn; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 => 'Subroutine may implicitly return a conditional statement'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use constant EXPL => 'When the last statement in a subroutine is a conditional, the return value may unexpectedly be the evaluated condition.'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 5 | ▶ | sub supported_parameters { () } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub default_severity { $SEVERITY_MEDIUM } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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 | 5 | ▶ | sub applies_to { 'PPI::Statement::Sub' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | my %conditionals = map { ($_ => 1) } qw(if unless); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 5 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 147 | ▶ | my $returns = $block->find(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 147 | my ($elem, $child) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # Don't search in blocks unless we know they are structural | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 147 | ▶ | if ($child->isa('PPI::Structure::Block')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | ▶ | return undef unless is_structural_block($child); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 147 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 139 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # Check the last statement if any non-empty return is present | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | 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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | my $last = $block->schild(-1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Check if last statement is a conditional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | if ($last and $last->isa('PPI::Statement::Compound') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
$last and $last->isa("PPI::Statement::Compound") and $last->schildren and exists $conditionals{$last->schild(0)}
$last and $last->isa("PPI::Statement::Compound") and $last->schildren
$last and $last->isa("PPI::Statement::Compound")
MC/DC25%1/4
$last and $last->isa("PPI::Statement::Compound") and $last->schildren and exists $conditionals{$last->schild(0)}
$last
$last->isa("PPI::Statement::Compound")
$last->schildren
exists $conditionals{$last->schild(0)}
Truth table40%2/5
A and B and C and D
A= $lastB= $last->isa("PPI::Statement::Compound")C= $last->schildrenD= exists $conditionals{$last->schild(0)}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | and $last->schildren and exists $conditionals{$last->schild(0)}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Make sure there isn't an "else" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | ▶ | unless (any { $_->isa('PPI::Token::Word') and $_ eq 'else' } $last->schildren) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | return $self->violation(DESC, EXPL, $last); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | return (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | Perl::Critic::Policy::Community::ConditionalImplicitReturn - Don't end a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | subroutine with a conditional block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | If the last statement in a subroutine is a conditional block such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | C<if ($foo) { ... }>, and the C<else> condition is not handled, the subroutine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | will return an unexpected value when the condition fails, and it is most likely | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | a logic error. Specify a return value after the conditional, or handle the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | C<else> condition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | sub { ... if ($foo) { return 1 } } # not ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | sub { ... if ($foo) { return 1 } return 0 } # ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | sub { ... if ($foo) { return 1 } else { return 0 } } # ok | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | This policy only applies if the subroutine contains a return statement with an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | explicit return value, indicating it is not intended to be used in void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | context. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 CAVEATS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | This policy currently only checks for implicitly returned conditionals in named | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | subroutines, anonymous subroutines are not checked. Also, return statements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | within blocks, other than compound statements like C<if> and C<foreach>, are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | not considered when determining if a function is intended to be used in void | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 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> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||