From d98717ceefd17a897e9b8a41112cf4cd5b11547b Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 12 Jul 2023 04:03:06 -0400 Subject: [PATCH] Fix `Style/SlicingWithRange` cop (#25923) --- .rubocop_todo.yml | 15 --------------- app/lib/emoji_formatter.rb | 2 +- app/lib/text_formatter.rb | 6 +++--- app/models/account_alias.rb | 2 +- app/models/domain_block.rb | 2 +- app/models/email_domain_block.rb | 2 +- app/models/preview_card_provider.rb | 2 +- app/validators/status_length_validator.rb | 2 +- ...26175042_add_case_insensitive_index_to_tags.rb | 2 +- lib/active_record/batches.rb | 2 +- lib/mastodon/premailer_webpack_strategy.rb | 2 +- lib/tasks/repo.rake | 2 +- 12 files changed, 13 insertions(+), 28 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 0aaae67fb5..115d496c24 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -908,21 +908,6 @@ Style/SingleArgumentDig: Exclude: - 'lib/webpacker/manifest_extensions.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -Style/SlicingWithRange: - Exclude: - - 'app/lib/emoji_formatter.rb' - - 'app/lib/text_formatter.rb' - - 'app/models/account_alias.rb' - - 'app/models/domain_block.rb' - - 'app/models/email_domain_block.rb' - - 'app/models/preview_card_provider.rb' - - 'app/validators/status_length_validator.rb' - - 'db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb' - - 'lib/active_record/batches.rb' - - 'lib/mastodon/premailer_webpack_strategy.rb' - - 'lib/tasks/repo.rake' - # This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. # SupportedStyles: require_parentheses, require_no_parentheses diff --git a/app/lib/emoji_formatter.rb b/app/lib/emoji_formatter.rb index 8c3856d897..15b98dc57e 100644 --- a/app/lib/emoji_formatter.rb +++ b/app/lib/emoji_formatter.rb @@ -53,7 +53,7 @@ class EmojiFormatter end end - result << Nokogiri::XML::Text.new(text[last_index..-1], tree.document) + result << Nokogiri::XML::Text.new(text[last_index..], tree.document) node.replace(result) end diff --git a/app/lib/text_formatter.rb b/app/lib/text_formatter.rb index 04b34cf193..f9205e7d47 100644 --- a/app/lib/text_formatter.rb +++ b/app/lib/text_formatter.rb @@ -57,8 +57,8 @@ class TextFormatter prefix = url.match(URL_PREFIX_REGEX).to_s display_url = url[prefix.length, 30] - suffix = url[prefix.length + 30..-1] - cutoff = url[prefix.length..-1].length > 30 + suffix = url[prefix.length + 30..] + cutoff = url[prefix.length..].length > 30 <<~HTML.squish.html_safe # rubocop:disable Rails/OutputSafety #{h(display_url)} @@ -84,7 +84,7 @@ class TextFormatter indices.last end - result << h(text[last_index..-1]) + result << h(text[last_index..]) result end diff --git a/app/models/account_alias.rb b/app/models/account_alias.rb index b7267d6320..f859344fa2 100644 --- a/app/models/account_alias.rb +++ b/app/models/account_alias.rb @@ -25,7 +25,7 @@ class AccountAlias < ApplicationRecord def acct=(val) val = val.to_s.strip - super(val.start_with?('@') ? val[1..-1] : val) + super(val.start_with?('@') ? val[1..] : val) end def pretty_acct diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index 108dfe3d45..a7af2d62e2 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -69,7 +69,7 @@ class DomainBlock < ApplicationRecord uri = Addressable::URI.new.tap { |u| u.host = domain.strip.delete('/') } segments = uri.normalized_host.split('.') - variants = segments.map.with_index { |_, i| segments[i..-1].join('.') } + variants = segments.map.with_index { |_, i| segments[i..].join('.') } where(domain: variants).order(Arel.sql('char_length(domain) desc')).first rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError diff --git a/app/models/email_domain_block.rb b/app/models/email_domain_block.rb index 3c9be51cab..60e90208db 100644 --- a/app/models/email_domain_block.rb +++ b/app/models/email_domain_block.rb @@ -64,7 +64,7 @@ class EmailDomainBlock < ApplicationRecord segments = uri.normalized_host.split('.') - segments.map.with_index { |_, i| segments[i..-1].join('.') } + segments.map.with_index { |_, i| segments[i..].join('.') } end end diff --git a/app/models/preview_card_provider.rb b/app/models/preview_card_provider.rb index 9f5f6d3cb9..f3e4b49013 100644 --- a/app/models/preview_card_provider.rb +++ b/app/models/preview_card_provider.rb @@ -54,6 +54,6 @@ class PreviewCardProvider < ApplicationRecord def self.matching_domain(domain) segments = domain.split('.') - where(domain: segments.map.with_index { |_, i| segments[i..-1].join('.') }).order(Arel.sql('char_length(domain) desc')).first + where(domain: segments.map.with_index { |_, i| segments[i..].join('.') }).order(Arel.sql('char_length(domain) desc')).first end end diff --git a/app/validators/status_length_validator.rb b/app/validators/status_length_validator.rb index e107912b77..727d24d919 100644 --- a/app/validators/status_length_validator.rb +++ b/app/validators/status_length_validator.rb @@ -53,7 +53,7 @@ class StatusLengthValidator < ActiveModel::Validator entity[:indices].last end - result << str[last_index..-1] + result << str[last_index..] result end end diff --git a/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb b/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb index 12cc40d960..f2d3aa8ee8 100644 --- a/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb +++ b/db/migrate/20190726175042_add_case_insensitive_index_to_tags.rb @@ -6,7 +6,7 @@ class AddCaseInsensitiveIndexToTags < ActiveRecord::Migration[5.2] def up Tag.connection.select_all('SELECT string_agg(id::text, \',\') AS ids FROM tags GROUP BY lower(name) HAVING count(*) > 1').to_ary.each do |row| canonical_tag_id = row['ids'].split(',').first - redundant_tag_ids = row['ids'].split(',')[1..-1] + redundant_tag_ids = row['ids'].split(',')[1..] safety_assured do execute "UPDATE accounts_tags AS t0 SET tag_id = #{canonical_tag_id} WHERE tag_id IN (#{redundant_tag_ids.join(', ')}) AND NOT EXISTS (SELECT t1.tag_id FROM accounts_tags AS t1 WHERE t1.tag_id = #{canonical_tag_id} AND t1.account_id = t0.account_id)" diff --git a/lib/active_record/batches.rb b/lib/active_record/batches.rb index 55d29e52ef..91e50cc43d 100644 --- a/lib/active_record/batches.rb +++ b/lib/active_record/batches.rb @@ -29,7 +29,7 @@ module ActiveRecord if flatten yield record[1] else - yield record[1..-1] + yield record[1..] end end diff --git a/lib/mastodon/premailer_webpack_strategy.rb b/lib/mastodon/premailer_webpack_strategy.rb index 45e87603f0..6816d04745 100644 --- a/lib/mastodon/premailer_webpack_strategy.rb +++ b/lib/mastodon/premailer_webpack_strategy.rb @@ -12,7 +12,7 @@ module PremailerWebpackStrategy css = if url.start_with?('http') HTTP.get(url).to_s else - url = url[1..-1] if url.start_with?('/') + url = url[1..] if url.start_with?('/') Rails.public_path.join(url).read end diff --git a/lib/tasks/repo.rake b/lib/tasks/repo.rake index 888337b4f2..33c454444e 100644 --- a/lib/tasks/repo.rake +++ b/lib/tasks/repo.rake @@ -50,7 +50,7 @@ namespace :repo do file.each_line do |line| if line.start_with?('-') new_line = line.gsub(/#([[:digit:]]+)*/) do |pull_request_reference| - pull_request_number = pull_request_reference[1..-1] + pull_request_number = pull_request_reference[1..] response = nil loop do