Project

General

Profile

Actions

Feature #15848

closed

Silence warning when conditional assignments are wrapped in parentheses

Added by sos4nt (Stefan Schüßler) over 5 years ago. Updated over 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:92642]

Description

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

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.

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.


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Bug #14562: Do not warn for assignment in conditionals inside ()ClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0