| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::ControlStructures::ProhibitReturnInMappingBlock; |
|
2
|
2
|
|
|
2
|
|
609197
|
use 5.008001; |
|
|
2
|
|
|
|
|
10
|
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
106
|
|
|
4
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
129
|
|
|
5
|
2
|
|
|
2
|
|
11
|
use parent 'Perl::Critic::Policy'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
33
|
|
|
6
|
2
|
|
|
2
|
|
209324
|
use List::Util qw(any); |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
194
|
|
|
7
|
2
|
|
|
2
|
|
11
|
use Perl::Critic::Utils qw(:severities); |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
133
|
|
|
8
|
2
|
|
|
2
|
|
300
|
use constant EXPL => 'A "return" in a mapping block causes confusing behavior.'; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
790
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my @MAPPING_BLOCK_KEYWORDS = qw(map grep); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
|
13
|
|
|
|
|
|
|
|
|
14
|
9
|
|
|
9
|
0
|
3721078
|
sub supported_parameters { return (); } |
|
15
|
6
|
|
|
6
|
1
|
131
|
sub default_severity { return $SEVERITY_HIGHEST; } |
|
16
|
0
|
|
|
0
|
1
|
0
|
sub default_themes { return qw(bugs complexity); } |
|
17
|
9
|
|
|
9
|
1
|
4417982
|
sub applies_to { return 'PPI::Structure::Block'; } |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub violates { |
|
20
|
20
|
|
|
20
|
1
|
325
|
my ($self, $elem, undef) = @_; |
|
21
|
|
|
|
|
|
|
|
|
22
|
20
|
|
|
|
|
65
|
my $keyword = _mapping_block_keyword($elem); |
|
23
|
20
|
100
|
|
|
|
86
|
return if !$keyword; |
|
24
|
|
|
|
|
|
|
|
|
25
|
5
|
|
|
|
|
19
|
my $desc = sprintf('"return" statement in "%s" block.', $keyword); |
|
26
|
5
|
|
|
|
|
26
|
my @stmts = $elem->schildren; |
|
27
|
5
|
50
|
|
|
|
108
|
return if !@stmts; |
|
28
|
|
|
|
|
|
|
|
|
29
|
5
|
|
|
|
|
12
|
my @violations; |
|
30
|
|
|
|
|
|
|
|
|
31
|
5
|
|
|
|
|
18
|
for my $stmt (@stmts) { |
|
32
|
11
|
100
|
|
|
|
1529
|
push @violations, $self->violation($desc, EXPL, $stmt) if _is_return($stmt); |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
5
|
|
|
|
|
52
|
return @violations; |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub _mapping_block_keyword { |
|
39
|
20
|
|
|
20
|
|
46
|
my ($elem) = @_; |
|
40
|
|
|
|
|
|
|
|
|
41
|
20
|
50
|
|
|
|
65
|
return if !$elem->sprevious_sibling; |
|
42
|
20
|
|
|
|
|
661
|
my $keyword = $elem->sprevious_sibling->content; |
|
43
|
20
|
100
|
|
36
|
|
653
|
return $keyword if any { $keyword eq $_ } @MAPPING_BLOCK_KEYWORDS; |
|
|
36
|
|
|
|
|
110
|
|
|
44
|
15
|
|
|
|
|
60
|
return; |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _is_return { |
|
48
|
11
|
|
|
11
|
|
30
|
my ($stmt) = @_; |
|
49
|
|
|
|
|
|
|
|
|
50
|
11
|
|
|
26
|
|
65
|
return any { $_->content eq 'return' } $stmt->schildren; |
|
|
26
|
|
|
|
|
283
|
|
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
1; |
|
54
|
|
|
|
|
|
|
__END__ |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=encoding utf-8 |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Perl::Critic::Policy::ControlStructures::ProhibitReturnInMappingBlock - Do not "return" in mapping blocks (map, grep) |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 AFFILIATION |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
This policy is part of the L<Perl::Critic::Policy::ControlStructures::ProhibitReturnInMappingBlock> distribution. |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Using C<return> in a mapping block (C<map> or C<grep>) causes unexpected behavior. |
|
69
|
|
|
|
|
|
|
A C<return> exits the entire enclosing subroutine, not just the block. |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub func { |
|
72
|
|
|
|
|
|
|
my @list = (1, 2, 3); |
|
73
|
|
|
|
|
|
|
my @result = map { |
|
74
|
|
|
|
|
|
|
return 0 unless $_; # not ok |
|
75
|
|
|
|
|
|
|
$_ + 5; |
|
76
|
|
|
|
|
|
|
} @list; |
|
77
|
|
|
|
|
|
|
return @result; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
If you want to skip an element, use C<next> instead: |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub func { |
|
83
|
|
|
|
|
|
|
my @list = (1, 2, 3); |
|
84
|
|
|
|
|
|
|
my @result = map { |
|
85
|
|
|
|
|
|
|
next unless $_; |
|
86
|
|
|
|
|
|
|
$_ + 5; |
|
87
|
|
|
|
|
|
|
} @list; |
|
88
|
|
|
|
|
|
|
return @result; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This applies equally to C<grep> blocks. |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 CONFIGURATION |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
This Policy is not configurable except for the standard options. |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
L<Perl::Critic::Policy::ControlStructures::ProhibitReturnInDoBlock> by utgwkk, which inspired this policy. |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 LICENSE |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Copyright (C) 2026 hogashi. Portions copyright (C) 2020 utgwkk. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
106
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 AUTHOR |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
hogashi |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
|
113
|
|
|
|
|
|
|
|