File Coverage

blib/lib/Perl/Critic/Policy/ProhibitOrReturn.pm
Criterion Covered Total %
statement 29 30 96.6
branch 5 6 83.3
condition n/a
subroutine 11 12 91.6
pod 4 5 80.0
total 49 53 92.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::ProhibitOrReturn;
2 3     3   793265 use 5.008001;
  3         28  
3 3     3   18 use strict;
  3         6  
  3         69  
4 3     3   19 use warnings;
  3         7  
  3         111  
5 3     3   501 use parent 'Perl::Critic::Policy';
  3         322  
  3         32  
6 3     3   254138 use constant DESC => '`or return` in source file';
  3         10  
  3         307  
7 3     3   21 use constant EXPL => '`or return` is prohibited. Use equivalent conditional statement instead.';
  3         10  
  3         147  
8              
9 3     3   20 use Perl::Critic::Utils qw( :severities );
  3         10  
  3         159  
10              
11             our $VERSION = "0.01";
12              
13 4     4 0 1134112 sub supported_parameters { return (); }
14 3     3 1 64 sub default_severity { return $SEVERITY_MEDIUM; }
15 0     0 1 0 sub default_themes { return qw(core bugs); }
16 4     4 1 1041057 sub applies_to { return 'PPI::Token::Word'; }
17              
18             sub violates {
19 22     22 1 996 my ($self, $elem, undef) = @_;
20              
21 22 100       87 return if $elem->content ne 'return';
22              
23 9         79 my $sprev = $elem->sprevious_sibling;
24 9 100       276 return if !$sprev;
25 3 50       10 return if $sprev->content ne 'or';
26              
27 3         30 return $self->violation(DESC, EXPL, $elem->parent);
28             }
29              
30             1;
31             __END__
32              
33             =encoding utf-8
34              
35             =head1 NAME
36              
37             Perl::Critic::Policy::ProhibitOrReturn - Do not use `or return`
38              
39             =head1 AFFILIATION
40            
41             This policy is a policy in the L<Perl::Critic::Policy::ProhibitOrReturn> distribution.
42              
43             =head1 DESCRIPTION
44              
45             Avoid using C<or return>. Consider using equivalent C<if> (or C<unless>) statement instead.
46              
47             # not ok
48             sub foo {
49             my ($x) = @_;
50             $x or return;
51             ...
52             }
53              
54             # ok
55             sub foo {
56             my ($x) = @_;
57             return if !$x;
58             ...
59             }
60              
61             =head1 CONFIGURATION
62            
63             This Policy is not configurable except for the standard options.
64              
65             =head1 LICENSE
66              
67             Copyright (C) utgwkk.
68              
69             This library is free software; you can redistribute it and/or modify
70             it under the same terms as Perl itself.
71              
72             =head1 AUTHOR
73              
74             utgwkk E<lt>utagawakiki@gmail.comE<gt>
75              
76             =cut
77