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/MultilineWhenThen autocorrect generates invalid Ruby for certain case statements #7434
Labels
Comments
8 tasks
koic
added a commit
to koic/rubocop
that referenced
this issue
Oct 21, 2019
…henThen` Fixes rubocop#7434. This PR fixes an incorrect autocorrect for `Style/MultilineWhenThen` when the body of `when` branch starts with `then`. The following is a reproduction procedure. ```console % cat example.rb # frozen_string_literal: true case foo when bar then do_something end ``` ``` % ruby -c example.rb Syntax OK ``` ## Before ```console % rubocop example.rb Inspecting 1 file C Offenses: example.rb:5:3: C: Style/MultilineWhenThen: Do not use then for multiline when statement. then do_something ^^^^ ``` It is changed to the code with syntax error as follows. ```ruby # frozen_string_literal: true case foo when bar do_something end ``` ```console % ruby -c example.rb example.rb:4: syntax error, unexpected tIDENTIFIER, expecting do or '{' or '(' when bar do_something ``` ## After ```console % rubocop example.rb -a Inspecting 1 file C Offenses: example.rb:5:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 1) spaces for indentation. do_something ^ example.rb:5:3: C: [Corrected] Style/MultilineWhenThen: Do not use then for multiline when statement. then do_something ^^^^ 1 file inspected, 2 offenses detected, 2 offenses corrected ``` It will be changed to valid Ruby code as follows. ```ruby # frozen_string_literal: true case foo when bar do_something end ``` There is a slight difference in indentation using only this cop. That indentation is corrected using `Layout/IndentationWidth` cop. ```diff case foo when bar - do_something + do_something end ```
bbatsov
pushed a commit
that referenced
this issue
Oct 21, 2019
Fixes #7434. This PR fixes an incorrect autocorrect for `Style/MultilineWhenThen` when the body of `when` branch starts with `then`. The following is a reproduction procedure. ```console % cat example.rb # frozen_string_literal: true case foo when bar then do_something end ``` ``` % ruby -c example.rb Syntax OK ``` ## Before ```console % rubocop example.rb Inspecting 1 file C Offenses: example.rb:5:3: C: Style/MultilineWhenThen: Do not use then for multiline when statement. then do_something ^^^^ ``` It is changed to the code with syntax error as follows. ```ruby # frozen_string_literal: true case foo when bar do_something end ``` ```console % ruby -c example.rb example.rb:4: syntax error, unexpected tIDENTIFIER, expecting do or '{' or '(' when bar do_something ``` ## After ```console % rubocop example.rb -a Inspecting 1 file C Offenses: example.rb:5:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 1) spaces for indentation. do_something ^ example.rb:5:3: C: [Corrected] Style/MultilineWhenThen: Do not use then for multiline when statement. then do_something ^^^^ 1 file inspected, 2 offenses detected, 2 offenses corrected ``` It will be changed to valid Ruby code as follows. ```ruby # frozen_string_literal: true case foo when bar do_something end ``` There is a slight difference in indentation using only this cop. That indentation is corrected using `Layout/IndentationWidth` cop. ```diff case foo when bar - do_something + do_something end ```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The autocorrect for
Style/MultilineWhenThen
generates invalid ruby when correcting a case statement with the following format (then
on the line after thewhen
):Definitely not a standard way to write a case statement but it is valid Ruby.
Full Example code:
Expected behavior
I would expect autocorrect to just remove the unnecessary
then
like so:Actual behavior
Instead, what we get is invalid ruby:
Steps to reproduce the problem
The sample code provided above can reproduce the problem.
RuboCop version
The text was updated successfully, but these errors were encountered: