Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for useless square bracket assignment #639

Closed
agrimm opened this issue Nov 20, 2013 · 7 comments · Fixed by #1156
Closed

Check for useless square bracket assignment #639

agrimm opened this issue Nov 20, 2013 · 7 comments · Fixed by #1156
Assignees

Comments

@agrimm
Copy link
Contributor

agrimm commented Nov 20, 2013

The following feature request is like #383 , but rather than concerning foo.bar = baz, which is covered by UselessSetterCall, it's about foo[bar] = baz.

Using []= on the last line of a method on a local variable is usually a mistake.

# Bad - returns 'o', and the string referred to by the variable foo is lost without doing anything
def create_string_bad
  foo = 'String'
  foo[2..2] = 'o'
end

# Good - returns the full string
def create_string_good
  foo = 'String'
  foo.tap do |foo|
    foo[2..2] = 'o'
  end
end

# Good - no objects created and lost
def modify_string
  @foo[2..2] = 'o'
end
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 4, 2013

Sounds reasonable. I'll look into that.

@fuadsaud
Copy link

Just a comment: rubocop also raises an offence for something like this:

class Doge
  attr_writer :wow

  def initialize(*)
    super

    # whatever
    wow = 'foo'
  end
end

It identifies that method call as a local variable assignment. I didn't find any closed issues about this topic, should a open a new one?

@agrimm
Copy link
Contributor Author

agrimm commented Dec 19, 2013

@fuadsaud that is a local variable assignment, just as rubocop says. You should have done self.wow = 'foo'.

If you have

# Incorrect
class Doge
  attr_writer :wow

  def initialize(*)
    super

    # whatever
    wow = 'foo'
  end
end

doge = Doge.new
puts doge.inspect

class Correct
  attr_writer :wow

  def initialize(*)
    super

    # whatever
    self.wow = 'foo'
  end
end

correct = Correct.new
puts correct.inspect

Then you'll get

#<Doge:0x007f7bfc5b8720>
#<Correct:0x007f7bfc5b8540 @wow="foo">

@fuadsaud
Copy link

@agrimm very correct. It was my bad, I didn't know self was required in a case like this. Thanks.

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 12, 2014

@yujinakayama @jonas054 What do you think about this? Are we doing it or closing it?

@yujinakayama
Copy link
Collaborator

Though I haven't looked into this, maybe this can be done by extending UselessSetterCall?

@bbatsov
Copy link
Collaborator

bbatsov commented Jun 16, 2014

@yujinakayama That's what I thought as well. Would you, please, look into this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants