Project

General

Profile

Feature #12490

Remove warning on shadowing block params

Added by soutaro (Soutaro Matsumoto) over 2 years ago. Updated about 2 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:76028]

Description

Running ruby with -w option reports warnings on shadowing block params.

$ cat -n w.rb
     1  a = [1]
     2  
     3  a.each do |a|
     4    a = 3
     5  end
     6  
     7  p a
$ ruby -cw w.rb
w.rb:3: warning: shadowing outer local variable - a
Syntax OK

I would like to propose to remove the warning.

It was introduced to ruby 1.9, a version which changes behaviors on conflicts of local vars and block param. I understand it is to tell the user the change, and let them fix if their program depends on 1.8 behavior. ruby 1.9.1 is released in 2009, almost seven years ago, and most ruby programmers today would have correct understanding of current behavior. In my opinion, the warning does not make sense now but just annoying.

  • I like giving block params shadowing name to avoid finding new good names
  • Lint tools including RuboCop can report the warning instead of ruby
remove-shadowing-warning.patch (3.62 KB) remove-shadowing-warning.patch soutaro (Soutaro Matsumoto), 06/15/2016 02:43 AM

Associated revisions

Revision 7f69d4e4
Added by mame (Yusuke Endoh) about 2 months ago

parse.y: remove "shadowing outer local variable" warning

You can now write the following without warning.

user = User.all.find {|user| cond(user) }

Fixes [Feature #12490].

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65367 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 65367
Added by mame (Yusuke Endoh) about 2 months ago

parse.y: remove "shadowing outer local variable" warning

You can now write the following without warning.

user = User.all.find {|user| cond(user) }

Fixes [Feature #12490].

Revision c9d720b8
Added by mame (Yusuke Endoh) about 2 months ago

parse.y: remove "shadowing outer local variable" warning

You can now write the following without warning.

user = User.all.find {|user| cond(user) }

Fixes [Feature #12490].
A patch from Soutaro Matsumoto matsumoto@soutaro.com.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65369 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 65369
Added by mame (Yusuke Endoh) about 2 months ago

parse.y: remove "shadowing outer local variable" warning

You can now write the following without warning.

user = User.all.find {|user| cond(user) }

Fixes [Feature #12490].
A patch from Soutaro Matsumoto matsumoto@soutaro.com.

History

#1 [ruby-core:76066] Updated by shevegen (Robert A. Heiler) over 2 years ago

I guess the argument given by Soutaro Matsumoto makes sense (if this was indeed the reason why the warning has been added).

On a belated note, perhaps in the path towards ruby 3.x, we may have a better/finer control into how ruby issues warnings
in general. In general I always run via -w, but not every warning appears to be hugely important to me. And I think there
is no fine-control over how to get notified about warnings (other than changing $VERBOSE and re-setting it again, but
this is a bit crude and it does not read extremely elegant in code when one assigns $VERBOSE = nil)

#2 [ruby-core:76809] Updated by shyouhei (Shyouhei Urabe) over 2 years ago

We looked at this issue at yesterday's developer meeting. While myself was positive to this, Matz is not. He thinks that shadowing by itself is a bad habit and should be discouraged. You need to show him a good advantage of encouraging variable shadowing to persuade him.

#3 [ruby-core:89357] Updated by knu (Akinori MUSHA) 2 months ago

I definitely agree. Here's a typical example that makes me mad when I get the warning:

user = users.find { |user| user.parent == parent }

And I just don't want to change the inner variable name to something like u or user_ just to silence the warning.

#4 [ruby-core:89358] Updated by knu (Akinori MUSHA) 2 months ago

And we've got Matz's approval for this proposal! Congratulations!

#5 Updated by mame (Yusuke Endoh) about 2 months ago

  • Status changed from Open to Closed

Applied in changeset trunk|r65367.


parse.y: remove "shadowing outer local variable" warning

You can now write the following without warning.

user = User.all.find {|user| cond(user) }

Fixes [Feature #12490].

Also available in: Atom PDF