File Coverage

blib/lib/Perl/Lint/Policy/Subroutines/ProhibitReturnSort.pm
Criterion Covered Total %
statement 45 47 95.7
branch 24 26 92.3
condition 13 18 72.2
subroutine 6 6 100.0
pod 0 1 0.0
total 88 98 89.8


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::Subroutines::ProhibitReturnSort;
2 133     133   68159 use strict;
  133         233  
  133         3356  
3 133     133   443 use warnings;
  133         173  
  133         2573  
4 133     133   854 use Perl::Lint::Constants::Type;
  133         154  
  133         59802  
5 133     133   603 use parent "Perl::Lint::Policy";
  133         175  
  133         572  
6              
7             use constant {
8 133         42846 DESC => '"return" statement followed by "sort"',
9             EXPL => 'Behavior is undefined if called in scalar context',
10 133     133   6686 };
  133         201  
11              
12             sub evaluate {
13 5     5 0 6 my ($class, $file, $tokens, $args) = @_;
14              
15 5         7 my @violations;
16 5         17 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 96         71 my $token_type = $token->{type};
18              
19 96 100       189 if ($token_type == RETURN) {
    100          
20 17         16 my $next_token = $tokens->[$i+1];
21 17 100 100     54 if ($next_token->{type} == BUILTIN_FUNC && $next_token->{data} eq 'sort') {
22 13         12 my $is_in_postposition_if = 0;
23 13         9 my $is_wantarray = 0;
24 13         21 for ($i++; my $token = $tokens->[$i]; $i++) {
25 81         49 my $token_type = $token->{type};
26 81 100 100     237 if ($token_type == SEMI_COLON) {
    100 66        
    100          
27 13 100       17 if (!$is_wantarray) {
28             push @violations, {
29             filename => $file,
30             line => $token->{line},
31 12         30 description => DESC,
32             explanation => EXPL,
33             policy => __PACKAGE__,
34             };
35             }
36 13         14 last;
37             }
38             elsif ($token_type == IF_STATEMENT) {
39 5         9 $is_in_postposition_if = 1;
40             }
41             elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'wantarray' && $is_in_postposition_if) {
42 1         2 $is_wantarray = 1;
43             }
44             }
45 13         20 next;
46             }
47             }
48             elsif ($token_type == IF_STATEMENT) {
49 1         3 my $left_brace_num = 0;
50 1         1 my $is_wantarray = 0;
51 1         4 for ($i++; my $token = $tokens->[$i]; $i++) {
52 9         9 my $token_type = $token->{type};
53 9 100 100     35 if ($token_type == LEFT_BRACE) {
    100          
    100          
    100          
54 1         3 $left_brace_num++;
55             }
56             elsif ($token_type == RIGHT_BRACE) {
57 1         3 $left_brace_num--;
58 1 50       4 last if ($left_brace_num <= 0);
59             }
60             elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'wantarray') {
61 1         3 $is_wantarray = 1;
62             }
63             elsif (!$is_wantarray) {
64 1         3 my $next_token = $tokens->[$i+1];
65 1 50 33     10 if ($next_token && $next_token->{type} == BUILTIN_FUNC && $next_token->{data} eq 'sort') {
      33        
66             push @violations, {
67             filename => $file,
68             line => $token->{line},
69 0         0 description => DESC,
70             explanation => EXPL,
71             policy => __PACKAGE__,
72             };
73 0         0 next;
74             }
75             }
76             }
77             }
78             }
79              
80 5         17 return \@violations;
81             }
82              
83             1;
84