diff options
Diffstat (limited to 'rubocop/cop/performance')
5 files changed, 13 insertions, 14 deletions
diff --git a/rubocop/cop/performance/active_record_subtransaction_methods.rb b/rubocop/cop/performance/active_record_subtransaction_methods.rb index 3b89d3ab858..1789e20ce45 100644 --- a/rubocop/cop/performance/active_record_subtransaction_methods.rb +++ b/rubocop/cop/performance/active_record_subtransaction_methods.rb @@ -5,7 +5,7 @@ module RuboCop module Performance # Cop that disallows certain methods that rely on subtransactions in their implementation. # Companion to Performance/ActiveRecordSubtransactions, which bans direct usage of subtransactions. - class ActiveRecordSubtransactionMethods < RuboCop::Cop::Cop + class ActiveRecordSubtransactionMethods < RuboCop::Cop::Base MSG = 'Methods that rely on subtransactions should not be used. ' \ 'For more information see: https://gitlab.com/gitlab-org/gitlab/-/issues/338346' @@ -21,7 +21,7 @@ module RuboCop def on_send(node) return unless DISALLOWED_METHODS.include?(node.method_name) - add_offense(node, location: :selector) + add_offense(node.loc.selector) end end end diff --git a/rubocop/cop/performance/active_record_subtransactions.rb b/rubocop/cop/performance/active_record_subtransactions.rb index a550b558e52..165efee7c6b 100644 --- a/rubocop/cop/performance/active_record_subtransactions.rb +++ b/rubocop/cop/performance/active_record_subtransactions.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Performance - class ActiveRecordSubtransactions < RuboCop::Cop::Cop + class ActiveRecordSubtransactions < RuboCop::Cop::Base MSG = 'Subtransactions should not be used. ' \ 'For more information see: https://gitlab.com/gitlab-org/gitlab/-/issues/338346' diff --git a/rubocop/cop/performance/ar_count_each.rb b/rubocop/cop/performance/ar_count_each.rb index 2fe8e549872..1dae473d87d 100644 --- a/rubocop/cop/performance/ar_count_each.rb +++ b/rubocop/cop/performance/ar_count_each.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Performance - class ARCountEach < RuboCop::Cop::Cop + class ARCountEach < RuboCop::Cop::Base def message(ivar) "If #{ivar} is AR relation, avoid `#{ivar}.count ...; #{ivar}.each... `, this will trigger two queries. " \ "Use `#{ivar}.load.size ...; #{ivar}.each... ` instead. If #{ivar} is an array, try to use #{ivar}.size." @@ -35,7 +35,7 @@ module RuboCop begin_node.each_descendant do |n| ivar_each = each_match(n) - add_offense(node, location: :expression, message: message(ivar_count)) if ivar_each == ivar_count + add_offense(node, message: message(ivar_count)) if ivar_each == ivar_count end end end diff --git a/rubocop/cop/performance/ar_exists_and_present_blank.rb b/rubocop/cop/performance/ar_exists_and_present_blank.rb index 54c2d6bf95a..c2ba6c2a206 100644 --- a/rubocop/cop/performance/ar_exists_and_present_blank.rb +++ b/rubocop/cop/performance/ar_exists_and_present_blank.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module Performance - class ARExistsAndPresentBlank < RuboCop::Cop::Cop + class ARExistsAndPresentBlank < RuboCop::Cop::Base def message_present(ivar) "Avoid `#{ivar}.present?`, because it will generate database query 'Select TABLE.*' which is expensive. "\ "Suggest to use `#{ivar}.any?` to replace `#{ivar}.present?`" @@ -46,8 +46,8 @@ module RuboCop ivar_exists = exists_match(n) next unless ivar_exists - add_offense(node, location: :expression, message: message_present(ivar_exists)) if ivar_exists == ivar_present - add_offense(node, location: :expression, message: message_blank(ivar_exists)) if ivar_exists == ivar_blank + add_offense(node, message: message_present(ivar_exists)) if ivar_exists == ivar_present + add_offense(node, message: message_blank(ivar_exists)) if ivar_exists == ivar_blank end end end diff --git a/rubocop/cop/performance/readlines_each.rb b/rubocop/cop/performance/readlines_each.rb index cb4ffaca6e9..7a3a15020db 100644 --- a/rubocop/cop/performance/readlines_each.rb +++ b/rubocop/cop/performance/readlines_each.rb @@ -3,7 +3,9 @@ module RuboCop module Cop module Performance - class ReadlinesEach < RuboCop::Cop::Cop + class ReadlinesEach < RuboCop::Cop::Base + extend RuboCop::Cop::AutoCorrector + MESSAGE = 'Avoid `IO.readlines.each`, since it reads contents into memory in full. ' \ 'Use `IO.each_line` or `IO.each` instead.' @@ -17,12 +19,9 @@ module RuboCop PATTERN def on_send(node) - full_file_read_via_class?(node) { add_offense(node, location: :selector, message: MESSAGE) } - full_file_read_via_instance?(node) { add_offense(node, location: :selector, message: MESSAGE) } - end + return unless full_file_read_via_class?(node) || full_file_read_via_instance?(node) - def autocorrect(node) - lambda do |corrector| + add_offense(node.loc.selector, message: MESSAGE) do |corrector| corrector.replace(node.loc.expression, node.source.gsub('readlines.each', 'each_line')) end end |