diff --git a/lib/rubocop/cop/lint/ambiguous_block_association.rb b/lib/rubocop/cop/lint/ambiguous_block_association.rb index 6f7a5c1df00..2f7cca915e0 100644 --- a/lib/rubocop/cop/lint/ambiguous_block_association.rb +++ b/lib/rubocop/cop/lint/ambiguous_block_association.rb @@ -30,10 +30,11 @@ class AmbiguousBlockAssociation < Cop 'call.' def on_send(node) - return if !node.arguments? || node.parenthesized? || - node.last_argument.lambda? || allowed_method?(node) + return unless node.arguments? return unless ambiguous_block_association?(node) + return if node.parenthesized? || + node.last_argument.lambda? || allowed_method?(node) add_offense(node) end diff --git a/lib/rubocop/cop/lint/big_decimal_new.rb b/lib/rubocop/cop/lint/big_decimal_new.rb index 02f595fd59e..fa55ff06c25 100644 --- a/lib/rubocop/cop/lint/big_decimal_new.rb +++ b/lib/rubocop/cop/lint/big_decimal_new.rb @@ -24,7 +24,9 @@ class BigDecimalNew < Cop PATTERN def on_send(node) - return unless big_decimal_new(node) do |captured_value| + return unless node.method?(:new) + + big_decimal_new(node) do |captured_value| double_colon = captured_value ? '::' : '' message = format(MSG, double_colon: double_colon) diff --git a/lib/rubocop/cop/lint/debugger.rb b/lib/rubocop/cop/lint/debugger.rb index d1df5ba45fe..de3859175f6 100644 --- a/lib/rubocop/cop/lint/debugger.rb +++ b/lib/rubocop/cop/lint/debugger.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'set' + module RuboCop module Cop module Lint @@ -35,6 +37,11 @@ module Lint class Debugger < Cop MSG = 'Remove debugger entry point `%s`.' + DEBUGGER_METHODS = %i[ + debugger byebug remote_byebug pry remote_pry pry_remote console rescue + save_and_open_page save_and_open_screenshot save_screenshot irb + ].to_set.freeze + def_node_matcher :kernel?, <<~PATTERN { (const nil? :Kernel) @@ -57,6 +64,7 @@ class Debugger < Cop PATTERN def on_send(node) + return unless DEBUGGER_METHODS.include?(node.method_name) return unless debugger_call?(node) || binding_irb?(node) add_offense(node) diff --git a/lib/rubocop/cop/lint/deprecated_class_methods.rb b/lib/rubocop/cop/lint/deprecated_class_methods.rb index 35b9fd3a4ba..63005c49453 100644 --- a/lib/rubocop/cop/lint/deprecated_class_methods.rb +++ b/lib/rubocop/cop/lint/deprecated_class_methods.rb @@ -60,7 +60,11 @@ def class_nodes replacement: :block_given?) ].freeze + DEPRECATED_METHODS = DEPRECATED_METHODS_OBJECT.map(&:deprecated_method).freeze + def on_send(node) + return unless DEPRECATED_METHODS.include?(node.method_name) + check(node) do |data| message = format(MSG, current: deprecated_method(data), prefer: replacement_method(data)) diff --git a/lib/rubocop/cop/lint/duplicate_methods.rb b/lib/rubocop/cop/lint/duplicate_methods.rb index fecead95116..08824a5badf 100644 --- a/lib/rubocop/cop/lint/duplicate_methods.rb +++ b/lib/rubocop/cop/lint/duplicate_methods.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'set' + module RuboCop module Cop module Lint @@ -53,6 +55,9 @@ class DuplicateMethods < Cop MSG = 'Method `%s` is defined at both %s and ' \ '%s.' + METHOD_DEF_METHODS = %i[alias_method attr_reader attr_writer + attr_accessor attr].to_set.freeze + def initialize(config = nil, options = nil) super @definitions = {} @@ -97,7 +102,10 @@ def on_alias(node) def_node_matcher :sym_name, '(sym $_name)' + # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def on_send(node) + return unless METHOD_DEF_METHODS.include?(node.method_name) + if (name = alias_method?(node)) return unless name return if node.ancestors.any?(&:if_type?) @@ -108,6 +116,7 @@ def on_send(node) on_attr(node, *attr) end end + # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity private diff --git a/lib/rubocop/cop/lint/format_parameter_mismatch.rb b/lib/rubocop/cop/lint/format_parameter_mismatch.rb index 356b06d5175..14db798904e 100644 --- a/lib/rubocop/cop/lint/format_parameter_mismatch.rb +++ b/lib/rubocop/cop/lint/format_parameter_mismatch.rb @@ -44,9 +44,10 @@ class FormatParameterMismatch < Cop KERNEL = 'Kernel' SHOVEL = '<<' STRING_TYPES = %i[str dstr].freeze + FORMAT_METHODS = %i[format sprintf %].freeze def on_send(node) - return unless format_string?(node) + return unless FORMAT_METHODS.include?(node.method_name) && format_string?(node) if invalid_format_string?(node) add_offense(node, location: :selector, message: MSG_INVALID) diff --git a/lib/rubocop/cop/lint/ineffective_access_modifier.rb b/lib/rubocop/cop/lint/ineffective_access_modifier.rb index c783d5f5a45..57a6caa3a83 100644 --- a/lib/rubocop/cop/lint/ineffective_access_modifier.rb +++ b/lib/rubocop/cop/lint/ineffective_access_modifier.rb @@ -70,9 +70,7 @@ def on_module(node) def check_node(node) return unless node&.begin_type? - ignored_methods = private_class_method_names(node) - - ineffective_modifier(node, ignored_methods) do |defs_node, modifier| + ineffective_modifier(node) do |defs_node, modifier| add_offense(defs_node, location: :keyword, message: format_message(modifier)) @@ -97,20 +95,26 @@ def format_message(modifier) alternative: alternative) end - def ineffective_modifier(node, ignored_methods, modifier = nil, &block) + # rubocop:disable Metrics/CyclomaticComplexity + def ineffective_modifier(node, modifier = nil, &block) + ignored_methods = nil + node.each_child_node do |child| case child.type when :send modifier = child if access_modifier?(child) when :defs + ignored_methods ||= private_class_method_names(node) next if correct_visibility?(child, modifier, ignored_methods) yield child, modifier when :kwbegin + ignored_methods ||= private_class_method_names(node) ineffective_modifier(child, ignored_methods, modifier, &block) end end end + # rubocop:enable Metrics/CyclomaticComplexity def access_modifier?(node) node.bare_access_modifier? && !node.method?(:module_function) diff --git a/lib/rubocop/cop/lint/inherit_exception.rb b/lib/rubocop/cop/lint/inherit_exception.rb index 713b535f8ba..d91b24df4ea 100644 --- a/lib/rubocop/cop/lint/inherit_exception.rb +++ b/lib/rubocop/cop/lint/inherit_exception.rb @@ -69,6 +69,8 @@ def on_class(node) end def on_send(node) + return unless node.method?(:new) + constant = class_new_call?(node) return unless constant && illegal_class_name?(constant) diff --git a/lib/rubocop/cop/lint/interpolation_check.rb b/lib/rubocop/cop/lint/interpolation_check.rb index f5adcacf6b1..766677ced48 100644 --- a/lib/rubocop/cop/lint/interpolation_check.rb +++ b/lib/rubocop/cop/lint/interpolation_check.rb @@ -21,11 +21,10 @@ class InterpolationCheck < Cop 'Use double quoted strings if you need interpolation.' def on_str(node) - return if heredoc?(node) - parent = node.parent return if parent && (parent.dstr_type? || parent.regexp_type?) return unless /(?