Perl / perl5

đŸȘ The Perl programming language
https://dev.perl.org/perl5/
Other
1.93k stars 552 forks source link

no warning for useless /d in tr/0-9//d #4586

Closed p5pRT closed 11 years ago

p5pRT commented 22 years ago

Migrated from rt.perl.org#7911 (status was 'rejected')

Searchable as RT7911$

p5pRT commented 22 years ago

From sdouglas@arm.com

[1a] perl could give a warning\, e.g. "Useless use of transliteration (tr///) in void context"\, for the following​:   perl -we '$_ = "1x32141x98234"; tr/x//'   perl -we 'my $x = "1x32141x98234"; $x =~ tr/x//' and

[1b] (somewhat harder and probably not as useful)   perl -we '$_ = "1x32141x98234"; tr/a-z/a-def-z//'   perl -we 'my $x = "1x32141x98234"; $x =~ tr/a-z/a-def-z/'

[2a] perl could give a warning\, e.g. "Useless use of /d in transliteration (tr///)"\, for the following​:   perl -we 'my $x = "1x32141x98234"; $x =~ tr/x//d' (which then becomes case [1a]) and

After the /d is known to be useless it could allow   perl -we '"1x32141x98234" =~ tr/x//d'

[2b] (somewhat harder and probably not as useful)   perl -we 'my $x = "1x32141x98234"; $x =~ tr/a-z/l-za-m/d' The tr is useful but the /d isn't.

Perl Info ``` Flags: category=core severity=low Site configuration information for perl v5.6.1: Configured by steve at Wed Apr 25 02:38:38 EDT 2001. Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration: Platform: osname=solaris, osvers=2.5.1, archname=sun4-solaris uname='sunos smc 5.5.1 generic_103640-32 sun4m sparc sunw,sparcstation-10 ' config_args='-Dcc=gcc -de' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=undef d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef Compiler: cc='gcc', ccflags ='-fno-strict-aliasing -I/usr/local/include ', optimize='-O', cppflags='-fno-strict-aliasing -I/usr/local/include' ccversion='', gccversion='2.95.3 20010315 (release)', gccosandvers='solaris2.5.1' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=4321 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4 alignbytes=8, usemymalloc=y, prototype=define Linker and Libraries: ld='gcc', ldflags =' -L/usr/local/lib ' libpth=/usr/local/lib /usr/lib /usr/ccs/lib libs=-lsocket -lnsl -ldl -lm -lc perllibs=-lsocket -lnsl -ldl -lm -lc libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' ' cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib' Locally applied patches: @INC for perl v5.6.1: /usr/local/lib/perl5/5.6.1/sun4-solaris /usr/local/lib/perl5/5.6.1 /usr/local/lib/perl5/site_perl/5.6.1/sun4-solaris /usr/local/lib/perl5/site_perl/5.6.1 /usr/local/lib/perl5/site_perl . Environment for perl v5.6.1: HOME=/home/sdouglas LANG (unset) LANGUAGE (unset) LD_LIBRARY_PATH=/usr/openwin/lib LOGDIR (unset) PATH=/eda/tools/gnu/native2.2/sunos5.6/bin:/home/sdouglas/bin:/chips/sdt/bin/solrs:/chips/sdt/scripts:/home/sdouglas/sdt/buildtools/bin/solrs:/home/sdouglas/sdt/buildtools/scripts/portable:/usr/local/contrib/bin:/usr/local/lsf/bin:/usr/local/bin:/usr/openwin/bin:/usr/openwin/bin/xview:/usr/local/gnu/bin:/usr/xpg4/bin:/bin:/usr/bin PERL_BADLANG (unset) SHELL=/bin/tcsh ```
p5pRT commented 22 years ago

From @tamias

[2a] perl could give a warning\, e.g. "Useless use of /d in transliteration (tr///)"\, for the following​: perl -we 'my $x = "1x32141x98234"; $x =~ tr/x//d'

How is that a useless use of /d?

(which then becomes case [1a]) and

After the /d is known to be useless it could allow perl -we '"1x32141x98234" =~ tr/x//d'

[2b] (somewhat harder and probably not as useful) perl -we 'my $x = "1x32141x98234"; $x =~ tr/a-z/l-za-m/d' The tr is useful but the /d isn't.

That's probably not very hard at all; simply count the number of characters in each class\, and if they're the same\, the /d is useless.

There could also be a warning when the left side includes more characters than the right side.

Of course\, adding these warnings would probably affect a fair amount of existing code.

Ronald

p5pRT commented 22 years ago

From [Unknown Contact. See original ticket]

You are correct. I though tr/x//d would be equivalent to tr/x/x/d. I missed this

  If the "/d" modifier is used\, the REPLACEMENTLIST is always   interpreted exactly as specified.

Sorry. The rest of my bug report is probably not important enough to bother with.

p5pRT commented 15 years ago

From module@renee-baecker.de

I don't know if these new warnings are wanted\, but attached is a patch that adds the new warnings\, tests and entries in perldiag.pod...

p5pRT commented 15 years ago

From module@renee-baecker.de

7911.patch ```diff --- op.c.orig.20081117 2008-11-17 14:14:31.000000000 +0100 +++ op.c 2008-11-17 14:54:25.000000000 +0100 @@ -3328,6 +3328,14 @@ } } } + + if( del && rlen == tlen && ckWARN( WARN_MISC ) ){ + Perl_warner( aTHX_ packWARN( WARN_MISC ), "Useless use of /d modifier in transliteration operator" ); + } + else if( rlen > tlen && ckWARN( WARN_MISC ) ){ + Perl_warner( aTHX_ packWARN( WARN_MISC ), "Replacementlist is longer than searchlist" ); + } + if (grows) o->op_private |= OPpTRANS_GROWS; #ifdef PERL_MAD --- pod/perldiag.pod.orig 2008-11-17 14:16:32.000000000 +0100 +++ pod/perldiag.pod 2008-11-17 14:57:21.000000000 +0100 @@ -3633,6 +3633,12 @@ numeric field that will never go blank so that the repetition never terminates. You might use ^# instead. See L. +=item Replacementlist is longer than searchlist + +(W misc) You have used a replacementlist that is longer than the +searchlist. So the additional elements in the replacementlist +are meaningless. + =item Reversed %s= operator (W syntax) You wrote your assignment operator backwards. The = must @@ -4588,6 +4594,12 @@ The <-- HERE shows in the regular expression about where the problem was discovered. See L. +=item Useless use of /d modifier in transliteration operator + +(W misc) You have used the /d modifier where the searchlist has the +same length as the replacelist. See L for more information +about the /d modifier. + =item Useless use of %s in void context (W void) You did something without a side effect in a context that does --- t/lib/warnings/op.orig 2008-11-17 15:08:56.000000000 +0100 +++ t/lib/warnings/op 2008-11-17 14:58:27.000000000 +0100 @@ -539,7 +539,7 @@ # op.c # use warnings 'misc' ; -my $a ; my @a = () ; my %a = () ; my $b = \@a ; my $c = \%a ; +my $a ; my @a = () ; my %a = () ; my $b = \@a ; my $c = \%a ;my $d = 'test'; @a =~ /abc/ ; @a =~ s/a/b/ ; @a =~ tr/a/b/ ; @@ -552,9 +552,11 @@ %$c =~ /abc/ ; %$c =~ s/a/b/ ; %$c =~ tr/a/b/ ; +$d =~ tr/a/b/d ; +$d =~ tr/a/bc/; { no warnings 'misc' ; -my $a ; my @a = () ; my %a = () ; my $b = \@a ; my $c = \%a ; +my $a ; my @a = () ; my %a = () ; my $b = \@a ; my $c = \%a ; my $d = 'test'; @a =~ /abc/ ; @a =~ s/a/b/ ; @a =~ tr/a/b/ ; @@ -567,6 +569,8 @@ %$c =~ /abc/ ; %$c =~ s/a/b/ ; %$c =~ tr/a/b/ ; +$d =~ tr/a/b/d ; +$d =~ tr/a/bc/ ; } EXPECT Applying pattern match (m//) to @array will act on scalar(@array) at - line 5. @@ -581,8 +585,10 @@ Applying pattern match (m//) to %hash will act on scalar(%hash) at - line 14. Applying substitution (s///) to %hash will act on scalar(%hash) at - line 15. Applying transliteration (tr///) to %hash will act on scalar(%hash) at - line 16. +Useless use of /d modifier in transliteration operator at - line 17. +Replacementlist is longer than searchlist at - line 18. Can't modify private array in substitution (s///) at - line 6, near "s/a/b/ ;" -BEGIN not safe after errors--compilation aborted at - line 18. +BEGIN not safe after errors--compilation aborted at - line 20. ######## # op.c use warnings 'parenthesis' ; ```
p5pRT commented 12 years ago

From @jkeenan

On Mon Nov 17 06​:13​:52 2008\, reneeb wrote​:

I don't know if these new warnings are wanted\, but attached is a patch that adds the new warnings\, tests and entries in perldiag.pod...

Can anyone comment on the request for addition of two more warnings in the patch attached?

Thank you very much. Jim Keenan

p5pRT commented 12 years ago

From @cpansprout

On Fri Apr 20 18​:28​:01 2012\, jkeenan wrote​:

On Mon Nov 17 06​:13​:52 2008\, reneeb wrote​:

I don't know if these new warnings are wanted\, but attached is a patch that adds the new warnings\, tests and entries in perldiag.pod...

Can anyone comment on the request for addition of two more warnings in the patch attached?

I don’t think these warnings are really necessary. They will be more of an annoyance than a help. But most code is unlikely to run into them\, so I won’t quibble over it if someone else really wants them in.

--

Father Chrysostomos

p5pRT commented 11 years ago

From @jkeenan

On Fri Apr 20 22​:16​:29 2012\, sprout wrote​:

On Fri Apr 20 18​:28​:01 2012\, jkeenan wrote​:

On Mon Nov 17 06​:13​:52 2008\, reneeb wrote​:

I don't know if these new warnings are wanted\, but attached is a patch that adds the new warnings\, tests and entries in perldiag.pod...

Can anyone comment on the request for addition of two more warnings in the patch attached?

I don’t think these warnings are really necessary. They will be more of an annoyance than a help. But most code is unlikely to run into them\, so I won’t quibble over it if someone else really wants them in.

In ten months\, no one has seconded the motion to apply the patch. Hence\, I'm going to conclude that it should not be applied and am closing this ticket.

Thank you very much. Jim Keenan

p5pRT commented 11 years ago

@jkeenan - Status changed from 'open' to 'rejected'