Perl::Critic::Policy::Freenode::EmptyReturn - Don't use return with no arguments
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
undef
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 );
Empty returns are permitted by this policy if the subroutine contains no explicit return values, indicating it is intended to be used in void context.
This policy currently only checks return statements 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.
if
foreach
Any non-empty return will cause empty returns within the same subroutine to report violations, even though in list context, return and return () are functionally equivalent. It is recommended to explicitly specify an empty list return with return () in a function that intends to return list context.
return ()
This policy is part of Perl::Critic::Freenode.
This policy is not configurable except for the standard options.
Dan Book, dbook@cpan.org
dbook@cpan.org
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.
Perl::Critic
To install Perl::Critic::Freenode, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl::Critic::Freenode
CPAN shell
perl -MCPAN -e shell install Perl::Critic::Freenode
For more information on module installation, please visit the detailed CPAN module installation guide.