line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Perl::Critic::Policy::Community::Wantarray; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
457
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
6
|
use Perl::Critic::Utils qw(:severities :classification :ppi); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
7
|
1
|
|
|
1
|
|
351
|
use parent 'Perl::Critic::Policy'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = 'v1.0.2'; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
84
|
use constant DESC => 'wantarray() called'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
68
|
|
12
|
1
|
|
|
1
|
|
8
|
use constant EXPL => 'Context-sensitive functions lead to unexpected errors or vulnerabilities. Functions should explicitly return either a list or a scalar value.'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
203
|
|
13
|
|
|
|
|
|
|
|
14
|
4
|
|
|
4
|
0
|
11229
|
sub supported_parameters { () } |
15
|
3
|
|
|
3
|
1
|
30
|
sub default_severity { $SEVERITY_LOW } |
16
|
0
|
|
|
0
|
1
|
0
|
sub default_themes { 'community' } |
17
|
4
|
|
|
4
|
1
|
21041
|
sub applies_to { 'PPI::Token::Word' } |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub violates { |
20
|
11
|
|
|
11
|
1
|
783
|
my ($self, $elem) = @_; |
21
|
11
|
100
|
100
|
|
|
27
|
return () unless (($elem eq 'wantarray' or $elem->literal eq 'CORE::wantarray') and is_function_call $elem); |
|
|
|
66
|
|
|
|
|
22
|
3
|
|
|
|
|
1034
|
return $self->violation(DESC, EXPL, $elem); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
1; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 NAME |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Perl::Critic::Policy::Community::Wantarray - Don't write context-sensitive |
30
|
|
|
|
|
|
|
functions using wantarray |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 DESCRIPTION |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
Context-sensitive functions, while one way to write functions that DWIM (Do |
35
|
|
|
|
|
|
|
What I Mean), tend to instead lead to unexpected behavior when the function is |
36
|
|
|
|
|
|
|
accidentally used in a different context, especially if the function's behavior |
37
|
|
|
|
|
|
|
changes significantly based on context. This also can lead to vulnerabilities |
38
|
|
|
|
|
|
|
when a function is intended to be used as a scalar, but is used in a list, such |
39
|
|
|
|
|
|
|
as a hash constructor or function parameter list. Instead, functions should be |
40
|
|
|
|
|
|
|
explicitly documented to return either a scalar value or a list, so there is no |
41
|
|
|
|
|
|
|
potential for confusion or vulnerability. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
return wantarray ? ('a','b','c') : 3; # not ok |
44
|
|
|
|
|
|
|
return CORE::wantarray ? ('a', 'b', 'c') : 3; # not ok |
45
|
|
|
|
|
|
|
return ('a','b','c'); # ok |
46
|
|
|
|
|
|
|
return 3; # ok |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub get_stuff { |
49
|
|
|
|
|
|
|
return wantarray ? @things : \@things; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
my $stuff = Stuff->new(stuff => get_stuff()); # oops! function will return a list! |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 AFFILIATION |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
This policy is part of L<Perl::Critic::Community>. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 CONFIGURATION |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
This policy is not configurable except for the standard options. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 AUTHOR |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Dan Book, C<dbook@cpan.org> |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Copyright 2015, Dan Book. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
This library is free software; you may redistribute it and/or modify it under |
70
|
|
|
|
|
|
|
the terms of the Artistic License version 2.0. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 SEE ALSO |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
L<Perl::Critic> |