| 1 | package Perl::Critic::Policy::Subroutines::ProhibitReturnSort; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||
| 12 | |||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{"return" statement followed by "sort"}; | |||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => q{Behavior is undefined if called in scalar context}; | |||||||||
| 17 | |||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||
| 19 | |||||||||||
| 20 | 90 | ▶ | sub supported_parameters { return () } | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 21 | 75 | ▶ | sub default_severity { return $SEVERITY_HIGHEST } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 22 | 74 | ▶ | sub default_themes { return qw(core bugs certrule ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | 37 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | |||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||
| 26 | |||||||||||
| 27 | sub violates { | ||||||||||
| 28 | 361 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||
| 29 | 361 | ▶ | return if $elem->content() ne 'return'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 5 | ▶ | return if is_hash_key($elem); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 5 | my $sib = $elem->snext_sibling(); | |||||||||
| 33 | 5 | ▶ | return if !$sib; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 5 | ▶ | return if !$sib->isa('PPI::Token::Word'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 1 | ▶ | return if $sib->content() ne 'sort'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | |||||||||||
| 37 | # Must be 'return sort' | ||||||||||
| 38 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 40 | 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 : | ||||||||||