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

Error converting if-then-else-end oneliner #11862

Closed
maagical opened this issue May 9, 2023 · 0 comments · Fixed by #11863
Closed

Error converting if-then-else-end oneliner #11862

maagical opened this issue May 9, 2023 · 0 comments · Fixed by #11863
Labels

Comments

@maagical
Copy link

maagical commented May 9, 2023

When using the RuboCop linter to autocorrect a one-liner if-then-else-end construct with the --autocorrect option, the resulting code is incorrect. The expected behavior is that the one-liner should be converted to a multi-line construct before the autocorrect is applied, resulting in the correct code.

Expected behavior

When running RuboCop with the --autocorrect option on the one-liner

if matches.size == 1 then matches.first else raise Exception.new :parse_error end

the expected output should be:
with rubocop -A:

raise StandardError, :parse_error unless matches.size == 1
matches.first

or (with only rubocop --autocorrect):

raise Exception, :parse_error unless matches.size == 1
matches.first

Actual behavior

The output generated by RuboCop with the --autocorrect option is incorrect, resulting in the code:

raise Exception.new :parse_error unless matches.size == 1 then matches.first

This code generates a syntax error when run on Rails 7:

./app/models/test.rb:12: syntax error, unexpected `then', expecting `end' or dummy end
...r unless matches.size == 1 then matches.first
... ^~~~

Full Code:

$ rubocop --debug --autocorrect test.rb 
For /var/home/user: Default configuration from /var/home/user/.rvm/gems/ruby-3.2.2/gems/rubocop-1.50.2/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /var/home/user/test.rb
Loading cache from /var/home/user/.cache/rubocop_cache/da75a89333f8c1bb0515b8e36c92928c3466edbc/6d7a3b621ca1730e04accd938619e4bdab66cfb1/8eac1b4809748bba093f0ace295341f33f387ef5
F

Offenses:

test.rb:1:1: C: [Corrected] Style/GuardClause: Use a guard clause (raise Exception.new :parse_error unless matches.size == 1) instead of wrapping the code inside a conditional expression.
if matches.size == 1 then matches.first else raise Exception.new :parse_error end 
^^
test.rb:1:1: C: [Corrected] Style/OneLineConditional: Favor the ternary operator (?:) or multi-line constructs over single-line if/then/else/end constructs.
if matches.size == 1 then matches.first else raise Exception.new :parse_error end 
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:1:46: C: [Corrected] Style/RaiseArgs: Provide an exception class and message as arguments to raise.
if matches.size == 1 then matches.first else raise Exception.new :parse_error end 
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test.rb:1:59: F: Lint/Syntax: unexpected token kTHEN
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
raise Exception.new :parse_error unless matches.size == 1 then matches.first   
                                                          ^^^^
test.rb:1:82: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
if matches.size == 1 then matches.first else raise Exception.new :parse_error end 
                                                                                 ^

1 file inspected, 5 offenses detected, 4 offenses corrected
Finished in 0.21446587999889744 seconds

Steps to reproduce the problem

Create a file test.rb with the following one-liner:

if matches.size == 1 then matches.first else raise Exception.new :parse_error end

Run the RuboCop linter with the --autocorrect option on the test.rb file:

$ rubocop --autocorrect test.rb

The resulting code generated by RuboCop is:

raise Exception.new :parse_error unless matches.size == 1 then matches.first

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

$ rubocop -V
1.50.2 (using Parser 3.2.2.1, rubocop-ast 1.28.1, running on ruby 3.2.2) [x86_64-linux]
@koic koic added the bug label May 9, 2023
koic added a commit to koic/rubocop that referenced this issue May 9, 2023
Fixes rubocop#11862.

This PR fixes an incorrect autocorrect for `Style/GuardClause`
when using `raise` in `else` branch in a one-liner with `then`.
koic added a commit that referenced this issue May 9, 2023
…style_guard_clause

[Fix #11862] Fix an incorrect autocorrect for `Style/GuardClause`
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