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

Fix an incorrect auto-correct for Style/SingleLineMethods #9909

Commits on Jul 3, 2021

  1. Fix an incorrect auto-correct for Style/SingleLineMethods

    This PR fixes an incorrect auto-correct for `Style/SingleLineMethods`
    when using `return`, `break`, or `next` for one line method body in Ruby 3.0.
    
    These are not supported in the endless method definition.
    
    ```console
    % ruby -v
    ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19]
    
    % cat example.rb
    def foo(argument) break bar(argument); end
    def foo(argument) next bar(argument); end
    def foo(argument) return bar(argument); end
    
    % ruby -c example.rb
    Syntax OK
    
    % bundle exec rubocop --only Style/SingleLineMethods -a
    (snip)
    
    Inspecting 1 file
    C
    
    Offenses:
    
    example.rb:1:1: C: [Corrected] Style/SingleLineMethods: Avoid
    single-line method definitions.
    def foo(argument) break bar(argument); end
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    example.rb:2:1: C: [Corrected] Style/SingleLineMethods: Avoid
    single-line method definitions.
    def foo(argument) next bar(argument); end
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    example.rb:3:1: C: [Corrected] Style/SingleLineMethods: Avoid
    single-line method definitions.
    def foo(argument) return bar(argument); end
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    1 file inspected, 3 offenses detected, 3 offenses corrected
    
    % cat example.rb
    def foo(argument) = break bar(argument)
    def foo(argument) = next bar(argument)
    def foo(argument) = return bar(argument)
    
    % ruby -ce 'def foo(argument) = break bar(argument)'
    -e:1: syntax error, unexpected local variable or method, expecting
    end-of-input
    def foo(argument) = break bar(argument)
    
    % ruby -ce 'def foo(argument) = next bar(argument)'
    -e:1: syntax error, unexpected local variable or method, expecting
    end-of-input
    def foo(argument) = next bar(argument)
    
    % ruby -ce 'def foo(argument) = return bar(argument)'
    -e:1: syntax error, unexpected local variable or method, expecting
    end-of-input
    def foo(argument) = return bar(argument)
    ```
    koic committed Jul 3, 2021
    Copy the full SHA
    d86070d View commit details
    Browse the repository at this point in the history