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/RedundantRequireStatement breaks code with modifiers #11120

Closed
tsugimoto opened this issue Oct 25, 2022 · 0 comments · Fixed by #11121
Closed

Lint/RedundantRequireStatement breaks code with modifiers #11120

tsugimoto opened this issue Oct 25, 2022 · 0 comments · Fixed by #11121
Labels

Comments

@tsugimoto
Copy link
Contributor

require 'pp' unless respond_to?(:pretty_inspect)

Autocorrect breaks code when require is followed by postfix unless.


Expected behavior

Autocorrects into valid Ruby code, possibly like below (considering the modifier condition may have side effects.)

respond_to?(:pretty_inspect)

Actual behavior

Offenses:

main.rb:1:1: W: [Correctable] Lint/RedundantRequireStatement: Remove unnecessary require statement.
require 'pp' unless respond_to?(:pretty_inspect)
^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense autocorrectable

effectively autocorrects code into

unless respond_to?(:pretty_inspect)

, which is syntactically incorrect.

Steps to reproduce the problem

$ cat main.rb
require 'pp' unless respond_to?(:pretty_inspect)
$ rubocop --only Lint/RedundantRequireStatement

RuboCop version

$ rubocop -V
1.37.1 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.1) [x86_64-darwin20]
$ 
@koic koic added the bug label Oct 25, 2022
koic added a commit to koic/rubocop that referenced this issue Oct 28, 2022
…equireStatement`

Fixes rubocop#11120.

This PR fixes an incorrect autocorrect for `Lint/RedundantRequireStatement`
when using redundant `require` with modifier form.

For example, in essential it is unknown how many times `do_something` will be
executed when `require 'enumerator' while do_something`.
So it does not autocorrects when modifier form is used to avoid side effects.
bbatsov pushed a commit that referenced this issue Oct 30, 2022
…tatement`

Fixes #11120.

This PR fixes an incorrect autocorrect for `Lint/RedundantRequireStatement`
when using redundant `require` with modifier form.

For example, in essential it is unknown how many times `do_something` will be
executed when `require 'enumerator' while do_something`.
So it does not autocorrects when modifier form is used to avoid side effects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants