File Coverage

blib/lib/Perl/Critic/Policy/Compatibility/ProhibitUnixDevNull.pm
Criterion Covered Total %
statement 45 45 100.0
branch 8 8 100.0
condition 5 6 83.3
subroutine 14 14 100.0
pod 1 1 100.0
total 73 74 98.6


line stmt bran cond sub pod time code
1             # Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
2              
3             # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by the
5             # Free Software Foundation; either version 3, or (at your option) any later
6             # version.
7             #
8             # Perl-Critic-Pulp is distributed in the hope that it will be useful, but
9             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11             # for more details.
12             #
13             # You should have received a copy of the GNU General Public License along
14             # with Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
15              
16              
17             package Perl::Critic::Policy::Compatibility::ProhibitUnixDevNull;
18 41     41   34002 use 5.006;
  41         171  
19 41     41   239 use strict;
  41         97  
  41         913  
20 41     41   217 use warnings;
  41         108  
  41         1141  
21 41     41   254 use List::Util;
  41         108  
  41         2170  
22 41     41   251 use base 'Perl::Critic::Policy';
  41         159  
  41         5542  
23 41     41   376347 use Perl::Critic::Utils;
  41         126  
  41         794  
24 41     41   38474 use Perl::Critic::Pulp;
  41         106  
  41         2449  
25              
26             our $VERSION = 99;
27              
28 41     41   269 use constant supported_parameters => ();
  41         109  
  41         2840  
29 41     41   297 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
  41         109  
  41         2505  
30 41     41   272 use constant default_themes => qw(pulp bugs);
  41         94  
  41         2690  
31 41         5020 use constant applies_to => qw(PPI::Token::Quote
32 41     41   273 PPI::Token::QuoteLike::Words);
  41         105  
33              
34             # See Perl_do_openn() for IsSPACE allowed leading, after mode and trailing.
35             # No layers in a two-arg open, only < > >> etc.
36             #
37 41         11049 use constant _DEV_NULL_RE => qr{^\s*
38             (\+?(<|>>?)\s*)?
39             /dev/null
40             \s*$
41 41     41   328 }sxo;
  41         108  
42              
43             my %equality_operators = (eq => 1, ne => 1);
44              
45             sub violates {
46 10     10 1 547361 my ($self, $elem, $document) = @_;
47              
48 10 100       57 if ($elem->isa('PPI::Token::QuoteLike::Words')) {
49 2 100   3   14 return unless List::Util::first {$_ eq '/dev/null'} $elem->literal;
  3         140  
50              
51             } else { # PPI::Token::Quote
52 8         38 my $str = $elem->string;
53 8 100       140 return unless $str =~ _DEV_NULL_RE;
54              
55             # Allow ... eq 'dev/null' or 'dev/null' eq ...
56             #
57             # Could think about the filetest operators too. -e '/dev/null' is
58             # probably a portability check, but believe still better to have
59             # File::Spec->devnull there.
60             #
61 6         54 foreach my $adj ($elem->sprevious_sibling, $elem->snext_sibling) {
62 11 100 66     458 if ($adj
      100        
63             && $adj->isa('PPI::Token::Operator')
64             && $equality_operators{$adj}) {
65 2         18 return;
66             }
67             }
68             }
69              
70 5         26 return $self->violation
71             ('For maximum portability use File::Spec->devnull instead of "/dev/null"',
72             '',
73             $elem);
74             }
75              
76             1;
77             __END__
78              
79             =for stopwords filename backticks Ryde
80              
81             =head1 NAME
82              
83             Perl::Critic::Policy::Compatibility::ProhibitUnixDevNull - don't use explicit /dev/null
84              
85             =head1 DESCRIPTION
86              
87             This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
88             add-on. It ask you to not to use filename
89              
90             =over
91              
92             F</dev/null>
93              
94             =back
95              
96             explicitly, but instead C<File::Spec-E<gt>devnull()> for maximum portability
97             across operating systems.
98              
99             This policy is under the C<maintenance> theme (see
100             L<Perl::Critic/POLICY THEMES>) on the basis that even if you're on a Unix
101             system now you never know where your code might travel in the future.
102              
103             C<devnull()> is new in C<File::Spec> version 0.8, so you should require that
104             version (it's included in Perl 5.6.0 and up).
105              
106             The checks for F</dev/null> are unsophisticated. A violation is reported
107             for any string C</dev/null>, possibly with an C<open> style mode part, and
108             any C<qw> containing C</dev/null>.
109              
110             open my $fh, '< /dev/null'; # bad
111             do_something ("/dev/null"); # bad
112             foreach my $file (qw(/dev/null /etc/passwd)) # bad
113              
114             String comparisons are allowed because they're not uses of F</dev/null> as
115             such but likely some sort of cross-platform check.
116              
117             if ($f eq '/dev/null') { ... } # ok
118             return ($f ne '>/dev/null'); # ok
119              
120             F</dev/null> as just part of a string is allowed, including things like
121             backticks and C<system>.
122              
123             print "Flames to /dev/null please\n" # ok
124             system ('rmdir /foo/bar >/dev/null 2>&1'); # ok
125             $hi = `echo hi </dev/null`; # ok
126              
127             Whether F</dev/null> is a good idea in such command strings depends what
128             sort of shell you reach with that command and how much of Unix it might
129             emulate on a non-Unix system.
130              
131             =head2 Disabling
132              
133             If you only ever use a system with F</dev/null> or if everything else you
134             write is hopelessly wedded to Unix anyway then you can disable
135             C<ProhibitUnixDevNull> from your F<.perlcriticrc> in the usual way (see
136             L<Perl::Critic/CONFIGURATION>),
137              
138             [-Compatibility::ProhibitUnixDevNull]
139              
140             =head1 SEE ALSO
141              
142             L<Perl::Critic::Pulp>, L<Perl::Critic>, L<File::Spec>
143              
144             =head1 HOME PAGE
145              
146             L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
147              
148             =head1 COPYRIGHT
149              
150             Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
151              
152             Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
153             under the terms of the GNU General Public License as published by the Free
154             Software Foundation; either version 3, or (at your option) any later
155             version.
156              
157             Perl-Critic-Pulp is distributed in the hope that it will be useful, but
158             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
159             or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
160             more details.
161              
162             You should have received a copy of the GNU General Public License along with
163             Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
164              
165             =cut