line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Subroutines::ProhibitReturnSort; |
2
|
|
|
|
|
|
|
|
3
|
40
|
|
|
40
|
|
26674
|
use 5.010001; |
|
40
|
|
|
|
|
163
|
|
4
|
40
|
|
|
40
|
|
264
|
use strict; |
|
40
|
|
|
|
|
98
|
|
|
40
|
|
|
|
|
860
|
|
5
|
40
|
|
|
40
|
|
196
|
use warnings; |
|
40
|
|
|
|
|
106
|
|
|
40
|
|
|
|
|
1012
|
|
6
|
40
|
|
|
40
|
|
231
|
use Readonly; |
|
40
|
|
|
|
|
86
|
|
|
40
|
|
|
|
|
2081
|
|
7
|
|
|
|
|
|
|
|
8
|
40
|
|
|
40
|
|
281
|
use Perl::Critic::Utils qw{ :severities :classification }; |
|
40
|
|
|
|
|
109
|
|
|
40
|
|
|
|
|
2191
|
|
9
|
40
|
|
|
40
|
|
14001
|
use parent 'Perl::Critic::Policy'; |
|
40
|
|
|
|
|
93
|
|
|
40
|
|
|
|
|
250
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '1.148'; |
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
|
93
|
|
|
93
|
0
|
1667
|
sub supported_parameters { return () } |
21
|
81
|
|
|
81
|
1
|
386
|
sub default_severity { return $SEVERITY_HIGHEST } |
22
|
74
|
|
|
74
|
1
|
322
|
sub default_themes { return qw(core bugs certrule ) } |
23
|
40
|
|
|
40
|
1
|
121
|
sub applies_to { return 'PPI::Token::Word' } |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub violates { |
28
|
403
|
|
|
403
|
1
|
785
|
my ( $self, $elem, undef ) = @_; |
29
|
403
|
100
|
|
|
|
838
|
return if $elem->content() ne 'return'; |
30
|
16
|
50
|
|
|
|
114
|
return if is_hash_key($elem); |
31
|
|
|
|
|
|
|
|
32
|
16
|
|
|
|
|
55
|
my $sib = $elem->snext_sibling(); |
33
|
16
|
100
|
|
|
|
328
|
return if !$sib; |
34
|
15
|
100
|
|
|
|
92
|
return if !$sib->isa('PPI::Token::Word'); |
35
|
10
|
100
|
|
|
|
29
|
return if $sib->content() ne 'sort'; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Must be 'return sort' |
38
|
7
|
|
|
|
|
102
|
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 : |