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 a false positive for Layout/SpaceAroundOperators #10054

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#10054](https://github.com/rubocop/rubocop/pull/10054): Fix a false positive for `Layout/SpaceAroundOperators` when match operators between `<<` and `+=`. ([@koic][])
8 changes: 7 additions & 1 deletion lib/rubocop/cop/mixin/preceding_following_alignment.rb
Expand Up @@ -93,7 +93,13 @@ def aligned_char?(range, line)
end

def aligned_assignment?(range, line)
range.source[-1] == '=' && line[range.last_column - 1] == '='
range.source[-1] == '=' && line[range.last_column - 1] == '=' ||
aligned_with_append_operator?(range, line)
end

def aligned_with_append_operator?(range, line)
range.source == '<<' && line[range.last_column - 1] == '=' ||
range.source[-1] == '=' && line[(range.last_column - 2)..(range.last_column - 1)] == '<<'
end

def aligned_identical?(range, line)
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/layout/space_around_operators_spec.rb
Expand Up @@ -858,6 +858,20 @@ class ShowSourceTestClass < ShowSourceTestSuperClass
RUBY
end

it 'does not register an offenses match operators between `<<` and `+=`' do
expect_no_offenses(<<~RUBY)
x << foo
yz += bar
RUBY
end

it 'does not register an offenses match operators between `+=` and `<<`' do
expect_no_offenses(<<~RUBY)
x += foo
yz << bar
RUBY
end

it 'registers an offense and corrects various assignments with too many spaces' do
expect_offense(<<~RUBY)
x ||= 0
Expand Down