File Coverage

blib/lib/Perl/Critic/Policy/Subroutines/ProhibitReturnSort.pm
Criterion Covered Total %
statement 28 29 96.5
branch 7 10 70.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 50 55 90.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Subroutines::ProhibitReturnSort;
2              
3 40     40   26005 use 5.010001;
  40         194  
4 40     40   222 use strict;
  40         83  
  40         827  
5 40     40   222 use warnings;
  40         88  
  40         975  
6 40     40   247 use Readonly;
  40         99  
  40         2339  
7              
8 40     40   282 use Perl::Critic::Utils qw{ :severities :classification };
  40         150  
  40         2019  
9 40     40   13727 use parent 'Perl::Critic::Policy';
  40         137  
  40         249  
10              
11             our $VERSION = '1.150';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{"return" statement followed by "sort"};
16             Readonly::Scalar my $EXPL => q{Behavior is undefined if called in scalar context};
17              
18             #-----------------------------------------------------------------------------
19              
20 89     89 0 1683 sub supported_parameters { return () }
21 74     74 1 305 sub default_severity { return $SEVERITY_HIGHEST }
22 74     74 1 349 sub default_themes { return qw(core bugs certrule ) }
23 36     36 1 134 sub applies_to { return 'PPI::Token::Word' }
24              
25             #-----------------------------------------------------------------------------
26              
27             sub violates {
28 358     358 1 560 my ( $self, $elem, undef ) = @_;
29 358 100       569 return if $elem->content() ne 'return';
30 5 50       49 return if is_hash_key($elem);
31              
32 5         16 my $sib = $elem->snext_sibling();
33 5 50       111 return if !$sib;
34 5 100       29 return if !$sib->isa('PPI::Token::Word');
35 1 50       5 return if $sib->content() ne 'sort';
36              
37             # Must be 'return sort'
38 0           return $self->violation( $DESC, $EXPL, $elem );
39             }
40              
41             1;
42              
43             __END__
44              
45             #-----------------------------------------------------------------------------
46              
47             =pod
48              
49             =for stopwords Ulrich Wisser
50              
51             =head1 NAME
52              
53             Perl::Critic::Policy::Subroutines::ProhibitReturnSort - Behavior of C<sort> is not defined if called in scalar context.
54              
55             =head1 AFFILIATION
56              
57             This Policy is part of the core L<Perl::Critic|Perl::Critic>
58             distribution.
59              
60             =head1 DESCRIPTION
61              
62             The behavior of the builtin C<sort> function is not defined if called
63             in scalar context. So if you write a subroutine that directly
64             C<return>s the result of a C<sort> operation, then your code will
65             behave unpredictably if someone calls your subroutine in a scalar
66             context. This Policy emits a violation if the C<return> keyword
67             is directly followed by the C<sort> function. To safely return a
68             sorted list of values from a subroutine, you should assign the
69             sorted values to a temporary variable first. For example:
70              
71             sub frobulate {
72              
73             return sort @list; # not ok!
74              
75             my @sorted_list = sort @list;
76             return @sorted_list # OK
77             }
78              
79             =head1 KNOWN BUGS
80              
81             This Policy is not sensitive to the C<wantarray> function. So the
82             following code would generate a false violation:
83              
84             sub frobulate {
85              
86             if (wantarray) {
87             return sort @list;
88             }
89             else{
90             return join @list;
91             }
92             }
93              
94             =head1 CONFIGURATION
95              
96             This Policy is not configurable except for the standard options.
97              
98             =head1 CREDITS
99              
100             This Policy was suggested by Ulrich Wisser and the L<http://iis.se> team.
101              
102             =head1 AUTHOR
103              
104             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
105              
106             =head1 COPYRIGHT
107              
108             Copyright (c) 2005-2017 Imaginative Software Systems. All rights reserved.
109              
110             This program is free software; you can redistribute it and/or modify
111             it under the same terms as Perl itself. The full text of this license
112             can be found in the LICENSE file included with this module.
113              
114             =cut
115              
116             ##############################################################################
117             # Local Variables:
118             # mode: cperl
119             # cperl-indent-level: 4
120             # fill-column: 78
121             # indent-tabs-mode: nil
122             # c-indentation-style: bsd
123             # End:
124             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :