ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: sawadatsuyoshi@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92643] [Ruby trunk Feature#15848] Silence warning when conditional assignments are wrapped in parentheses
Date: Tue, 14 May 2019 08:13:59 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-78006.20190514081358.cdf8a2d787520e28@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15848.20190514075151@ruby-lang.org

Issue #15848 has been updated by sawa (Tsuyoshi Sawada).


Why does the parentheses indicate that assignment is intended, and not comparison?

----------------------------------------
Feature #15848: Silence warning when conditional assignments are wrapped in parentheses
https://bugs.ruby-lang.org/issues/15848#change-78006

* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Sometime it's convenient to have an assignment in an `if`-condition. The Ruby documentation even contains an example showing this *"most common use of side-effect"* practice:

http://ruby-doc.org/core-2.6.3/doc/syntax/control_expressions_rdoc.html#label-if+Expression
```ruby
if a = object.some_value
  # do something to a
end
```

Running that code however results in a warning:

> warning: found = in conditional, should be ==

It's very unfortunate that the Ruby docs contain example code that the parser complains about. And it's unfortunate that we can't make use of conditional assignments without getting warnings or turning off warnings.

I propose an obvious change to the current warning mechanism:

**Don't show the warning when the assignment is wrapped in parentheses.**

```ruby
if a = object.some_value
  # warning
end

if (a = object.some_value)
  # no warning
end
```

This is the way RoboCop already works and it's also a known workaround from other languages.

It would also allow the documentation to contain a warning-free example.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

  parent reply	other threads:[~2019-05-14  8:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15848.20190514075151@ruby-lang.org>
2019-05-14  7:51 ` [ruby-core:92642] [Ruby trunk Feature#15848] Silence warning when conditional assignments are wrapped in parentheses mail
2019-05-14  8:13 ` sawadatsuyoshi [this message]
2019-05-14  9:08 ` [ruby-core:92644] " mail
2019-05-14  9:43 ` [ruby-core:92645] " shevegen
2019-05-14 10:35 ` [ruby-core:92646] " mail
2019-05-15  6:49 ` [ruby-core:92652] " nobu
2019-05-15  8:26 ` [ruby-core:92653] " mail

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-78006.20190514081358.cdf8a2d787520e28@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).