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

Lint/LiteralAsCondition false positive with case/in expression #10755

Closed
tomajuskeviciute opened this issue Jun 27, 2022 · 0 comments · Fixed by #10756
Closed

Lint/LiteralAsCondition false positive with case/in expression #10755

tomajuskeviciute opened this issue Jun 27, 2022 · 0 comments · Fixed by #10756
Labels

Comments

@tomajuskeviciute
Copy link

@tomajuskeviciute tomajuskeviciute commented Jun 27, 2022

Since Ruby 2.7, case expressions also provide a more powerful experimental pattern matching feature via the in keyword:

case { a: 1, b: 2, c: 3 }
in a: Integer => m
  "matched: #{m}"
else
  "not matched"
end
# => "matched: 1"

Lint/LiteralAsCondition offense is detected when using this new pattern matching feature.


Expected behavior

No offenses detected for Lint/LiteralAsCondition cop.

Actual behavior

Offenses:

example.rb:1:6: W: Lint/LiteralAsCondition: Literal { a: 1, b: 2, c: 3 } appeared as a condition.
case { a: 1, b: 2, c: 3 }
     ^^^^^^^^^^^^^^^^^^^^

Steps to reproduce the problem

case { a: 1, b: 2, c: 3 }
in a: Integer => m
  "matched: #{m}"
else
  "not matched"
end

RuboCop version

$ bundle exec rubocop -V
1.31.0 (using Parser 3.1.2.0, rubocop-ast 1.18.0, running on ruby 3.0.4 x86_64-linux-musl)
  - rubocop-graphql 0.14.3
  - rubocop-performance 1.14.2
  - rubocop-rails 2.15.1
  - rubocop-rspec 2.11.1
@koic koic added the bug label Jun 27, 2022
koic added a commit to koic/rubocop that referenced this issue Jun 27, 2022
Fixes rubocop#10755.

This PR fixes a false positive for `Lint/LiteralAsCondition`
when using a literal in `case-in` condition where the match variable is used in
`in` are accepted as a pattern matching.
bbatsov pushed a commit that referenced this issue Jun 28, 2022
Fixes #10755.

This PR fixes a false positive for `Lint/LiteralAsCondition`
when using a literal in `case-in` condition where the match variable is used in
`in` are accepted as a pattern matching.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants