blib/lib/Perl/Lint/Policy/Subroutines/ProhibitReturnSort.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 SCAR 0.1
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Perl/Lint/Policy/Subroutines/ProhibitNestedSubs.pm ↑ Summary blib/lib/Perl/Lint/Policy/Subroutines/ProhibitSubroutinePrototypes.pm »
1 package Perl::Lint::Policy::Subroutines::ProhibitReturnSort;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Perl::Lint::Constants::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use parent "Perl::Lint::Policy";
6  
7 use constant {
8 DESC => '"return" statement followed by "sort"',
9 EXPL => 'Behavior is undefined if called in scalar context',
10 };
11  
12 sub evaluate {
13 my ($class, $file, $tokens, $args) = @_;
14  
15 my @violations;
16 for (my $i = 0; my $token = $tokens->[$i]; $i++) {
17 my $token_type = $token->{type};
18  
19 if ($token_type == RETURN) {
20 my $next_token = $tokens->[$i+1];
21 if ($next_token->{type} == BUILTIN_FUNC && $next_token->{data} eq 'sort') {
22 my $is_in_postposition_if = 0;
23 my $is_wantarray = 0;
24 for ($i++; my $token = $tokens->[$i]; $i++) {
25 my $token_type = $token->{type};
26 if ($token_type == SEMI_COLON) {
27 if (!$is_wantarray) {
28 push @violations, {
29 filename => $file,
30 line => $token->{line},
31 description => DESC,
32 explanation => EXPL,
33 policy => __PACKAGE__,
34 };
35 }
36 last;
37 }
38 elsif ($token_type == IF_STATEMENT) {
39 $is_in_postposition_if = 1;
40 }
41 elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'wantarray' && $is_in_postposition_if) {
42 $is_wantarray = 1;
43 }
44 }
45 next;
46 }
47 }
48 elsif ($token_type == IF_STATEMENT) {
49 my $left_brace_num = 0;
50 my $is_wantarray = 0;
51 for ($i++; my $token = $tokens->[$i]; $i++) {
52 my $token_type = $token->{type};
53 if ($token_type == LEFT_BRACE) {
54 $left_brace_num++;
55 }
56 elsif ($token_type == RIGHT_BRACE) {
57 $left_brace_num--;
58 last if ($left_brace_num <= 0);
59 }
60 elsif ($token_type == BUILTIN_FUNC && $token->{data} eq 'wantarray') {
61 $is_wantarray = 1;
62 }
63 elsif (!$is_wantarray) {
64 my $next_token = $tokens->[$i+1];
65 if ($next_token && $next_token->{type} == BUILTIN_FUNC && $next_token->{data} eq 'sort') {
66 push @violations, {
67 filename => $file,
68 line => $token->{line},
69 description => DESC,
70 explanation => EXPL,
71 policy => __PACKAGE__,
72 };
73 next;
74 }
75 }
76 }
77 }
78 }
79  
80 return \@violations;
81 }
82  
83 1;
84  
« blib/lib/Perl/Lint/Policy/Subroutines/ProhibitNestedSubs.pm ↑ Summary blib/lib/Perl/Lint/Policy/Subroutines/ProhibitSubroutinePrototypes.pm »