This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
b4cbf9926d6382f6bc2d6112ae83805f75172697
[perl5.git] / ext / B / t / lint.t
1 #!./perl -w
2
3 BEGIN {
4     if ( $ENV{PERL_CORE} ) {
5         chdir('t') if -d 't';
6         @INC = ( '.', '../lib' );
7     }
8     else {
9         unshift @INC, 't';
10         push @INC, "../../t";
11     }
12     require Config;
13     if ( ( $Config::Config{'extensions'} !~ /\bB\b/ ) ) {
14         print "1..0 # Skip -- Perl configured without B module\n";
15         exit 0;
16     }
17     require 'test.pl';
18 }
19
20 plan tests => 29;
21
22 # Runs a separate perl interpreter with the appropriate lint options
23 # turned on
24 sub runlint ($$$;$) {
25     my ( $opts, $prog, $result, $testname ) = @_;
26     my $res = runperl(
27         switches => ["-MO=Lint,$opts"],
28         prog     => $prog,
29         stderr   => 1,
30     );
31     $res =~ s/-e syntax OK\n$//;
32     local $Level = $Level + 1;
33     is( $res, $result, $testname || $opts );
34 }
35
36 runlint 'magic-diamond', 'while(<>){}', <<'RESULT';
37 Use of <> at -e line 1
38 RESULT
39
40 runlint 'magic-diamond', 'while(<ARGV>){}', <<'RESULT';
41 Use of <> at -e line 1
42 RESULT
43
44 runlint 'magic-diamond', 'while(<FOO>){}', <<'RESULT';
45 RESULT
46
47 runlint 'context', '$foo = @bar', <<'RESULT';
48 Implicit scalar context for array in scalar assignment at -e line 1
49 RESULT
50
51 runlint 'context', '$foo = length @bar', <<'RESULT';
52 Implicit scalar context for array in length at -e line 1
53 RESULT
54
55 runlint 'context', 'our @bar', '';
56
57 runlint 'context', 'exists $BAR{BAZ}', '';
58
59 runlint 'implicit-read', '/foo/', <<'RESULT';
60 Implicit match on $_ at -e line 1
61 RESULT
62
63 runlint 'implicit-read', 'grep /foo/, ()', '';
64
65 runlint 'implicit-read', 'grep { /foo/ } ()', '';
66
67 runlint 'implicit-write', 's/foo/bar/', <<'RESULT';
68 Implicit substitution on $_ at -e line 1
69 RESULT
70
71 runlint 'implicit-read', 'for ( @ARGV ) { 1 }',
72     <<'RESULT', 'implicit-read in foreach';
73 Implicit use of $_ in foreach at -e line 1
74 RESULT
75
76 runlint 'implicit-read', '1 for @ARGV', '', 'implicit-read in foreach';
77
78 runlint 'dollar-underscore', '$_ = 1', <<'RESULT';
79 Use of $_ at -e line 1
80 RESULT
81
82 runlint 'dollar-underscore', 'sub foo {}; foo( $_ ) for @A',      '';
83 runlint 'dollar-underscore', 'sub foo {}; map { foo( $_ ) } @A',  '';
84 runlint 'dollar-underscore', 'sub foo {}; grep { foo( $_ ) } @A', '';
85
86 runlint 'dollar-underscore', 'print',
87     <<'RESULT', 'dollar-underscore in print';
88 Use of $_ at -e line 1
89 RESULT
90
91 runlint 'private-names', 'sub A::_f{};A::_f()', <<'RESULT';
92 Illegal reference to private name '_f' at -e line 1
93 RESULT
94
95 runlint 'private-names', '$A::_x', <<'RESULT';
96 Illegal reference to private name '_x' at -e line 1
97 RESULT
98
99 runlint 'private-names', 'sub A::_f{};A->_f()', <<'RESULT',
100 Illegal reference to private method name '_f' at -e line 1
101 RESULT
102     'private-names (method)';
103
104 runlint 'undefined-subs', 'foo()', <<'RESULT';
105 Nonexistant subroutine 'foo' called at -e line 1
106 RESULT
107
108 runlint 'undefined-subs', 'foo();sub foo;', <<'RESULT';
109 Undefined subroutine 'foo' called at -e line 1
110 RESULT
111
112 runlint 'regexp-variables', 'print $&', <<'RESULT';
113 Use of regexp variable $& at -e line 1
114 RESULT
115
116 runlint 'regexp-variables', 's/./$&/', <<'RESULT';
117 Use of regexp variable $& at -e line 1
118 RESULT
119
120 runlint 'bare-subs', 'sub bare(){1};$x=bare', '';
121
122 runlint 'bare-subs', 'sub bare(){1}; $x=[bare=>0]; $x=$y{bare}', <<'RESULT';
123 Bare sub name 'bare' interpreted as string at -e line 1
124 Bare sub name 'bare' interpreted as string at -e line 1
125 RESULT
126
127 {
128
129     # Check for backwards-compatible plugin support. This was where
130     # preloaded mdoules would register themselves with B::Lint.
131     my $res = runperl(
132         switches => ["-MB::Lint"],
133         prog     =>
134             'BEGIN{B::Lint->register_plugin(X=>[q[x]])};use O(qw[Lint x]);sub X::match{warn qq[X ok.\n]};dummy()',
135         stderr => 1,
136     );
137     like( $res, qr/X ok\./, 'Lint legacy plugin' );
138 }
139
140 {
141
142     # Check for Module::Plugin support
143     my $res = runperl(
144         switches => [ '-I../ext/B/t/pluglib', '-MO=Lint,none' ],
145         prog     => 1,
146         stderr   => 1,
147     );
148     like( $res, qr/Module::Pluggable ok\./, 'Lint uses Module::Pluggable' );
149 }