| 1 | package Perl::Critic::Policy::Community::Wantarray; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use constant DESC => 'wantarray() called'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use constant EXPL => 'Context-sensitive functions lead to unexpected errors or vulnerabilities. Functions should explicitly return either a list or a scalar value.'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | sub supported_parameters { () } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | sub default_severity { $SEVERITY_LOW } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub default_themes { 'community' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | ▶ | sub applies_to { 'PPI::Token::Word' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 11 | ▶ | sub violates { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
violates: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | my ($self, $elem) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 11 | ▶ | return () unless (($elem eq 'wantarray' or $elem->literal eq 'CORE::wantarray') and is_function_call $elem); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$elem eq "wantarray" || $elem->literal eq "CORE::wantarray" and is_function_call($elem)
$elem eq "wantarray" or $elem->literal eq "CORE::wantarray"
MC/DC66%2/3
$elem eq "wantarray" || $elem->literal eq "CORE::wantarray" and is_function_call($elem)
$elem eq "wantarray"
$elem->literal eq "CORE::wantarray"
is_function_call($elem)
Truth table60%3/5
A or B and C
A= $elem eq "wantarray"B= $elem->literal eq "CORE::wantarray"C= is_function_call($elem)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | return $self->violation(DESC, EXPL, $elem); | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | 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> | ||||||||||||||||||||||||||||||||||||||||||||||