The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Perl::Critic::Policy::Freenode::EmptyReturn - Don't use return with no arguments

DESCRIPTION

Context-sensitive functions, while one way to write functions that DWIM (Do What I Mean), tend to instead lead to unexpected behavior when the function is accidentally used in a different context, especially if the function's behavior changes significantly based on context. This also can lead to vulnerabilities when a function is intended to be used as a scalar, but is used in a list, such as a hash constructor or function parameter list. return with no arguments will return either undef or an empty list depending on context. Instead, return the appropriate value explicitly.

  return;       # not ok
  return ();    # ok
  return undef; # ok

  sub get_stuff {
    return unless @things;
    return join(' ', @things);
  }
  my %stuff = (
    one => 1,
    two => 2,
    three => get_stuff(), # oops! function returns empty list if @things is empty
  );

AFFILIATION

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

CONFIGURATION

This policy is not configurable except for the standard options.

CAVEATS

This policy currently does not detect an empty return which has a conditional modifier, such as return if $foo;.

AUTHOR

Dan Book, dbook@cpan.org

COPYRIGHT AND LICENSE

Copyright 2015, Dan Book.

This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

Perl::Critic