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

Teach Lint/AssignmentInCondition about case #12546

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12546](https://github.com/rubocop/rubocop/pull/12546): Teach `Lint/AssignmentInCondition` about `case`. ([@sambostock][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/lint/assignment_in_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def on_if(node)
end
alias on_while on_if
alias on_until on_if
alias on_case on_if

private

Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/lint/assignment_in_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@
RUBY
end

it 'registers an offense for lvar assignment in case condition' do
expect_offense(<<~RUBY)
case test = 10
^ Use `==` if you meant to do a comparison or wrap the expression in parentheses to indicate you meant to assign in a condition.
when nil
end
RUBY

expect_correction(<<~RUBY)
case (test = 10)
when nil
end
RUBY
end

it 'registers an offense for ivar assignment in condition' do
expect_offense(<<~RUBY)
if @test = 10
Expand Down