Perl::Critic::Policy::Community::ConditionalImplicitReturn(3pm)

Perl::Critic::Policy::Community::ConditionalImplicitReturn3pm

Don't end a subroutine with a conditional block

Overview

If the last statement in a subroutine is a conditional block such as "if ($foo) { ... }", and the "else" condition is not handled, the subroutine will return an unexpected value when the condition fails, and it is most likely a logic error. Specify a return value after the conditional, or handle the "else" condition.

  sub { ... if ($foo) { return 1 } }                   # not ok
  sub { ... if ($foo) { return 1 } return 0 }          # ok
  sub { ... if ($foo) { return 1 } else { return 0 } } # ok

This policy only applies if the subroutine contains a return statement with an explicit return value, indicating it is not intended to be used in void context.

Caveats

This policy currently only checks for implicitly returned conditionals in named subroutines, anonymous subroutines are not checked. Also, return statements within blocks, other than compound statements like "if" and "foreach", are not considered when determining if a function is intended to be used in void context.

Affiliation

This policy is part of Perl::Critic::Community.

Configuration

This policy is not configurable except for the standard options.

Releases

2 releases since Debian 12 (Bookworm); the text changed 1 time.

  1. 12
  2. 13

Author

Dan Book, "[email protected]"

From libperl-critic-community-perl 1.0.4-1 in Debian 13 (Trixie). Parsed by carta from the page's man source.