From: mail@... Date: 2019-05-14T10:35:52+00:00 Subject: [ruby-core:92646] [Ruby trunk Feature#15848] Silence warning when conditional assignments are wrapped in parentheses Issue #15848 has been updated by sos4nt (Stefan Sch��ler). shevegen (Robert A. Heiler) wrote: > I can't think of a simple way to controls warnings though :( - I guess if we may have some > simple way, somewhat similar to frozen-string literals true/false, that could solve quite > a bit in this regard. A per-file setting is too broad. Given: ```ruby if (a = foo) do_this if a == x do_that if a = y end ``` I wanted Ruby to still warn me on the `a = y` part. ---------------------------------------- Feature #15848: Silence warning when conditional assignments are wrapped in parentheses https://bugs.ruby-lang.org/issues/15848#change-78009 * 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: