You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 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:
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
the expected output should be:
with rubocop -A:
or (with only rubocop --autocorrect):
Actual behavior
The output generated by RuboCop with the --autocorrect option is incorrect, resulting in the code:
This code generates a syntax error when run on Rails 7:
Full Code:
Steps to reproduce the problem
Create a file test.rb with the following one-liner:
Run the RuboCop linter with the --autocorrect option on the test.rb file:
The resulting code generated by RuboCop is:
RuboCop version
Include the output of
rubocop -V
orbundle 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:The text was updated successfully, but these errors were encountered: