diff --git a/changelog/fix_incorrect_autocorrect_for_style_single_line_methods.md b/changelog/fix_incorrect_autocorrect_for_style_single_line_methods.md new file mode 100644 index 00000000000..33eefffa1a8 --- /dev/null +++ b/changelog/fix_incorrect_autocorrect_for_style_single_line_methods.md @@ -0,0 +1 @@ +* [#9909](https://github.com/rubocop/rubocop/pull/9909): 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. ([@koic][]) diff --git a/lib/rubocop/cop/style/single_line_methods.rb b/lib/rubocop/cop/style/single_line_methods.rb index 20e20fbb0c9..098dd7c4d7f 100644 --- a/lib/rubocop/cop/style/single_line_methods.rb +++ b/lib/rubocop/cop/style/single_line_methods.rb @@ -36,6 +36,7 @@ class SingleLineMethods < Base extend AutoCorrector MSG = 'Avoid single-line method definitions.' + NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES = %i[return break next].freeze def on_def(node) return unless node.single_line? @@ -62,13 +63,10 @@ def allow_empty? def correct_to_endless?(body_node) return false if target_ruby_version < 3.0 - - endless_method_config = config.for_cop('Style/EndlessMethod') - - return false unless endless_method_config['Enabled'] - return false if endless_method_config['EnforcedStyle'] == 'disallow' + return false if disallow_endless_method_style? return false unless body_node - return false if body_node.parent.assignment_method? + return false if body_node.parent.assignment_method? || + NOT_SUPPORTED_ENDLESS_METHOD_BODY_TYPES.include?(body_node.type) !(body_node.begin_type? || body_node.kwbegin_type?) end @@ -129,6 +127,13 @@ def method_body_source(method_body) def require_parentheses?(method_body) method_body.send_type? && !method_body.arguments.empty? && !method_body.comparison_method? end + + def disallow_endless_method_style? + endless_method_config = config.for_cop('Style/EndlessMethod') + return false unless endless_method_config['Enabled'] + + endless_method_config['EnforcedStyle'] == 'disallow' + end end end end diff --git a/spec/rubocop/cop/style/single_line_methods_spec.rb b/spec/rubocop/cop/style/single_line_methods_spec.rb index 61d954132c5..96b4c57d7aa 100644 --- a/spec/rubocop/cop/style/single_line_methods_spec.rb +++ b/spec/rubocop/cop/style/single_line_methods_spec.rb @@ -219,6 +219,30 @@ def #{op}(other) = self #{op} other end end + it 'does not to an endless class method definition when using `return`' do + expect_correction(<<~RUBY.strip, source: 'def foo(argument) return bar(argument); end') + def foo(argument)#{trailing_whitespace} + return bar(argument);#{trailing_whitespace} + end + RUBY + end + + it 'does not to an endless class method definition when using `break`' do + expect_correction(<<~RUBY.strip, source: 'def foo(argument) break bar(argument); end') + def foo(argument)#{trailing_whitespace} + break bar(argument);#{trailing_whitespace} + end + RUBY + end + + it 'does not to an endless class method definition when using `next`' do + expect_correction(<<~RUBY.strip, source: 'def foo(argument) next bar(argument); end') + def foo(argument)#{trailing_whitespace} + next bar(argument);#{trailing_whitespace} + end + RUBY + end + # NOTE: Setter method cannot be defined in the endless method definition. it 'corrects to multiline method definition when defining setter method' do expect_correction(<<~RUBY.chop, source: 'def foo=(foo) @foo = foo end')