Skip to content

Commit 7d4f115

Browse files
koicbbatsov
authored andcommitted
Fix false positives for Style/EndlessMethod
Follow-up to #13874 (comment) This PR fixes false positives for `Style/EndlessMethod` when using assignment method definitions. Setter methods cannot be defined as endless methods. ```console $ ruby -vc -e "def foo=(arg) = bar" ruby 3.4.2 (2025-02-15 revision d2930f8e7a) +PRISM [x86_64-darwin23] ruby: -e:1: syntax error found (SyntaxError) > 1 | def foo=(arg) = bar | ^~~~ invalid method name; a setter method cannot be defined in an endless method definition ```
1 parent cc6da5c commit 7d4f115

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#13910](https://github.com/rubocop/rubocop/pull/13910): Fix false positives for `Style/EndlessMethod` when using setter method definitions. ([@koic][])

lib/rubocop/cop/style/endless_method.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class EndlessMethod < Base
144144
MSG_REQUIRE_ALWAYS = 'Use endless method definitions.'
145145

146146
def on_def(node)
147+
return if node.assignment_method?
148+
147149
case style
148150
when :allow_single_line, :allow_always
149151
handle_allow_style(node)

spec/rubocop/cop/style/endless_method_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,14 @@ def my_method = x
244244
RUBY
245245
end
246246

247+
it 'does not register an offense for a single line setter method' do
248+
expect_no_offenses(<<~RUBY)
249+
def my_method=(arg)
250+
arg.foo
251+
end
252+
RUBY
253+
end
254+
247255
it 'does not register an offense when the endless version excess Metrics/MaxLineLength[Max]' do
248256
expect_no_offenses(<<~RUBY)
249257
def my_method
@@ -392,6 +400,16 @@ def my_method = x.foo
392400
RUBY
393401
end
394402

403+
it 'does not register an offense for a multiline setter method' do
404+
expect_no_offenses(<<~RUBY)
405+
def my_method=(arg)
406+
x.foo
407+
.bar
408+
.baz
409+
end
410+
RUBY
411+
end
412+
395413
it 'does not register an offense when the endless version excess Metrics/MaxLineLength[Max]' do
396414
expect_no_offenses(<<~RUBY)
397415
def my_method

0 commit comments

Comments
 (0)