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

-a --safe --only "Style/OrAssignment" introduces a syntax error #7712

Closed
samjewell opened this issue Feb 13, 2020 · 0 comments · Fixed by #7720
Closed

-a --safe --only "Style/OrAssignment" introduces a syntax error #7712

samjewell opened this issue Feb 13, 2020 · 0 comments · Fixed by #7720
Labels

Comments

@samjewell
Copy link

samjewell commented Feb 13, 2020

Expected behavior

Using the --safe flag should mean syntax errors aren't introduced into code.

Actual behavior

Using rubocop -a --safe --only "Style/OrAssignment" introduces a syntax error

Steps to reproduce the problem

$ cat mock_logger.rb
msg =
  if msg
    msg
  elsif block_given?
    yield
  else
    progname
  end
logged_messages[severity] << msg
msg
$ rubocop -a --safe --only "Style/OrAssignment" mock_logger.rb
Inspecting 1 file
E

Offenses:

mock_logger.rb:1:1: C: [Corrected] Style/OrAssignment: Use the double pipe equals operator ||= instead.
msg = ...
^^^^^
mock_logger.rb:1:9: E: Lint/Syntax: unexpected token kELSIF
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
msg ||= elsif block_given?
        ^^^^^
mock_logger.rb:3:3: E: Lint/Syntax: unexpected token kELSE
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
  else
  ^^^^

1 file inspected, 3 offenses detected, 1 offense corrected

RuboCop version

$ rubocop -V
0.79.0 (using Parser 2.7.0.2, running on ruby 2.7.0 x86_64-darwin18)
@koic koic added the bug label Feb 15, 2020
koic added a commit to koic/rubocop that referenced this issue Feb 17, 2020
Fixes rubocop#7712.

This PR fixes an incorrect autocorrect for `Style/OrAssignment`
when using `elsif` branch.

The following is a reproduction procedure.

```ruby
% cat example.rb
foo = if foo
        foo
      elsif
        bar
      else
        'default'
      end
```

It is changed to the code with syntax error as follows.

```console
% bundle exec rubocop -a --only Style/OrAssignment
(snip)

% cat example.rb
foo ||= elsif
          bar
        else
         'default'

% ruby -c example.rb
example.rb:1: syntax error, unexpected `elsif'
foo ||= elsif
```

The or assignment operator replaced when using `elsif` branch is complicated.
The PR will accept the above case without offense.
bbatsov pushed a commit that referenced this issue Feb 17, 2020
Fixes #7712.

This PR fixes an incorrect autocorrect for `Style/OrAssignment`
when using `elsif` branch.

The following is a reproduction procedure.

```ruby
% cat example.rb
foo = if foo
        foo
      elsif
        bar
      else
        'default'
      end
```

It is changed to the code with syntax error as follows.

```console
% bundle exec rubocop -a --only Style/OrAssignment
(snip)

% cat example.rb
foo ||= elsif
          bar
        else
         'default'

% ruby -c example.rb
example.rb:1: syntax error, unexpected `elsif'
foo ||= elsif
```

The or assignment operator replaced when using `elsif` branch is complicated.
The PR will accept the above case without offense.
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