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   93132 use strict;
  133         276  
  133         4803  
3 133     133   641 use warnings;
  133         228  
  133         3399  
4 133     133   1148 use Perl::Lint::Constants::Type;
  133         220  
  133         83380  
5 133     133   1015 use parent "Perl::Lint::Policy";
  133         241  
  133         839  
6              
7             use constant {
8 133         56157 DESC => '"return" statement followed by "sort"',
9             EXPL => 'Behavior is undefined if called in scalar context',
10 133     133   9120 };
  133         258  
11              
12             sub evaluate {
13 5     5 0 8 my ($class, $file, $tokens, $args) = @_;
14              
15 5         5 my @violations;
16 5         17 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 96         60 my $token_type = $token->{type};
18              
19 96 100       225 if ($token_type == RETURN) {
    100          
20 17         16 my $next_token = $tokens->[$i+1];
21 17 100 100     60 if ($next_token->{type} == BUILTIN_FUNC && $next_token->{data} eq 'sort') {
22 13         10 my $is_in_postposition_if = 0;
23 13         9 my $is_wantarray = 0;
24 13         20 for ($i++; my $token = $tokens->[$i]; $i++) {
25 81         70 my $token_type = $token->{type};
26 81 100 100     253 if ($token_type == SEMI_COLON) {
    100 66        
    100          
27 13 100       17 if (!$is_wantarray) {
28 12         40 push @violations, {
29             filename => $file,
30             line => $token->{line},
31             description => DESC,
32             explanation => EXPL,
33             policy => __PACKAGE__,
34             };
35             }
36 13         13 last;
37             }
38             elsif ($token_type == IF_STATEMENT) {
39 5         7 $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         22 next;
46             }
47             }
48             elsif ($token_type == IF_STATEMENT) {
49 1         2 my $left_brace_num = 0;
50 1         2 my $is_wantarray = 0;
51 1         3 for ($i++; my $token = $tokens->[$i]; $i++) {
52 9         8 my $token_type = $token->{type};
53 9 100 100     37 if ($token_type == LEFT_BRACE) {
    100          
    100          
    100          
54 1         2 $left_brace_num++;
55             }
56             elsif ($token_type == RIGHT_BRACE) {
57 1         1 $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         2 $is_wantarray = 1;
62             }
63             elsif (!$is_wantarray) {
64 1         2 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 0         0 push @violations, {
67             filename => $file,
68             line => $token->{line},
69             description => DESC,
70             explanation => EXPL,
71             policy => __PACKAGE__,
72             };
73 0         0 next;
74             }
75             }
76             }
77             }
78             }
79              
80 5         20 return \@violations;
81             }
82              
83             1;
84