diff --git a/CHANGELOG.md b/CHANGELOG.md index 390288cee9c..91a43e2329b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [#7709](https://github.com/rubocop-hq/rubocop/issues/7709): Fix correction of `Style/RedundantCondition` when the else branch contains a range. ([@rrosenblum][]) * [#7682](https://github.com/rubocop-hq/rubocop/issues/7682): Fix `Style/InverseMethods` autofix leaving parenthesis. ([@tejasbubane][]) * [#7745](https://github.com/rubocop-hq/rubocop/issues/7745): Suppress a pending cop warnings when pending cop's department is disabled. ([@koic][]) +* [#7759](https://github.com/rubocop-hq/rubocop/issues/7759): Fix an error for `Layout/LineLength` cop when using lambda syntax that argument is not enclosed in parentheses. ([@koic][]) ## 0.80.0 (2020-02-18) diff --git a/lib/rubocop/cop/layout/line_length.rb b/lib/rubocop/cop/layout/line_length.rb index 8f1b1e9e1b5..503ca44255b 100644 --- a/lib/rubocop/cop/layout/line_length.rb +++ b/lib/rubocop/cop/layout/line_length.rb @@ -131,7 +131,7 @@ def check_for_breakable_block(block_node) end def breakable_block_range(block_node) - if block_node.arguments? + if block_node.arguments? && !block_node.lambda? block_node.arguments.loc.end else block_node.loc.begin diff --git a/spec/rubocop/cop/layout/line_length_spec.rb b/spec/rubocop/cop/layout/line_length_spec.rb index 3f32300e4db..d8e60219539 100644 --- a/spec/rubocop/cop/layout/line_length_spec.rb +++ b/spec/rubocop/cop/layout/line_length_spec.rb @@ -791,6 +791,36 @@ def baz(bar) RUBY end end + + context 'lambda syntax' do + context 'when argument is enclosed in parentheses' do + it 'registers an offense and corrects' do + expect_offense(<<~RUBY) + ->(x) { fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [70/40] + RUBY + + expect_correction(<<~RUBY) + ->(x) { + fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + RUBY + end + end + + context 'when argument is not enclosed in parentheses' do + it 'registers an offense and corrects' do + expect_offense(<<~RUBY) + -> x { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [70/40] + RUBY + + expect_correction(<<~RUBY) + -> x { + foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + RUBY + end + end + end end context 'semicolon' do