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

Standard "corrects"return *array to something unparseable #277

Closed
myronmarston opened this issue Mar 22, 2021 · 2 comments · Fixed by rubocop/rubocop#9631
Closed

Standard "corrects"return *array to something unparseable #277

myronmarston opened this issue Mar 22, 2021 · 2 comments · Fixed by rubocop/rubocop#9631
Labels
rubocop bug 🚨 An apparent bug in RuboCop

Comments

@myronmarston
Copy link

Given this issue.rb source file:

class MyClass
  def something
    data = [1, 2, 3]
    return *data
  end
end

...standardrb reports an issue:

$ standardrb issue.rb                                                                                                                                                                                                                                                                                   
Inspecting 1 file
C

Offenses:

issue.rb:4:5: C: [Correctable] Style/RedundantReturn: Redundant return detected.
    return *data
    ^^^^^^

1 file inspected, 1 offense detected, 1 offense auto-correctable

When running it with --fix, it "corrects" it by removing the return but leaving the *, rendering it unparseable:

$ standardrb issue.rb --fix                                                                                                                                                                                                                                                                            
Inspecting 1 file
E

Offenses:

issue.rb:4:5: C: [Corrected] Style/RedundantReturn: Redundant return detected.
    return *data
    ^^^^^^
issue.rb:4:10: E: Lint/Syntax: unexpected token tNL
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
    *data ...


1 file inspected, 2 offenses detected, 1 offense corrected
$ ruby issue.rb                                                                                                                                                                                                                                                                                        
issue.rb:4: syntax error, unexpected '\n', expecting '='
$ standardrb issue.rb                                                                                                                                                                                                                                                                                  
Inspecting 1 file
E

Offenses:

issue.rb:4:10: E: Lint/Syntax: unexpected token tNL
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
    *data ...


1 file inspected, 1 offense detected

Here's the final version of issue.rb after the "fix" was applied:

class MyClass
  def something
    data = [1, 2, 3]
    *data
  end
end

When return *array is used I think standardb --fix should strip both the return and the *.

@koic
Copy link
Contributor

koic commented Mar 22, 2021

Thank you for opening the issue. This is a RuboCop's bug and I opened rubocop/rubocop#9631 to RuboCop core.

@jmkoni jmkoni added the rubocop bug 🚨 An apparent bug in RuboCop label Mar 22, 2021
koic added a commit to koic/rubocop that referenced this issue Mar 22, 2021
Fixes standardrb/standard#277.

This PR fixes the following false positive for `Style/RedundantReturn`
when using `return` with splat argument.

```console
% cat example.rb
class MyClass
  def something
    data = [1, 2, 3]
    return *data
  end
end
```

## Before

Auto-corrected to invalid code.

```console
% rubocop --only Style/RedundantReturn -a
(snip)

% cat example.rb
class MyClass
  def something
    data = [1, 2, 3]
    *data
  end
end

% ruby -c example.rb
example.rb:4: syntax error, unexpected '\n', expecting '='
```

## After

Auto-corrected to valid code.

```console
% rubocop --only Style/RedundantReturn -a
(snip)

% cat example.rb
class MyClass
  def something
    data = [1, 2, 3]
    data
  end
end

% ruby -c example.rb
Syntax OK
```
@myronmarston
Copy link
Author

Thanks for the quick fix @koic!

bbatsov pushed a commit to rubocop/rubocop that referenced this issue Mar 22, 2021
Fixes standardrb/standard#277.

This PR fixes the following false positive for `Style/RedundantReturn`
when using `return` with splat argument.

```console
% cat example.rb
class MyClass
  def something
    data = [1, 2, 3]
    return *data
  end
end
```

## Before

Auto-corrected to invalid code.

```console
% rubocop --only Style/RedundantReturn -a
(snip)

% cat example.rb
class MyClass
  def something
    data = [1, 2, 3]
    *data
  end
end

% ruby -c example.rb
example.rb:4: syntax error, unexpected '\n', expecting '='
```

## After

Auto-corrected to valid code.

```console
% rubocop --only Style/RedundantReturn -a
(snip)

% cat example.rb
class MyClass
  def something
    data = [1, 2, 3]
    data
  end
end

% ruby -c example.rb
Syntax OK
```
@jmkoni jmkoni closed this as completed Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rubocop bug 🚨 An apparent bug in RuboCop
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants