From 93d50875c63df4bc76f5e46d729eb7ada456657b Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 2 Nov 2021 06:24:02 +0900 Subject: [PATCH] [Fix #10227] Fix a false positive for `Style/ParenthesesAroundCondition` Fixes #10227. This PR fixes a false positive for `Style/ParenthesesAroundCondition` when parentheses in multiple expressions separated by semicolon. --- ...itive_for_style_parentheses_around_condition.md | 1 + .../cop/style/parentheses_around_condition.rb | 14 ++++++++++++-- .../cop/style/parentheses_around_condition_spec.rb | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_false_positive_for_style_parentheses_around_condition.md diff --git a/changelog/fix_false_positive_for_style_parentheses_around_condition.md b/changelog/fix_false_positive_for_style_parentheses_around_condition.md new file mode 100644 index 00000000000..3ba79301f63 --- /dev/null +++ b/changelog/fix_false_positive_for_style_parentheses_around_condition.md @@ -0,0 +1 @@ +* [#10227](https://github.com/rubocop/rubocop/issues/10227): Fix a false positive for `Style/ParenthesesAroundCondition` when parentheses in multiple expressions separated by semicolon. ([@koic][]) diff --git a/lib/rubocop/cop/style/parentheses_around_condition.rb b/lib/rubocop/cop/style/parentheses_around_condition.rb index 4ee96b36a63..a33e4eac694 100644 --- a/lib/rubocop/cop/style/parentheses_around_condition.rb +++ b/lib/rubocop/cop/style/parentheses_around_condition.rb @@ -56,6 +56,7 @@ module Style class ParenthesesAroundCondition < Base include SafeAssignment include Parentheses + include RangeHelp extend AutoCorrector def on_if(node) @@ -73,13 +74,14 @@ def on_while(node) # @!method control_op_condition(node) def_node_matcher :control_op_condition, <<~PATTERN - (begin $_ ...) + (begin $_ $...) PATTERN def process_control_op(node) cond = node.condition - control_op_condition(cond) do |first_child| + control_op_condition(cond) do |first_child, rest_children| + return if semicolon_separated_expressions?(first_child, rest_children) return if modifier_op?(first_child) return if parens_allowed?(cond) @@ -90,6 +92,14 @@ def process_control_op(node) end end + def semicolon_separated_expressions?(first_exp, rest_exps) + return false unless (second_exp = rest_exps.first) + + range = range_between(first_exp.source_range.end_pos, second_exp.source_range.begin_pos) + + range.source.include?(';') + end + def modifier_op?(node) return false if node.if_type? && node.ternary? return true if node.rescue_type? diff --git a/spec/rubocop/cop/style/parentheses_around_condition_spec.rb b/spec/rubocop/cop/style/parentheses_around_condition_spec.rb index 685c70cd25f..012a41a78b4 100644 --- a/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +++ b/spec/rubocop/cop/style/parentheses_around_condition_spec.rb @@ -102,6 +102,14 @@ RUBY end + it 'does not register an offense when parentheses in multiple expressions separated by semicolon' do + expect_no_offenses(<<~RUBY) + if (foo; bar) + do_something + end + RUBY + end + context 'safe assignment is allowed' do it 'accepts variable assignment in condition surrounded with parentheses' do expect_no_offenses(<<~RUBY)