From a3bc2489abb344d602900954bb9cc52ca0257d67 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 28 Jun 2022 02:16:22 +0900 Subject: [PATCH] [Fix #10755] Fix a false positive for `Lint/LiteralAsCondition` 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. --- ..._positive_for_lint_literal_as_condition.md | 1 + lib/rubocop/cop/lint/literal_as_condition.rb | 5 +++++ .../cop/lint/literal_as_condition_spec.rb | 20 +++++++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_lint_literal_as_condition.md diff --git a/changelog/fix_a_false_positive_for_lint_literal_as_condition.md b/changelog/fix_a_false_positive_for_lint_literal_as_condition.md new file mode 100644 index 00000000000..3c2547c16cf --- /dev/null +++ b/changelog/fix_a_false_positive_for_lint_literal_as_condition.md @@ -0,0 +1 @@ +* [#10755](https://github.com/rubocop/rubocop/issues/10755): Fix 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. ([@koic][]) diff --git a/lib/rubocop/cop/lint/literal_as_condition.rb b/lib/rubocop/cop/lint/literal_as_condition.rb index 4f03eff4c55..a4473136830 100644 --- a/lib/rubocop/cop/lint/literal_as_condition.rb +++ b/lib/rubocop/cop/lint/literal_as_condition.rb @@ -7,6 +7,9 @@ module Lint # operands in and/or expressions serving as the conditions of # if/while/until/case-when/case-in. # + # NOTE: Literals in `case-in` condition where the match variable is used in + # `in` are accepted as a pattern matching. + # # @example # # # bad @@ -69,6 +72,8 @@ def on_case(case_node) def on_case_match(case_match_node) if case_match_node.condition + return if case_match_node.descendants.any?(&:match_var_type?) + check_case(case_match_node) else case_match_node.each_in_pattern do |in_pattern_node| diff --git a/spec/rubocop/cop/lint/literal_as_condition_spec.rb b/spec/rubocop/cop/lint/literal_as_condition_spec.rb index aa8ce87dc31..3b4044ff1a0 100644 --- a/spec/rubocop/cop/lint/literal_as_condition_spec.rb +++ b/spec/rubocop/cop/lint/literal_as_condition_spec.rb @@ -75,11 +75,19 @@ end context '>= Ruby 2.7', :ruby27 do - it "registers an offense for literal #{lit} in case match" do + it "accepts an offense for literal #{lit} in case match with a match var" do + expect_no_offenses(<<~RUBY, lit: lit) + case %{lit} + in x then top + end + RUBY + end + + it "registers an offense for literal #{lit} in case match without a match var" do expect_offense(<<~RUBY, lit: lit) case %{lit} ^{lit} Literal `#{lit}` appeared as a condition. - in x then top + in CONST then top end RUBY end @@ -219,6 +227,14 @@ RUBY end + it 'accepts an offense for case match with a match var' do + expect_no_offenses(<<~RUBY) + case { a: 1, b: 2, c: 3 } + in a: Integer => m + end + RUBY + end + it 'accepts dstr literal in case match' do expect_no_offenses(<<~'RUBY') case "#{x}"