From b1778dd5e6830f3ef758aaae26b71ee06f274c55 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 18 Feb 2022 02:00:32 +0900 Subject: [PATCH] [Fix #10421] Make `Style/DefWithParentheses` aware of endless method definition Fixes #10421. This PR makes `Style/DefWithParentheses` aware of endless method definition. `def foo() do_something end` will continue to allow because `def foo do_something end` is a syntax error. On the other hand, be aware that the endless method definition's parentheses can be omitted. --- ...eses_aware_of_endless_method_definition.md | 1 + lib/rubocop/cop/style/def_with_parentheses.rb | 20 ++++++++++++------- .../cop/style/def_with_parentheses_spec.rb | 13 ++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 changelog/fix_make_style_def_with_parentheses_aware_of_endless_method_definition.md diff --git a/changelog/fix_make_style_def_with_parentheses_aware_of_endless_method_definition.md b/changelog/fix_make_style_def_with_parentheses_aware_of_endless_method_definition.md new file mode 100644 index 00000000000..51cdef326a3 --- /dev/null +++ b/changelog/fix_make_style_def_with_parentheses_aware_of_endless_method_definition.md @@ -0,0 +1 @@ +* [#10421](https://github.com/rubocop/rubocop/issues/10421): Make `Style/DefWithParentheses` aware of endless method definition. ([@koic][]) diff --git a/lib/rubocop/cop/style/def_with_parentheses.rb b/lib/rubocop/cop/style/def_with_parentheses.rb index 7a3c43f4830..0f502b63cd3 100644 --- a/lib/rubocop/cop/style/def_with_parentheses.rb +++ b/lib/rubocop/cop/style/def_with_parentheses.rb @@ -11,27 +11,33 @@ module Style # # # bad # def foo() - # # does a thing + # do_something # end # # # good # def foo - # # does a thing + # do_something # end # - # # also good - # def foo() does_a_thing end + # # bad + # def foo() = do_something + # + # # good + # def foo = do_something + # + # # good (without parentheses it's a syntax error) + # def foo() do_something end # # @example # # # bad # def Baz.foo() - # # does a thing + # do_something # end # # # good # def Baz.foo - # # does a thing + # do_something # end class DefWithParentheses < Base extend AutoCorrector @@ -39,7 +45,7 @@ class DefWithParentheses < Base MSG = "Omit the parentheses in defs when the method doesn't accept any arguments." def on_def(node) - return if node.single_line? + return if node.single_line? && !node.endless? return unless !node.arguments? && (node_arguments = node.arguments.source_range) add_offense(node_arguments) do |corrector| diff --git a/spec/rubocop/cop/style/def_with_parentheses_spec.rb b/spec/rubocop/cop/style/def_with_parentheses_spec.rb index 52ffe16bbc0..51ae86d61d5 100644 --- a/spec/rubocop/cop/style/def_with_parentheses_spec.rb +++ b/spec/rubocop/cop/style/def_with_parentheses_spec.rb @@ -29,6 +29,19 @@ def Test.func RUBY end + context 'Ruby >= 3.0', :ruby30 do + it 'reports an offense for endless method definition with empty parens' do + expect_offense(<<~RUBY) + def foo() = do_something + ^^ Omit the parentheses in defs when the method doesn't accept any arguments. + RUBY + + expect_correction(<<~RUBY) + def foo = do_something + RUBY + end + end + it 'accepts def with arg and parens' do expect_no_offenses(<<~RUBY) def func(a)