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

Style/EmptyLiteral is (possibly) not safe #6648

Closed
deadeyejoe opened this issue Jan 9, 2019 · 2 comments · Fixed by #6680
Closed

Style/EmptyLiteral is (possibly) not safe #6648

deadeyejoe opened this issue Jan 9, 2019 · 2 comments · Fixed by #6680
Labels

Comments

@deadeyejoe
Copy link

I've encountered a situation where this cop is possibly not safe.

I have a class with subclasses. The constructor for the parent class accepts a hash, and I want this to be an empty hash in one of the child classes.

class Parent
  def initialize(hash)
    @data = hash
  end
end

class Child < Parent
  def initialize(string)
    super Hash.new
    @string = string
  end
end

When I autocorrect the child class it gets changed to:

class Child < Parent
  def initialize(string)
    super {}
    @string = string
  end
end

Which is not equivalent. This results in the argument string being passed to the super call (i.e. it behaves as a 'bare' super call). Changing this line to super({}) causes it to work as expected.


$ [bundle exec] rubocop -V
0.62.0 (using Parser 2.5.1.2, running on ruby 2.5.1 x86_64-linux)
@pocke pocke added the bug label Jan 10, 2019
@rrosenblum
Copy link
Contributor

This likely applies to any method, not just super, that is accepting Hash.new as an argument.

@rrosenblum
Copy link
Contributor

Other methods are a non issue. They were already accounted for. super was missed because it is considered its own type rather than a send type.

rrosenblum added a commit to rrosenblum/rubocop that referenced this issue Jan 16, 2019
…correcting Hash.new is the fist argument to super
Drenmi added a commit that referenced this issue Jan 22, 2019
[Fix #6648] EmptyLiteral - wrap arguments in parentheses when correcting Hash.new is the fist argument to super
@Drenmi Drenmi mentioned this issue Feb 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants