https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112017-09-06T15:43:57ZRuby Issue Tracking SystemRuby master - Bug #13872: Duplicate assignment no longer silences "assigned but unused variable" warninghttps://bugs.ruby-lang.org/issues/13872?journal_id=665142017-09-06T15:43:57Zted (Ted Johansson)drenmi@gmail.com
<ul></ul><p>Previously, <code>foo = foo = "bar"</code> was used as a workaround. Now that the workaround is "fixed", I see incoming PRs changing the workaround to <code>foo = "bar"; foo = foo</code>.</p>
<p>It would be nice if we had a way to handle these false positives directly, without having to resort to tricks. :-)</p>
<p>segiddins (Samuel Giddins) wrote:</p>
<blockquote>
<p>On ruby 2.4.1:</p>
<p><code>ruby -W -e 'def a; var = var = "foo"; end'</code></p>
<p>prints no warning</p>
<p>On ruby 2.5.0dev (2017-09-04 trunk 59742) [x86_64-darwin16]</p>
<p><code>ruby -W -e 'def a; var = var = "foo"; end'</code></p>
<p>prints warning <code>-e:1: warning: assigned but unused variable - var</code></p>
<p>This feature was useful when the local binding would be passed to another scope (like to ERB), and those variables were only accessed via the binding</p>
</blockquote> Ruby master - Bug #13872: Duplicate assignment no longer silences "assigned but unused variable" warninghttps://bugs.ruby-lang.org/issues/13872?journal_id=665152017-09-06T18:29:54Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Well, the first could be solved if there is some way to also propagate (local) variables towards ERB<br>
or other bindings. Like to have different binding "levels" available. Copy environments and stuff. :D<br>
(Actually ... one could probably store stuff in ENV and have them then propagated? But admittedly that<br>
is also another work around towards anyone just wanting to use local variables instead).</p>
<p>But I think that, to the topic at hand, there may also be cases where people may not have wanted<br>
to do such assignments; could happen due to some quick copy/paste or so. So for these cases, I<br>
think that the 2.5.x behaviour is better, though it also does not seem to distinguish between<br>
same_name_variable = same_name_variable = 'something else' - the "did you mean" gem could also<br>
ask a question such as "did you really mean to give the variable the same name again"? :-)</p>
<p>The "workaround" was probably never intended to be used or be meaningful; at the least I can<br>
not think that matz would think that the above was ... well, revealing the intent.<br>
(self-assignment of variables? By the way, I did not know this workaround), but I agree that<br>
some people may have liked that workaround. Perhaps ruby core may consider if you could add<br>
some alternative proposal without the double assignment?</p>
<p>I mean, if the major use case is in regards to bindings and local variables then perhaps some<br>
other way to use/propagate variables like that could convince matz / the core team?</p> Ruby master - Bug #13872: Duplicate assignment no longer silences "assigned but unused variable" warninghttps://bugs.ruby-lang.org/issues/13872?journal_id=672632017-10-16T02:23:15Zmatsuda (Akira Matsuda)ronnie@dio.jp
<ul></ul><p>This is not a bug. The warning you see is a result of an improvement of "unused variable" detection at r59585.<br>
Now it simply checks whether each variable is actually "used" or not. Please also take a look at <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Unused variable warning does not occur when a method with the same name was called (Closed)" href="https://bugs.ruby-lang.org/issues/13809">#13809</a> to see what was solved here.<br>
<code>var = var = "foo"</code> warns now because <code>var = var = "foo"</code> doesn't really "use" the variable <code>var</code>.</p>
<p>Please update your code to actually "use" all the assigned local variables, in this case, to <code>var = "foo"; var = var</code>.</p> Ruby master - Bug #13872: Duplicate assignment no longer silences "assigned but unused variable" warninghttps://bugs.ruby-lang.org/issues/13872?journal_id=672652017-10-16T04:08:32Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:ronnie@dio.jp" class="email">ronnie@dio.jp</a> wrote:</p>
<blockquote>
<p>Please upgrade your code to actually "use" all the assigned<br>
local variables, in this case, to <code>var = "foo"; var = var</code>.</p>
</blockquote>
<p>The problem is with binding:</p>
<pre><code>require 'erb'
foo = :bar
puts ERB.new('<%= foo %>').result(binding)
</code></pre>
<p>The above uses foo, but binding isn't handled by the parser.<br>
Maybe the parser should not warn if the word <code>binding' appears in scope? And check for local variables named </code>binding'...</p>
<p>But yes, this is annoying with ERB when I want my code<br>
to be warning-clean.</p> Ruby master - Bug #13872: Duplicate assignment no longer silences "assigned but unused variable" warninghttps://bugs.ruby-lang.org/issues/13872?journal_id=672742017-10-17T00:16:24Zmatsuda (Akira Matsuda)ronnie@dio.jp
<ul></ul><p>Yes, in my understanding that is exactly the case we use this idiom for, usually when the contents of ERB is user-given and so it's uncertain if the local variable 'foo' will be used there or not.</p>
<pre><code class="diff syntaxhl" data-language="diff"> require 'erb'
<span class="gd">-foo = :bar
</span><span class="gi">+foo = :bar; foo = foo
</span> puts ERB.new('<%= foo %>').result(binding)
</code></pre> Ruby master - Bug #13872: Duplicate assignment no longer silences "assigned but unused variable" warninghttps://bugs.ruby-lang.org/issues/13872?journal_id=672762017-10-17T09:30:03Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>There could be:</p>
<ol>
<li>add assignments in user code</li>
<li>enable duplicate assignment as a special case</li>
<li>add another way to suppress warnings, e.g. magic comment</li>
<li>etc</li>
</ol> Ruby master - Bug #13872: Duplicate assignment no longer silences "assigned but unused variable" warninghttps://bugs.ruby-lang.org/issues/13872?journal_id=674922017-10-22T01:37:47Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r60339.</p>
<hr>
<p>parse.y: workaround for warnings</p>
<ul>
<li>parse.y (mark_lvar_used): enable workaround to suppress unused<br>
local variables. <a href="/issues/13872">[ruby-core:82656]</a> [Bug <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Duplicate assignment no longer silences "assigned but unused variable" warning (Closed)" href="https://bugs.ruby-lang.org/issues/13872">#13872</a>]</li>
</ul>