Warn about re-assigned but not re-used variables #458

Closed
agrimm opened this Issue Aug 29, 2013 · 3 comments

Comments

Projects
None yet
3 participants
Contributor

agrimm commented Aug 29, 2013

The following code

def reassigned_unused
  a = 42
  b = a.to_s
  a = 56
  b
end

Should be regarded as problematic, because of the second assignment to a which isn't used. It'd be nice if rubocop could generate a warning about it.

I proposed the following as an enhancement for MRI, but it hasn't attracted any attention yet: https://bugs.ruby-lang.org/issues/8691 . The Rubinius team isn't planning on adding this kind of thing to Rubinius itself, based on rubinius/rubinius#2054. The JRuby team isn't enthusiastic about this kind of thing, based on https://jira.codehaus.org/browse/JRUBY-6905

Owner

bbatsov commented Aug 29, 2013

I think that such a check makes sense indeed. @yujinakayama Would you look into it?

Collaborator

yujinakayama commented Aug 29, 2013

👍

Actually I wondered why MRI does not warn about it when I was implementing UnusedLocalVariable. However I was not sure if warning of it is valid at that time, so I've made the cop mimic MRI. But yes, I'll try.

yujinakayama was assigned Sep 3, 2013

bbatsov closed this in #469 Sep 9, 2013

Contributor

agrimm commented Sep 9, 2013

Thanks for all the work you put into doing this!

It's detected several useless assignments in my application, so it's proved to be useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment