diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7ec8f51d6e..15984f982c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2019-07-20 09:00:25 +0900 using RuboCop version 0.72.0. +# on 2019-07-27 17:51:55 +0800 using RuboCop version 0.73.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -15,16 +15,16 @@ InternalAffairs/NodeDestructuring: - 'lib/rubocop/cop/performance/regexp_match.rb' - 'lib/rubocop/cop/performance/string_replacement.rb' -# Offense count: 7 +# Offense count: 8 Metrics/AbcSize: Max: 17 # Offense count: 2 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 149 + Max: 156 -# Offense count: 14 +# Offense count: 15 # Configuration parameters: CountComments, ExcludedMethods. Metrics/MethodLength: Max: 14 diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ccd6897bf..1eebf85098 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * [#67](https://github.com/rubocop-hq/rubocop-performance/issues/67): Fix an error for `Performance/RedundantMerge` when `MaxKeyValuePairs` option is set to `null`. ([@koic][]) +* [#73](https://github.com/rubocop-hq/rubocop-performance/pull/73): Fix a false negative for `Performance/RegexpMatch` when `MatchData` is not detected in `if` branch of guard condition. ([@koic][]) ## 1.4.0 (2019-06-20) diff --git a/lib/rubocop/cop/performance/regexp_match.rb b/lib/rubocop/cop/performance/regexp_match.rb index bb2f068353..4aa15d7655 100644 --- a/lib/rubocop/cop/performance/regexp_match.rb +++ b/lib/rubocop/cop/performance/regexp_match.rb @@ -177,15 +177,23 @@ def last_match_used?(match_node) scope_root = scope_root(match_node) body = scope_root ? scope_body(scope_root) : match_node.ancestors.last - return true if match_node.parent.if_type? && - match_node.parent.modifier_form? + range = + if match_node.parent.if_type? && match_node.parent.modifier_form? + if_branch_range(match_node) + else + match_node_pos = match_node.loc.expression.begin_pos + next_match_pos = next_match_pos(body, match_node_pos, scope_root) + + match_node_pos..next_match_pos + end - match_node_pos = match_node.loc.expression.begin_pos + find_last_match(body, range, scope_root) + end - next_match_pos = next_match_pos(body, match_node_pos, scope_root) - range = match_node_pos..next_match_pos + def if_branch_range(match_node) + expression = match_node.parent.if_branch.loc.expression - find_last_match(body, range, scope_root) + expression.begin_pos..expression.end_pos end def next_match_pos(body, match_node_pos, scope_root) diff --git a/spec/rubocop/cop/performance/regexp_match_spec.rb b/spec/rubocop/cop/performance/regexp_match_spec.rb index 6f25765e5e..c1d97e30dd 100644 --- a/spec/rubocop/cop/performance/regexp_match_spec.rb +++ b/spec/rubocop/cop/performance/regexp_match_spec.rb @@ -38,6 +38,18 @@ end RUBY2 + include_examples 'offense', "#{name} in if condition", <<-RUBY, <<-RUBY2 + do_something if #{cond} + RUBY + do_something if #{correction} + RUBY2 + + include_examples 'offense', "#{name} in unless condition", <<-RUBY, <<-RUBY2 + do_something unless #{cond} + RUBY + do_something unless #{correction} + RUBY2 + include_examples 'offense', "#{name} in elsif condition", <<-RUBY, <<-RUBY2 if cond do_something