Issue #15848 has been updated by nobu (Nobuyoshi Nakada).

Status changed from Open to Rejected

sos4nt (Stefan Sch=FC=DFler) wrote:
> ```ruby
> if a =3D object.some_value
>   # do something to a
> end
> ```
> =

> Running that code however results in a warning:
> =

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

No, it doesn't.
That warning is only when the RHS is a literal, but a method call doesn't c=
ause the warning.


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

* Author: sos4nt (Stefan Sch=FC=DFler)
* Status: Rejected
* Priority: Normal
* Assignee: =

* Target version: =

----------------------------------------
Sometime it's convenient to have an assignment in an `if`-condition. The Ru=
by 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#lab=
el-if+Expression
```ruby
if a =3D object.some_value
  # do something to a
end
```

Running that code however results in a warning:

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

It's very unfortunate that the Ruby docs contain example code that the pars=
er complains about. And it's unfortunate that we can't make use of conditio=
nal 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 =3D object.some_value
  # warning
end

if (a =3D 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=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>