From 6042aed6f3dbc464e70d0fd289b07067e7fd0833 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 16 Jul 2020 17:02:30 +0300 Subject: [PATCH] Small `Style` cops perf tweaks --- .../cop/style/access_modifier_declarations.rb | 12 ++++++- lib/rubocop/cop/style/colon_method_call.rb | 6 ++-- lib/rubocop/cop/style/eval_with_location.rb | 4 +++ .../cop/style/expand_path_arguments.rb | 4 +++ lib/rubocop/cop/style/format_string.rb | 4 +++ lib/rubocop/cop/style/format_string_token.rb | 1 + lib/rubocop/cop/style/hash_syntax.rb | 4 ++- lib/rubocop/cop/style/inverse_methods.rb | 5 ++- .../method_call_without_args_parentheses.rb | 2 +- lib/rubocop/cop/style/numeric_predicate.rb | 4 +++ lib/rubocop/cop/style/random_with_offset.rb | 1 + lib/rubocop/cop/style/redundant_exception.rb | 4 +++ lib/rubocop/cop/style/redundant_sort.rb | 35 +++++++++++++------ lib/rubocop/cop/style/signal_exception.rb | 2 ++ .../cop/style/zero_length_predicate.rb | 16 +++++---- 15 files changed, 79 insertions(+), 25 deletions(-) diff --git a/lib/rubocop/cop/style/access_modifier_declarations.rb b/lib/rubocop/cop/style/access_modifier_declarations.rb index 21853fa9dbc..7530f52d17d 100644 --- a/lib/rubocop/cop/style/access_modifier_declarations.rb +++ b/lib/rubocop/cop/style/access_modifier_declarations.rb @@ -65,6 +65,8 @@ module Style class AccessModifierDeclarations < Cop include ConfigurableEnforcedStyle + ACCESS_MODIFIERS = %i[private protected public module_function].to_set.freeze + GROUP_STYLE_MESSAGE = [ '`%s` should not be', 'inlined in method definitions.' @@ -80,7 +82,7 @@ class AccessModifierDeclarations < Cop PATTERN def on_send(node) - return unless node.access_modifier? + return unless access_modifier?(node) return if node.parent.pair_type? return if cop_config['AllowModifiersOnSymbols'] && access_modifier_with_symbol?(node) @@ -96,6 +98,14 @@ def on_send(node) private + def access_modifier?(node) + maybe_access_modifier?(node) && node.access_modifier? + end + + def maybe_access_modifier?(node) + !node.receiver && ACCESS_MODIFIERS.include?(node.method_name) + end + def offense?(node) (group_style? && access_modifier_is_inlined?(node)) || (inline_style? && access_modifier_is_not_inlined?(node)) diff --git a/lib/rubocop/cop/style/colon_method_call.rb b/lib/rubocop/cop/style/colon_method_call.rb index abe3407c1e6..309d455d8e1 100644 --- a/lib/rubocop/cop/style/colon_method_call.rb +++ b/lib/rubocop/cop/style/colon_method_call.rb @@ -30,12 +30,12 @@ def self.autocorrect_incompatible_with end def on_send(node) - # ignore Java interop code like Java::int - return if java_type_node?(node) - return unless node.receiver && node.double_colon? return if node.camel_case_method? + # ignore Java interop code like Java::int + return if java_type_node?(node) + add_offense(node, location: :dot) end diff --git a/lib/rubocop/cop/style/eval_with_location.rb b/lib/rubocop/cop/style/eval_with_location.rb index eaa1298370b..dbd46a43680 100644 --- a/lib/rubocop/cop/style/eval_with_location.rb +++ b/lib/rubocop/cop/style/eval_with_location.rb @@ -37,6 +37,8 @@ class EvalWithLocation < Cop MSG_INCORRECT_LINE = 'Use `%s` instead of `%s`, ' \ 'as they are used by backtraces.' + EVAL_METHODS = %i[eval class_eval module_eval instance_eval].to_set.freeze + def_node_matcher :eval_without_location?, <<~PATTERN { (send nil? :eval ${str dstr}) @@ -61,6 +63,8 @@ class EvalWithLocation < Cop PATTERN def on_send(node) + return unless EVAL_METHODS.include?(node.method_name) + eval_without_location?(node) do |code| if with_lineno?(node) on_with_lineno(node, code) diff --git a/lib/rubocop/cop/style/expand_path_arguments.rb b/lib/rubocop/cop/style/expand_path_arguments.rb index b2faefdbeb2..6bad78ce997 100644 --- a/lib/rubocop/cop/style/expand_path_arguments.rb +++ b/lib/rubocop/cop/style/expand_path_arguments.rb @@ -73,7 +73,10 @@ class ExpandPathArguments < Cop $_) :parent) :expand_path) PATTERN + # rubocop:disable Metrics/PerceivedComplexity def on_send(node) + return unless node.method?(:expand_path) + if (captured_values = file_expand_path(node)) current_path, default_dir = captured_values @@ -88,6 +91,7 @@ def on_send(node) add_offense(node, message: PATHNAME_NEW_MSG) end end + # rubocop:enable Metrics/PerceivedComplexity def autocorrect(node) lambda do |corrector| diff --git a/lib/rubocop/cop/style/format_string.rb b/lib/rubocop/cop/style/format_string.rb index 9775e85fe42..5c3ec685339 100644 --- a/lib/rubocop/cop/style/format_string.rb +++ b/lib/rubocop/cop/style/format_string.rb @@ -40,6 +40,8 @@ class FormatString < Cop MSG = 'Favor `%s` over `%s`.' + FORMAT_METHODS = %i[format sprintf %].freeze + def_node_matcher :formatter, <<~PATTERN { (send nil? ${:sprintf :format} _ _ ...) @@ -53,6 +55,8 @@ class FormatString < Cop PATTERN def on_send(node) + return unless FORMAT_METHODS.include?(node.method_name) + formatter(node) do |selector| detected_style = selector == :% ? :percent : selector diff --git a/lib/rubocop/cop/style/format_string_token.rb b/lib/rubocop/cop/style/format_string_token.rb index a0076eee3f9..76e547a1b95 100644 --- a/lib/rubocop/cop/style/format_string_token.rb +++ b/lib/rubocop/cop/style/format_string_token.rb @@ -41,6 +41,7 @@ class FormatStringToken < Cop include ConfigurableEnforcedStyle def on_str(node) + return unless node.value.include?('%') return if node.each_ancestor(:xstr, :regexp).any? tokens(node) do |detected_style, token_range| diff --git a/lib/rubocop/cop/style/hash_syntax.rb b/lib/rubocop/cop/style/hash_syntax.rb index d6df3726e21..34158935900 100644 --- a/lib/rubocop/cop/style/hash_syntax.rb +++ b/lib/rubocop/cop/style/hash_syntax.rb @@ -62,6 +62,7 @@ class HashSyntax < Cop MSG_NO_MIXED_KEYS = "Don't mix styles in the same hash." MSG_HASH_ROCKETS = 'Use hash rockets syntax.' + # rubocop:disable Metrics/PerceivedComplexity, Metrics/AbcSize def on_hash(node) pairs = node.pairs @@ -73,10 +74,11 @@ def on_hash(node) ruby19_no_mixed_keys_check(pairs) elsif style == :no_mixed_keys no_mixed_keys_check(pairs) - else + elsif node.source.include?('=>') ruby19_check(pairs) end end + # rubocop:enable Metrics/PerceivedComplexity, Metrics/AbcSize def ruby19_check(pairs) check(pairs, '=>', MSG_19) if sym_indices?(pairs) diff --git a/lib/rubocop/cop/style/inverse_methods.rb b/lib/rubocop/cop/style/inverse_methods.rb index 84bae2e560c..39aac9c82d0 100644 --- a/lib/rubocop/cop/style/inverse_methods.rb +++ b/lib/rubocop/cop/style/inverse_methods.rb @@ -64,12 +64,11 @@ def self.autocorrect_incompatible_with PATTERN def on_send(node) - return if part_of_ignored_node?(node) - inverse_candidate?(node) do |_method_call, lhs, method, rhs| return unless inverse_methods.key?(method) - return if possible_class_hierarchy_check?(lhs, rhs, method) return if negated?(node) + return if part_of_ignored_node?(node) + return if possible_class_hierarchy_check?(lhs, rhs, method) add_offense(node, message: format(MSG, method: method, diff --git a/lib/rubocop/cop/style/method_call_without_args_parentheses.rb b/lib/rubocop/cop/style/method_call_without_args_parentheses.rb index 7cbd1738c3c..57e2a198174 100644 --- a/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +++ b/lib/rubocop/cop/style/method_call_without_args_parentheses.rb @@ -18,8 +18,8 @@ class MethodCallWithoutArgsParentheses < Cop 'no arguments.' def on_send(node) - return if ineligible_node?(node) return unless !node.arguments? && node.parenthesized? + return if ineligible_node?(node) return if ignored_method?(node.method_name) return if same_name_assignment?(node) diff --git a/lib/rubocop/cop/style/numeric_predicate.rb b/lib/rubocop/cop/style/numeric_predicate.rb index 4f7a44bc263..7fe50b19269 100644 --- a/lib/rubocop/cop/style/numeric_predicate.rb +++ b/lib/rubocop/cop/style/numeric_predicate.rb @@ -53,7 +53,11 @@ class NumericPredicate < Cop 'negative?' => '<' }.freeze + COMPARISON_METHODS = %i[== > < positive? negative? zero?].to_set.freeze + def on_send(node) + return unless COMPARISON_METHODS.include?(node.method_name) + numeric, replacement = check(node) return unless numeric diff --git a/lib/rubocop/cop/style/random_with_offset.rb b/lib/rubocop/cop/style/random_with_offset.rb index 4add03e04be..e9ba9b06c83 100644 --- a/lib/rubocop/cop/style/random_with_offset.rb +++ b/lib/rubocop/cop/style/random_with_offset.rb @@ -56,6 +56,7 @@ class RandomWithOffset < Cop PATTERN def on_send(node) + return unless node.receiver return unless integer_op_rand?(node) || rand_op_integer?(node) || rand_modified?(node) diff --git a/lib/rubocop/cop/style/redundant_exception.rb b/lib/rubocop/cop/style/redundant_exception.rb index 2e519d63883..0bd810706d7 100644 --- a/lib/rubocop/cop/style/redundant_exception.rb +++ b/lib/rubocop/cop/style/redundant_exception.rb @@ -23,9 +23,13 @@ class RedundantException < Base MSG_2 = 'Redundant `RuntimeError.new` call can be replaced with ' \ 'just the message.' + RAISE_METHODS = %i[raise fail].freeze + # Switch `raise RuntimeError, 'message'` to `raise 'message'`, and # `raise RuntimeError.new('message')` to `raise 'message'`. def on_send(node) + return unless RAISE_METHODS.include?(node.method_name) + fix_exploded(node) || fix_compact(node) end diff --git a/lib/rubocop/cop/style/redundant_sort.rb b/lib/rubocop/cop/style/redundant_sort.rb index bcdb0403d87..fc193f55895 100644 --- a/lib/rubocop/cop/style/redundant_sort.rb +++ b/lib/rubocop/cop/style/redundant_sort.rb @@ -55,6 +55,8 @@ class RedundantSort < Cop MSG = 'Use `%s` instead of '\ '`%s...%s`.' + SORT_METHODS = %i[sort sort_by].freeze + def_node_matcher :redundant_sort?, <<~MATCHER { (send $(send _ $:sort ...) ${:last :first}) @@ -71,20 +73,25 @@ class RedundantSort < Cop } MATCHER + # rubocop:disable Metrics/AbcSize def on_send(node) - redundant_sort?(node) do |sort_node, sorter, accessor| - range = range_between( - sort_node.loc.selector.begin_pos, - node.loc.expression.end_pos - ) + return unless sort_method?(node) - add_offense(node, - location: range, - message: message(node, - sorter, - accessor)) + if (sort_node, sorter, accessor = redundant_sort?(node.parent)) + ancestor = node.parent + elsif (sort_node, sorter, accessor = redundant_sort?(node.parent&.parent)) + ancestor = node.parent.parent + else + return end + + add_offense(ancestor, + location: offense_range(sort_node, ancestor), + message: message(ancestor, + sorter, + accessor)) end + # rubocop:enable Metrics/AbcSize def autocorrect(node) sort_node, sorter, accessor = redundant_sort?(node) @@ -108,6 +115,14 @@ def autocorrect(node) private + def sort_method?(node) + SORT_METHODS.include?(node.method_name) + end + + def offense_range(sort_node, ancestor) + range_between(sort_node.loc.selector.begin_pos, ancestor.loc.expression.end_pos) + end + def message(node, sorter, accessor) accessor_source = range_between( node.loc.selector.begin_pos, diff --git a/lib/rubocop/cop/style/signal_exception.rb b/lib/rubocop/cop/style/signal_exception.rb index fe2d676a186..c5aed00d722 100644 --- a/lib/rubocop/cop/style/signal_exception.rb +++ b/lib/rubocop/cop/style/signal_exception.rb @@ -191,6 +191,8 @@ def check_send(method_name, node) end def command_or_kernel_call?(name, node) + return unless node.method?(name) + node.command?(name) || kernel_call?(node, name) end diff --git a/lib/rubocop/cop/style/zero_length_predicate.rb b/lib/rubocop/cop/style/zero_length_predicate.rb index b6571d37224..539ca825ff4 100644 --- a/lib/rubocop/cop/style/zero_length_predicate.rb +++ b/lib/rubocop/cop/style/zero_length_predicate.rb @@ -30,6 +30,8 @@ class ZeroLengthPredicate < Cop NONZERO_MSG = 'Use `!empty?` instead of ' \ '`%s %s %s`.' + LENGTH_METHODS = %i[size length].freeze + def on_send(node) check_zero_length_predicate(node) check_nonzero_length_predicate(node) @@ -44,31 +46,33 @@ def autocorrect(node) private def check_zero_length_predicate(node) - zero_length_predicate = zero_length_predicate(node) + return unless LENGTH_METHODS.include?(node.method_name) + zero_length_predicate = zero_length_predicate(node.parent) return unless zero_length_predicate lhs, opr, rhs = zero_length_predicate - return if non_polymorphic_collection?(node) + return if non_polymorphic_collection?(node.parent) add_offense( - node, + node.parent, message: format(ZERO_MSG, lhs: lhs, opr: opr, rhs: rhs) ) end def check_nonzero_length_predicate(node) - nonzero_length_predicate = nonzero_length_predicate(node) + return unless LENGTH_METHODS.include?(node.method_name) + nonzero_length_predicate = nonzero_length_predicate(node.parent) return unless nonzero_length_predicate lhs, opr, rhs = nonzero_length_predicate - return if non_polymorphic_collection?(node) + return if non_polymorphic_collection?(node.parent) add_offense( - node, + node.parent, message: format(NONZERO_MSG, lhs: lhs, opr: opr, rhs: rhs) ) end