Project

General

Profile

Actions

Bug #12881

closed

Double-bang (!!) raises warning on string literal

Added by rrroybbbean (RRRoy BBBean) over 7 years ago. Updated over 7 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
[ruby-core:77801]

Description

I use !!x or !!(x) to force x to true or false.
I just noticed this trick raises a warning with string literals.
It would be nice if this warning were not raised, since it is sometimes convenient to force some object to true or false.
I first saw this in irb, but it also happens when running a script from the command line.
Caveat: Although the output of my sample program suggests that double-quoted strings avoid this warning, it actually happens with !!"hi" as well as !!'hi'

CODE: bb.rb
1 p [ '!!false', !!false ]
2 p [ '!!(false)', !!(false) ]
3 p [ '!!nil', !!nil ]
4 p [ '!!(nil)', !!(nil) ]
5 p [ '!!true', !!true ]
6 p [ '!!(true)', !!(true) ]
7 p [ '!![]', !![] ]
8 p [ '!!([])', !!([]) ]
9 p [ '!!{}', !!{} ]
10 p [ '!!Object.new', !!Object.new ]
11 p [ '!!(Object.new)', !!(Object.new) ]
12 p [ '!!1', !!1 ]
13 p [ '!!(1)', !!(1) ]
14 p [ '!!0.1', !!0.1 ]
15 p [ '!!(0.1)', !!(0.1) ]
16 p [ '!!:a', !!:a ]
17 p [ '!!(:a)', !!(:a) ]
18 p [ '!!({})', !!({}) ]
19 p [ '!!String.new', !!String.new ]
20 p [ '!!(String.new)', !!(String.new) ]
21 p [ '!!""', !!"" ]
22 p [ '!!("")', !!("") ]
23 p [ '!!"b"', !!"b" ]
24 p [ '!!("b")', !!("b") ]
25 p [ "!!''", !!'' ]
26 p [ "!!('')", !!('') ]
27 p [ "!!'b'", !!'b' ]
28 p [ "!!('b')", !!('b') ]

OUTPUT
bb.rb:25: warning: string literal in condition
bb.rb:26: warning: string literal in condition
bb.rb:27: warning: string literal in condition
bb.rb:28: warning: string literal in condition
["!!false", false]
["!!(false)", false]
["!!nil", false]
["!!(nil)", false]
["!!true", true]
["!!(true)", true]
["!![]", true]
["!!([])", true]
["!!{}", true]
["!!Object.new", true]
["!!(Object.new)", true]
["!!1", true]
["!!(1)", true]
["!!0.1", true]
["!!(0.1)", true]
["!!:a", true]
["!!(:a)", true]
["!!({})", true]
["!!String.new", true]
["!!(String.new)", true]
["!!""", true]
["!!("")", true]
["!!"b"", true]
["!!("b")", true]
["!!''", true]
["!!('')", true]
["!!'b'", true]
["!!('b')", true]

ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
I compiled it myself from sources, running on Fedora24 amd64 workstation


Files

bb.rb (685 Bytes) bb.rb the code i use to produce the example rrroybbbean (RRRoy BBBean), 10/28/2016 05:33 PM
Actions #1

Updated by nobu (Nobuyoshi Nakada) over 7 years ago

  • Status changed from Open to Closed

Applied in changeset r56522.


parse.y: !-operator warning

  • parse.y (cond0): !-operator is a method call, no warning for
    literal in condition. [ruby-core:77801] [Bug #12881]

Updated by nagachika (Tomoyuki Chikanaga) over 7 years ago

  • Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: REQUIRED

Updated by rrroybbbean (RRRoy BBBean) over 7 years ago

This is a follow-up. I tested for the same issue with Ruby 2.4.0 preview
2. I got slightly different behavior, but essentially the same issue.
Here are the details. I wasn't sure if I could add them to the issue
tracker without making a mess, so I posted them here.

I downloaded Ruby 2.4.0 preview 2 and built it alongside 2.3.1. The
configure and make ran fine. I did not install 2.4.0p2, but I was able
to run it from inside the build directory (with gems disabled). I ran it
on the same test file that I uploaded for the original issue. This time,
all 8 lines of code with string literals generated the warning.

sh> ./ruby --disable-gems bb.rb
bb.rb:21: warning: string literal in condition
bb.rb:22: warning: string literal in condition
bb.rb:23: warning: string literal in condition
bb.rb:24: warning: string literal in condition
bb.rb:25: warning: string literal in condition
bb.rb:26: warning: string literal in condition
bb.rb:27: warning: string literal in condition
bb.rb:28: warning: string literal in condition
["!!false", false]
["!!(false)", false]
["!!nil", false]
["!!(nil)", false]
["!!true", true]
["!!(true)", true]
["!![]", true]
["!!([])", true]
["!!{}", true]
["!!Object.new", true]
["!!(Object.new)", true]
["!!1", true]
["!!(1)", true]
["!!0.1", true]
["!!(0.1)", true]
["!!:a", true]
["!!(:a)", true]
["!!({})", true]
["!!String.new", true]
["!!(String.new)", true]
["!!""", true]
["!!("")", true]
["!!"b"", true]
["!!("b")", true]
["!!''", true]
["!!('')", true]
["!!'b'", true]
["!!('b')", true]
sh> ./ruby --version
ruby 2.4.0preview2 (2016-09-09 trunk 56129) [x86_64-linux]

On 10/29/2016 01:29 PM, wrote:

Issue #12881 has been updated by Tomoyuki Chikanaga.

Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: REQUIRED


Bug #12881: Double-bang (!!) raises warning on string literal
https://bugs.ruby-lang.org/issues/12881#change-61117

  • Author: RRRoy BBBean
  • Status: Closed
  • Priority: Normal
  • Assignee:
  • ruby -v: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
  • Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: REQUIRED

I use !!x or !!(x) to force x to true or false.
I just noticed this trick raises a warning with string literals.
It would be nice if this warning were not raised, since it is sometimes convenient to force some object to true or false.
I first saw this in irb, but it also happens when running a script from the command line.
Caveat: Although the output of my sample program suggests that double-quoted strings avoid this warning, it actually happens with !!"hi" as well as !!'hi'

CODE: bb.rb
1 p [ '!!false', !!false ]
2 p [ '!!(false)', !!(false) ]
3 p [ '!!nil', !!nil ]
4 p [ '!!(nil)', !!(nil) ]
5 p [ '!!true', !!true ]
6 p [ '!!(true)', !!(true) ]
7 p [ '!![]', !![] ]
8 p [ '!!([])', !!([]) ]
9 p [ '!!{}', !!{} ]
10 p [ '!!Object.new', !!Object.new ]
11 p [ '!!(Object.new)', !!(Object.new) ]
12 p [ '!!1', !!1 ]
13 p [ '!!(1)', !!(1) ]
14 p [ '!!0.1', !!0.1 ]
15 p [ '!!(0.1)', !!(0.1) ]
16 p [ '!!:a', !!:a ]
17 p [ '!!(:a)', !!(:a) ]
18 p [ '!!({})', !!({}) ]
19 p [ '!!String.new', !!String.new ]
20 p [ '!!(String.new)', !!(String.new) ]
21 p [ '!!""', !!"" ]
22 p [ '!!("")', !!("") ]
23 p [ '!!"b"', !!"b" ]
24 p [ '!!("b")', !!("b") ]
25 p [ "!!''", !!'' ]
26 p [ "!!('')", !!('') ]
27 p [ "!!'b'", !!'b' ]
28 p [ "!!('b')", !!('b') ]

OUTPUT
bb.rb:25: warning: string literal in condition
bb.rb:26: warning: string literal in condition
bb.rb:27: warning: string literal in condition
bb.rb:28: warning: string literal in condition
["!!false", false]
["!!(false)", false]
["!!nil", false]
["!!(nil)", false]
["!!true", true]
["!!(true)", true]
["!![]", true]
["!!([])", true]
["!!{}", true]
["!!Object.new", true]
["!!(Object.new)", true]
["!!1", true]
["!!(1)", true]
["!!0.1", true]
["!!(0.1)", true]
["!!:a", true]
["!!(:a)", true]
["!!({})", true]
["!!String.new", true]
["!!(String.new)", true]
["!!""", true]
["!!("")", true]
["!!"b"", true]
["!!("b")", true]
["!!''", true]
["!!('')", true]
["!!'b'", true]
["!!('b')", true]

ruby --version
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
I compiled it myself from sources, running on Fedora24 amd64 workstation

---Files--------------------------------
bb.rb (685 Bytes)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0