Debian Codegraph @

t/RegularExpressions/ProhibitUnusedCapture.run

raw
1 ## name non-captures
2 ## failures 0
3 ## cut
4
5 m/foo/;
6 m/(?:foo)/;
7
8 if (m/foo/) {
9 print "bar";
10 }
11
12 #-----------------------------------------------------------------------------
13
14 ## name assignment captures
15 ## failures 0
16 ## cut
17
18 my ($foo) = m/(foo)/;
19 my ($foo) = m/(foo|bar)/;
20 my ($foo) = m/(foo)(?:bar)/;
21 my @foo = m/(foo)/;
22 my @foo = m/(foo)/g;
23 my %foo = m/(foo)(bar)/g;
24
25 my ($foo, $bar) = m/(foo)(bar)/;
26 my @foo = m/(foo)(bar)/;
27 my ($foo, @bar) = m/(foo)(bar)/;
28 my ($foo, @bar) = m/(foo)(bar)(baz)/;
29
30 #-----------------------------------------------------------------------------
31
32 ## name undef array captures
33 ## failures 0
34 ## cut
35
36 () = m/(foo)/;
37 (undef) = m/(foo)/;
38 my ($foo) =()= m/(foo)/g;
39
40 #-----------------------------------------------------------------------------
41
42 ## name complex array assignment captures
43 ## failures 0
44 ## cut
45
46 @$foo = m/(foo)(bar)/;
47 @{$foo} = m/(foo)(bar)/;
48 %$foo = m/(foo)(bar)/;
49 %{$foo} = m/(foo)(bar)/;
50
51 ($foo,@$foo) = m/(foo)(bar)/;
52 ($foo,@{$foo}) = m/(foo)(bar)/;
53
54 #-----------------------------------------------------------------------------
55
56 ## name conditional captures
57 ## failures 0
58 ## cut
59
60 if (m/(foo)/) {
61 my $foo = $1;
62 print $foo;
63 }
64 if (m/(foo)(bar)/) {
65 my $foo = $1;
66 my $bar = $2;
67 print $foo, $bar;
68 }
69 if (m/(foo)(bar)/) {
70 my ($foo, $bar) = ($1, $2);
71 print $foo, $bar;
72 }
73 if (m/(foo)(bar)/) {
74 my (@foo) = ($1, $2);
75 print @foo;
76 }
77
78 if (m/(foo)/) {
79 # bug, but not a violation of THIS policy
80 my (@foo) = ($1, $2);
81 print @foo;
82 }
83
84 #-----------------------------------------------------------------------------
85
86 ## name RT #38942
87 ## failures 0
88 ## cut
89
90 while ( pos() < length ) {
91 m{\G(a)(b)(c)}gcxs or die;
92 my ($a, $b, $c) = ($1, $2, $3);
93 }
94
95 #-----------------------------------------------------------------------------
96
97 ## name boolean and ternary captures
98 ## failures 0
99 ## cut
100
101 m/(foo)/ && print $1;
102 m/(foo)/ ? print $1 : die;
103 m/(foo)/ && ($1 == 'foo') ? print 1 : die;
104
105 #-----------------------------------------------------------------------------
106
107 ## name loop captures
108 ## failures 0
109 ## cut
110
111 for (m/(foo)/) {
112 my $foo = $1;
113 print $foo;
114 }
115
116 #-----------------------------------------------------------------------------
117
118 ## name slurpy array loop captures
119 ## failures 0
120 ## cut
121
122 map {print} m/(foo)/;
123 foo(m/(foo)/);
124 foo('bar', m/(foo)/);
125 foo(m/(foo)/, 'bar');
126 foo m/(foo)/;
127 foo 'bar', m/(foo)/;
128 foo m/(foo)/, 'bar';
129
130 ## name slurpy with assignment
131 ## failures 0
132 ## cut
133
134 my ($foo) = grep {$b++ == 2} m/(foo)/g;
135 my ($foo) = grep {$b++ == 2} $str =~ m/(foo)/g;
136
137 #-----------------------------------------------------------------------------
138
139 ## name slurpy with array assignment
140 ## failures 0
141 ## cut
142
143 my @foo = grep {$b++ > 2} m/(foo)/g;
144 my @foo = grep {$b++ > 2} $str =~ m/(foo)/g;
145
146 #-----------------------------------------------------------------------------
147
148 ## name assignment captures on string
149 ## failures 0
150 ## cut
151
152 my ($foo) = $str =~ m/(foo)/;
153 my ($foo) = $str =~ m/(foo|bar)/;
154 my ($foo) = $str =~ m/(foo)(?:bar)/;
155 my @foo = $str =~ m/(foo)/;
156 my @foo = $str =~ m/(foo)/g;
157
158 my ($foo, $bar) = $str =~ m/(foo)(bar)/;
159 my @foo = $str =~ m/(foo)(bar)/;
160 my ($foo, @bar) = $str =~ m/(foo)(bar)/;
161 my (@bar) = $str =~ m/(foo)(bar)/;
162 my ($foo, @bar) = $str =~ m/(foo)(bar)(baz)/;
163
164 #-----------------------------------------------------------------------------
165
166 ## name slurpy captures on string
167 ## failures 0
168 ## cut
169
170 map {print} $str =~ m/(foo)/g;
171
172 #-----------------------------------------------------------------------------
173
174 ## name self captures
175 ## failures 0
176 ## cut
177
178 m/(foo)\1/;
179 s/(foo)/$1/;
180 s/(foo)/\1/;
181 s<\A t[\\/] (\w+) [\\/] (\w+) [.]run \z><$1\::$2>xms
182
183 #-----------------------------------------------------------------------------
184
185 ## name basic failures
186 ## failures 5
187 ## cut
188
189 m/(foo)/;
190 my ($foo) = m/(foo)/g;
191
192 if (m/(foo)/) {
193 print "bar";
194 }
195 if (m/(foo)(bar)/) {
196 my $foo = $1;
197 print $foo;
198 }
199
200 for (m/(foo)/) {
201 print "bar";
202 }
203
204 #-----------------------------------------------------------------------------
205
206 ## name negated regexp failures
207 ## failures 1
208 ## cut
209
210 my ($foo) = $str !~ m/(foo)/;
211
212 #-----------------------------------------------------------------------------
213
214 ## name statement failures
215 ## failures 1
216 ## cut
217
218 m/(foo)/ && m/(bar)/ && print $1;
219
220 #-----------------------------------------------------------------------------
221
222 ## name sub failures
223 ## failures 1
224 ## cut
225
226 sub foo {
227 m/(foo)/;
228 return;
229 }
230 print $1;
231
232 #-----------------------------------------------------------------------------
233
234 ## name anon sub failures
235 ## failures 1
236 ## TODO PPI v1.118 doesn't recognize anonymous subroutines
237 ## cut
238
239 my $sub = sub foo {
240 m/(foo)/;
241 return;
242 };
243 print $1;
244
245 #-----------------------------------------------------------------------------
246
247 ## name ref constructors
248 ## failures 0
249 ## cut
250
251 $f = { m/(\w+)=(\w+)/g };
252 $f = [ m/(\w+)/g ];
253
254 #-----------------------------------------------------------------------------
255
256 ## name sub returns
257 ## failures 0
258 ## cut
259
260 sub foo {
261 m/(foo)/;
262 }
263 sub foo {
264 return m/(foo)/;
265 }
266 map { m/(foo)/ } (1, 2, 3);
267
268 #-----------------------------------------------------------------------------
269
270 ## name failing regexp with syntax error
271 ## failures 0
272 ## cut
273
274 m/(foo)(/;
275
276 #-----------------------------------------------------------------------------
277
278 ## name lvalue sub assignment pass
279 ## failures 0
280 ## cut
281
282 (substr $str, 0, 1) = m/(\w+)/;
283
284 #-----------------------------------------------------------------------------
285
286 ## name lvalue sub assignment failure
287 ## failures 1
288 ## TODO lvalue subs are too complex to support
289 ## cut
290
291 (substr $str, 0, 1) = m/(\w+)(\d+)/;
292
293 #-----------------------------------------------------------------------------
294
295 ## name code coverage
296 ## failures 1
297 ## cut
298
299 m/(foo)/;
300 print $0;
301 print @ARGV;
302 print $_;
303
304 #-----------------------------------------------------------------------------
305
306 ## name while loop with /g
307 ## failures 0
308 ## cut
309
310 while (m/(\d+)/g) {
311 print $1, "\n";
312 }
313
314 #-----------------------------------------------------------------------------
315
316 ## name conditional named captures
317 ## failures 0
318 ## cut
319
320 if ( m/(?<foo>bar)/ ) {
321 print $+{foo}, "\n";
322 }
323
324 while ( m/(?'foo'\d+)/g ) {
325 print $-{foo}[0], "\n";
326 }
327
328 m/(?P<foo>\w+)|(?<foo>\W+)/ and print $+{foo}, "\n";
329
330 #-----------------------------------------------------------------------------
331
332 ## name named capture in array context is unused
333 ## failures 2
334 ## cut
335
336 my @foo = m/(?<foo>\w+)/;
337 sub foo {
338 return m/(?<foo>\W+)/;
339 }
340
341 #-----------------------------------------------------------------------------
342
343 ## name named capture in array context with siblings is OK
344 ## failures 0
345 ## cut
346
347 my @foo = m/(?<foo>\w+)/;
348 print $+{foo}, "\n";
349
350 #-----------------------------------------------------------------------------
351
352 ## name named capture not used in replacement
353 ## failures 1
354 ## cut
355
356 s/(?<foo>\w+)/foo$1/g;
357
358 #-----------------------------------------------------------------------------
359
360 ## name named capture used in replacement
361 ## failures 0
362 ## cut
363
364 s/(?<foo>\w+)/foo$+{foo}/g;
365
366 #-----------------------------------------------------------------------------
367
368 ## name subscripted capture
369 ## failures 0
370 ## cut
371
372 s/(foo)/$+[ 1 ]/;
373 s/(foo)/$-[ 1 ]/;
374 s/(foo)/$+[ -1 ]/;
375 s/(foo)/$-[ -1 ]/;
376 m/(\w+)/ and print substr( $_, $-[ 1 ], $+[ 1 ] - $-[ 1 ] );
377 m/(\w+)/ and print substr( $_, $-[ -1 ], $+[ -1 ] - $-[ -1 ] );
378
379 #-----------------------------------------------------------------------------
380
381 ## name named capture English name in replacement RT #60002
382 ## failures 1
383 ## cut
384
385 s/(?<foo>\w+)/foo$LAST_PAREN_MATCH{foo}/g;
386
387 #-----------------------------------------------------------------------------
388
389 ## name named capture English name in code RT #60002
390 ## failures 1
391 ## cut
392
393
394 m/(?P<foo>\w+)|(?<foo>\W+)/ and print $LAST_PAREN_MATCH{foo}, "\n";
395
396 #-----------------------------------------------------------------------------
397
398 ## name named capture English name in replacement RT #60002
399 ## failures 0
400 ## cut
401
402 use English;
403
404 s/(?<foo>\w+)/foo$LAST_PAREN_MATCH{foo}/g;
405
406 #-----------------------------------------------------------------------------
407
408 ## name named capture English name in code RT #60002
409 ## failures 0
410 ## cut
411
412 use English;
413
414 m/(?P<foo>\w+)|(?<foo>\W+)/ and print $LAST_PAREN_MATCH{foo}, "\n";
415
416 #-----------------------------------------------------------------------------
417
418 ## name English subscripted capture without use English
419 ## failures 6
420 ## cut
421
422 s/(foo)/$LAST_MATCH_END[ 1 ]/;
423 s/(foo)/$LAST_MATCH_START[ 1 ]/;
424 s/(foo)/$LAST_MATCH_END[ -1 ]/;
425 s/(foo)/$LAST_MATCH_START[ -1 ]/;
426 m/(\w+)/ and print substr(
427 $_, $LAST_MATCH_START[ 1 ], $LAST_MATCH_END[ 1 ] - $LAST_MATCH_START[ 1 ] );
428 m/(\w+)/ and print substr(
429 $_, $LAST_MATCH_START[ -1 ],
430 $LAST_MATCH_END[ -1 ] - $LAST_MATCH_START[ -1 ] );
431
432 #-----------------------------------------------------------------------------
433
434 ## name English subscripted capture with use English
435 ## failures 0
436 ## cut
437
438 use English;
439
440 s/(foo)/$LAST_MATCH_END[ 1 ]/;
441 s/(foo)/$LAST_MATCH_START[ 1 ]/;
442 s/(foo)/$LAST_MATCH_END[ -1 ]/;
443 s/(foo)/$LAST_MATCH_START[ -1 ]/;
444 m/(\w+)/ and print substr(
445 $_, $LAST_MATCH_START[ 1 ], $LAST_MATCH_END[ 1 ] - $LAST_MATCH_START[ 1 ] );
446 m/(\w+)/ and print substr(
447 $_, $LAST_MATCH_START[ -1 ],
448 $LAST_MATCH_END[ -1 ] - $LAST_MATCH_START[ -1 ] );
449
450 #-----------------------------------------------------------------------------
451
452 ## name Capture used in substitution portion of s/.../.../e
453 ## failures 0
454 ## cut
455
456 s/(\w+)/$replace{$1} || "<$1>"/ge;
457
458 #-----------------------------------------------------------------------------
459
460 ## name Capture used in double-quotish string. RT #38942 redux
461 ## failures 0
462 ## cut
463
464 m/(\w+)(\W+)/;
465 print "$+[2] $1";
466
467 m/(?<foo>(\w+)/;
468 print "$+{foo}";
469
470 m/(\d+)/;
471 print "${1}234";
472
473 #-----------------------------------------------------------------------------
474
475 ## name Capture used in a here document. RT #38942 redux
476 ## failures 0
477 ## cut
478
479 m/(\w+)(\W+)/;
480 print <<EOD
481 $+[2] $1
482 EOD
483
484 ## name Capture used in an indented here document.
485 ## failures 0
486 ## cut
487
488 m/(\w+)(\W+)/;
489 print <<~EOD
490 $+[2] $1
491 EOD
492
493 #-----------------------------------------------------------------------------
494
495 ## name Alternation. RT #38942 redux
496 ## failures 0
497 ## cut
498
499 if ( /(a)/ || /(b)/ ) {
500 say $1;
501 }
502
503 # Yes, this is incorrect code, but that's ProhibitCaptureWithoutTest's
504 # problem.
505 if ( /(a)/ // /(b)/ ) {
506 say $1;
507 }
508
509 # Contrived, but worse things happen at sea.
510 if ( ( /(a)/ || undef ) // /(b)/ ) {
511 say $1;
512 }
513
514 if ( /(a)/ or /(b)/ ) {
515 say $1;
516 }
517
518 #-----------------------------------------------------------------------------
519
520 ## name Alternation with conjunction. RT #38942 redux
521 ## failures 4
522 ## cut
523
524 # 1 failure here: the /(b)/
525 if ( /(a)/ || /(b)/ && /(c)/ ) {
526 say $1;
527 }
528
529 # 1 failure here: the /(b)/
530 if ( /(a)/ or /(b)/ and /(c)/ ) {
531 say $1;
532 }
533
534 # 2 failures here: the /(a)/ and the /(b)/
535 if ( /(a)/ || /(b)/ and /(c)/ ) {
536 say $1;
537 }
538
539 #-----------------------------------------------------------------------------
540
541 ## name RT #67116 - Incorrect check of here document.
542 ## failures 1
543 ## cut
544
545 $x !~ /()/;
546 <<X;
547 .
548 .
549 .
550 X
551
552 #-----------------------------------------------------------------------------
553
554 ## name RT #69867 - Incorrect check of if() statement if regexp negated
555 ## failures 0
556 ## cut
557
558 if ( $ip !~ /^(.*?)::(.*)\z/sx ) {
559 @fields = split /:/x, $ip;
560 } else {
561 my ( $before, $after ) = ( $1, $2 );
562 }
563
564 #-----------------------------------------------------------------------------
565
566 ## name RT #72086 - False positive with /e and parens
567 ## failures 0
568 ## cut
569
570 s/(.)/($1)/e;
571 s/(.)/ { $1 } /e;
572
573 #-----------------------------------------------------------------------------
574
575 ## name @- and @+
576 ## failures 0
577 ## cut
578
579 if ( m/(at-minus array)/ ) {
580 print @-;
581 }
582
583 if ( m/(at-plus array)/ ) {
584 print @+;
585 }
586
587 #-----------------------------------------------------------------------------
588
589 ## name english @LAST_MATCH_START and @LAST_MATCH_END
590 ## failures 0
591 ## cut
592
593 use English
594
595 if ( m/(LAST_MATCH_START array)/ ) {
596 print @LAST_MATCH_START;
597 }
598
599 if ( m/(LAST_MATCH_END array)/ ) {
600 print @LAST_MATCH_END;
601 }
602
603 #-----------------------------------------------------------------------------
604
605 ## name @{^CAPTURE} added in 5.25.7 (GitHub #778)
606 ## failures 0
607 ## cut
608
609 if ( m/(CAPTURE array)/ ) {
610 print @{^CAPTURE};
611 }
612
613 if ( m/(quoted CAPTURE array)/ ) {
614 print "@{^CAPTURE}\n";
615 }
616
617 if ( m/(CAPTURE array element)/ ) {
618 print ${^CAPTURE}[0];
619 }
620
621 if ( m/(quoted CAPTURE array element)/ ) {
622 print "${^CAPTURE}[0]\n";
623 }
624
625 #-----------------------------------------------------------------------------
626
627 ## name %{^CAPTURE_ALL} added in 5.25.7
628 ## failures 0
629 ## cut
630
631 if ( m/(?<x>CAPTURE_ALL)/ ) {
632 print ${^CAPTURE_ALL}{x};
633 }
634
635 #-----------------------------------------------------------------------------
636
637 ## name ${^CAPTURE_ALL}[1] has nothing to do with captures
638 ## failures 1
639 ## TODO ${^CAPTURE_ALL}[1] has nothing to do with captures
640 ## cut
641
642 if ( m/(CAPTURE_ALL)/ ) {
643 print ${^CAPTURE_ALL}[1];
644 }
645
646 ## name GitHub #888 - False positive with split
647 ## failures 0
648 ## cut
649
650 /(.+)/;
651 my @a = split /x/, $1;
652 my @b = split( /x/, $1 );
653
654 #-----------------------------------------------------------------------------
655
656 ## name %- %+ %{^CAPTURE} capture all named captures
657 ## failures 0
658 ## cut
659
660 if ( m/(?<%>-)/ ) {
661 print %-;
662 }
663
664 if ( m/(?<%>+)/ ) {
665 print %+;
666 }
667
668 if ( m/(?<%>^CAPTURE))/ ) {
669 print %{^CAPTURE};
670 }
671
672 #-----------------------------------------------------------------------------
673
674 ## name %LAST_PAREN_MATCH captures all named captures
675 ## failures 0
676 ## cut
677
678 use English;
679
680 if ( m/(?<x>LAST_PAREN_MATCH)/ ) {
681 print %LAST_PAREN_MATCH;
682 }
683
684 #-----------------------------------------------------------------------------
685
686 ## name %+ doesn't capture unnamed captures
687 ## failures 1
688 ## cut
689
690 if ( m/(foo)(?<bar>bar)/ ) {
691 print %+;
692 }
693
694 #-----------------------------------------------------------------------------
695
696 # Local Variables:
697 # mode: cperl
698 # cperl-indent-level: 4
699 # fill-column: 78
700 # indent-tabs-mode: nil
701 # c-indentation-style: bsd
702 # End:
703 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :