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

Lint/ToJSON suggest wrong code #6858

Closed
mrasu opened this issue Mar 22, 2019 · 1 comment
Closed

Lint/ToJSON suggest wrong code #6858

mrasu opened this issue Mar 22, 2019 · 1 comment
Labels

Comments

@mrasu
Copy link

mrasu commented Mar 22, 2019

Expected behavior

With following suggestion at Lint/ToJSON, not raise error.
https://github.com/rubocop-hq/rubocop/blob/master/lib/rubocop/cop/lint/to_json.rb#L17

@example
  # bad
  def to_json
  end

  # good
  def to_json(_opts)
  end

Actual behavior

Because I get Lint/ToJSON, I changed my code like below

# Before
class Ex
  def to_json
    {a: 1}.to_json
  end
end

# After
class Ex
  def to_json(_opts)
    {a: 1}.to_json
  end
end

Then, Ex.new.to_json start raising error saying ArgumentError: wrong number of arguments (given 0, expected 1)

As talked #6379 (comment), document should suggest the argument to be optional

RuboCop version

$ bundle exec rubocop -V
0.66.0 (using Parser 2.6.0.0, running on ruby 2.4.1 x86_64-linux)
@koic
Copy link
Member

koic commented Mar 22, 2019

I think it was a mistake of multiple arguments. I opened a PR #6859.

koic added a commit to koic/rubocop that referenced this issue Mar 23, 2019
Fixes rubocop#6858.

I think it was a mistake of multiple arguments.

## Before

```ruby
def to_json(_opts)
end
```

## After

```ruby
def to_json(*_args)
end
```
koic added a commit that referenced this issue Mar 24, 2019
…_to_json

[Fix #6858] Fix an incorrect auto-correct for `Lint/ToJSON`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants