Skip to content

Commit

Permalink
Enable Layout/RedundantLineBreak cop
Browse files Browse the repository at this point in the history
  • Loading branch information
koic committed Jul 30, 2022
1 parent c2bdc1a commit 5c15788
Show file tree
Hide file tree
Showing 36 changed files with 101 additions and 235 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -72,6 +72,9 @@ Layout/ClassStructure:
- protected_methods
- private_methods

Layout/RedundantLineBreak:
Enabled: true

# Trailing white space is meaningful in code examples
Layout/TrailingWhitespace:
AllowInHeredoc: true
Expand Down
10 changes: 2 additions & 8 deletions Rakefile
Expand Up @@ -36,11 +36,7 @@ end
desc 'Run RuboCop over itself'
RuboCop::RakeTask.new(:internal_investigation)

task default: %i[
documentation_syntax_check
spec
internal_investigation
]
task default: %i[documentation_syntax_check spec internal_investigation]

desc 'Generate a new cop template'
task :new_cop, [:cop] do |_task, args|
Expand All @@ -55,9 +51,7 @@ task :new_cop, [:cop] do |_task, args|

generator.write_source
generator.write_spec
generator.inject_require(
root_file_path: 'lib/rubocop/cop/performance_cops.rb'
)
generator.inject_require(root_file_path: 'lib/rubocop/cop/performance_cops.rb')
generator.inject_config(config_file_path: 'config/default.yml')

puts generator.todo
Expand Down
Expand Up @@ -41,11 +41,7 @@ def on_send(node)
end
elsif (numeric_to_d = to_d?(node))
add_offense(numeric_to_d.source_range) do |corrector|
big_decimal_args = node
.arguments
.map(&:source)
.unshift("'#{numeric_to_d.source}'")
.join(', ')
big_decimal_args = node.arguments.map(&:source).unshift("'#{numeric_to_d.source}'").join(', ')

corrector.replace(node, "BigDecimal(#{big_decimal_args})")
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/performance/bind_call.rb
Expand Up @@ -26,8 +26,7 @@ class BindCall < Base

minimum_target_ruby_version 2.7

MSG = 'Use `bind_call(%<bind_arg>s%<comma>s%<call_args>s)` ' \
'instead of `bind(%<bind_arg>s).call(%<call_args>s)`.'
MSG = 'Use `bind_call(%<bind_arg>s%<comma>s%<call_args>s)` instead of `bind(%<bind_arg>s).call(%<call_args>s)`.'
RESTRICT_ON_SEND = %i[call].freeze

def_node_matcher :bind_with_call_method?, <<~PATTERN
Expand Down
18 changes: 6 additions & 12 deletions lib/rubocop/cop/performance/case_when_splat.rb
Expand Up @@ -99,8 +99,7 @@ def replacement(conditions)

def inline_fix_branch(corrector, when_node)
conditions = when_node.conditions
range = range_between(conditions[0].loc.expression.begin_pos,
conditions[-1].loc.expression.end_pos)
range = range_between(conditions[0].loc.expression.begin_pos, conditions[-1].loc.expression.end_pos)

corrector.replace(range, replacement(conditions))
end
Expand All @@ -111,8 +110,7 @@ def reorder_condition(corrector, when_node)
return if when_branches.one?

corrector.remove(when_branch_range(when_node))
corrector.insert_after(when_branches.last.source_range,
reordering_correction(when_node))
corrector.insert_after(when_branches.last.source_range, reordering_correction(when_node))
end

def reordering_correction(when_node)
Expand All @@ -126,11 +124,9 @@ def reordering_correction(when_node)
end

def when_branch_range(when_node)
next_branch =
when_node.parent.when_branches[when_node.branch_index + 1]
next_branch = when_node.parent.when_branches[when_node.branch_index + 1]

range_between(when_node.source_range.begin_pos,
next_branch.source_range.begin_pos)
range_between(when_node.source_range.begin_pos, next_branch.source_range.begin_pos)
end

def new_condition_with_then(node, new_condition)
Expand Down Expand Up @@ -162,13 +158,11 @@ def splat_offenses(when_conditions)
def non_splat?(condition)
variable, = *condition

(condition.splat_type? && variable.array_type?) ||
!condition.splat_type?
(condition.splat_type? && variable.array_type?) || !condition.splat_type?
end

def needs_reorder?(when_node)
following_branches =
when_node.parent.when_branches[(when_node.branch_index + 1)..]
following_branches = when_node.parent.when_branches[(when_node.branch_index + 1)..]

following_branches.any? do |when_branch|
when_branch.conditions.any? do |condition|
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/performance/collection_literal_in_loop.rb
Expand Up @@ -104,9 +104,7 @@ def parent_is_loop?(node)
end

def loop?(ancestor, node)
keyword_loop?(ancestor.type) ||
kernel_loop?(ancestor) ||
node_within_enumerable_loop?(node, ancestor)
keyword_loop?(ancestor.type) || kernel_loop?(ancestor) || node_within_enumerable_loop?(node, ancestor)
end

def keyword_loop?(type)
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/performance/constant_regexp.rb
Expand Up @@ -39,9 +39,7 @@ class ConstantRegexp < Base
MSG = 'Extract this regexp into a constant, memoize it, or append an `/o` option to its options.'

def on_regexp(node)
return if within_allowed_assignment?(node) ||
!include_interpolated_const?(node) ||
node.single_interpolation?
return if within_allowed_assignment?(node) || !include_interpolated_const?(node) || node.single_interpolation?

add_offense(node) do |corrector|
corrector.insert_after(node, 'o')
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/performance/double_start_end_with.rb
Expand Up @@ -41,8 +41,7 @@ module Performance
class DoubleStartEndWith < Base
extend AutoCorrector

MSG = 'Use `%<receiver>s.%<method>s(%<combined_args>s)` ' \
'instead of `%<original_code>s`.'
MSG = 'Use `%<receiver>s.%<method>s(%<combined_args>s)` instead of `%<original_code>s`.'

def on_or(node)
receiver, method, first_call_args, second_call_args = process_source(node)
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/performance/end_with.rb
Expand Up @@ -50,8 +50,7 @@ class EndWith < Base
include RegexpMetacharacter
extend AutoCorrector

MSG = 'Use `String#end_with?` instead of a regex match anchored to ' \
'the end of the string.'
MSG = 'Use `String#end_with?` instead of a regex match anchored to the end of the string.'
RESTRICT_ON_SEND = %i[match =~ match?].freeze

def_node_matcher :redundant_regex?, <<~PATTERN
Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/performance/inefficient_hash_search.rb
Expand Up @@ -83,9 +83,7 @@ def current_method(node)

def use_long_method
preferred_config = config.for_all_cops['Style/PreferredHashMethods']
preferred_config &&
preferred_config['EnforcedStyle'] == 'long' &&
preferred_config['Enabled']
preferred_config && preferred_config['EnforcedStyle'] == 'long' && preferred_config['Enabled']
end

def autocorrect_argument(node)
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/performance/open_struct.rb
Expand Up @@ -32,8 +32,7 @@ module Performance
# end
#
class OpenStruct < Base
MSG = 'Consider using `Struct` over `OpenStruct` ' \
'to optimize the performance.'
MSG = 'Consider using `Struct` over `OpenStruct` to optimize the performance.'
RESTRICT_ON_SEND = %i[new].freeze

def_node_matcher :open_struct, <<~PATTERN
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/performance/redundant_match.rb
Expand Up @@ -20,8 +20,7 @@ module Performance
class RedundantMatch < Base
extend AutoCorrector

MSG = 'Use `=~` in places where the `MatchData` returned by ' \
'`#match` will not be used.'
MSG = 'Use `=~` in places where the `MatchData` returned by `#match` will not be used.'
RESTRICT_ON_SEND = %i[match].freeze

# 'match' is a fairly generic name, so we don't flag it unless we see
Expand Down
7 changes: 2 additions & 5 deletions lib/rubocop/cop/performance/redundant_merge.rb
Expand Up @@ -99,8 +99,7 @@ def kwsplat_used?(pairs)
end

def non_redundant_value_used?(receiver, node)
node.value_used? &&
!EachWithObjectInspector.new(node, receiver).value_used?
node.value_used? && !EachWithObjectInspector.new(node, receiver).value_used?
end

def correct_multiple_elements(corrector, node, parent, new_source)
Expand All @@ -125,9 +124,7 @@ def to_assignments(receiver, pairs)

key = key.sym_type? && pair.colon? ? ":#{key.source}" : key.source

format(AREF_ASGN, receiver: receiver.source,
key: key,
value: value.source)
format(AREF_ASGN, receiver: receiver.source, key: key, value: value.source)
end
end

Expand Down
12 changes: 2 additions & 10 deletions lib/rubocop/cop/performance/regexp_match.rb
Expand Up @@ -217,8 +217,7 @@ def modifier_form?(match_node)
def find_last_match(body, range, scope_root)
last_matches(body).find do |ref|
ref_pos = ref.loc.expression.begin_pos
range.cover?(ref_pos) &&
scope_root(ref) == scope_root
range.cover?(ref_pos) && scope_root(ref) == scope_root
end
end

Expand All @@ -241,14 +240,7 @@ def scope_root(node)
end

def match_gvar?(sym)
%i[
$~
$MATCH
$PREMATCH
$POSTMATCH
$LAST_PAREN_MATCH
$LAST_MATCH_INFO
].include?(sym)
%i[$~ $MATCH $PREMATCH $POSTMATCH $LAST_PAREN_MATCH $LAST_MATCH_INFO].include?(sym)
end

def correct_operator(corrector, recv, arg, oper = nil)
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/performance/squeeze.rb
Expand Up @@ -24,10 +24,7 @@ class Squeeze < Base
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[gsub gsub!].freeze

PREFERRED_METHODS = {
gsub: :squeeze,
gsub!: :squeeze!
}.freeze
PREFERRED_METHODS = { gsub: :squeeze, gsub!: :squeeze! }.freeze

def_node_matcher :squeeze_candidate?, <<~PATTERN
(send
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/performance/start_with.rb
Expand Up @@ -50,8 +50,7 @@ class StartWith < Base
include RegexpMetacharacter
extend AutoCorrector

MSG = 'Use `String#start_with?` instead of a regex match anchored to ' \
'the beginning of the string.'
MSG = 'Use `String#start_with?` instead of a regex match anchored to the beginning of the string.'
RESTRICT_ON_SEND = %i[match =~ match?].freeze

def_node_matcher :redundant_regex?, <<~PATTERN
Expand Down
9 changes: 3 additions & 6 deletions lib/rubocop/cop/performance/string_replacement.rb
Expand Up @@ -86,8 +86,7 @@ def accept_first_param?(first_param)

unless first_param.str_type?
return true if options
return true unless first_source.is_a?(String) &&
first_source =~ DETERMINISTIC_REGEX
return true unless first_source.is_a?(String) && first_source =~ DETERMINISTIC_REGEX

# This must be done after checking DETERMINISTIC_REGEX
# Otherwise things like \s will trip us up
Expand Down Expand Up @@ -141,8 +140,7 @@ def replacement_method(node, first_source, second_source)
end

def message(node, first_source, second_source)
replacement_method =
replacement_method(node, first_source, second_source)
replacement_method = replacement_method(node, first_source, second_source)

format(MSG, prefer: replacement_method, current: node.method_name)
end
Expand All @@ -152,8 +150,7 @@ def method_suffix(node)
end

def remove_second_param(corrector, node, first_param)
end_range = range_between(first_param.source_range.end_pos,
node.source_range.end_pos)
end_range = range_between(first_param.source_range.end_pos, node.source_range.end_pos)

corrector.replace(end_range, method_suffix(node))
end
Expand Down
6 changes: 2 additions & 4 deletions lib/rubocop/cop/performance/times_map.rb
Expand Up @@ -32,8 +32,7 @@ module Performance
class TimesMap < Base
extend AutoCorrector

MESSAGE = 'Use `Array.new(%<count>s)` with a block ' \
'instead of `.times.%<map_or_collect>s`'
MESSAGE = 'Use `Array.new(%<count>s)` with a block instead of `.times.%<map_or_collect>s`'
MESSAGE_ONLY_IF = 'only if `%<count>s` is always 0 or more'
RESTRICT_ON_SEND = %i[map collect].freeze

Expand All @@ -50,8 +49,7 @@ def on_block(node)
def check(node)
times_map_call(node) do |map_or_collect, count|
add_offense(node, message: message(map_or_collect, count)) do |corrector|
replacement = "Array.new(#{count.source}" \
"#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"
replacement = "Array.new(#{count.source}#{map_or_collect.arguments.map { |arg| ", #{arg.source}" }.join})"

corrector.replace(map_or_collect.loc.expression, replacement)
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/performance/uri_default_parser.rb
Expand Up @@ -15,8 +15,7 @@ module Performance
class UriDefaultParser < Base
extend AutoCorrector

MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of ' \
'`%<double_colon>sURI::Parser.new`.'
MSG = 'Use `%<double_colon>sURI::DEFAULT_PARSER` instead of `%<double_colon>sURI::Parser.new`.'
RESTRICT_ON_SEND = %i[new].freeze

def_node_matcher :uri_parser_new?, <<~PATTERN
Expand Down
17 changes: 5 additions & 12 deletions spec/project_spec.rb
Expand Up @@ -31,10 +31,8 @@
it 'requires a nicely formatted `VersionAdded` metadata for all cops' do
cop_names.each do |name|
version = config.dig(name, 'VersionAdded')
expect(version.nil?).to(be(false),
"`VersionAdded` configuration is required for `#{name}`.")
expect(version).to(match(version_regexp),
"#{version} should be format ('X.Y' or '<<next>>') for #{name}.")
expect(version.nil?).to(be(false), "`VersionAdded` configuration is required for `#{name}`.")
expect(version).to(match(version_regexp), "#{version} should be format ('X.Y' or '<<next>>') for #{name}.")
end
end

Expand All @@ -44,8 +42,7 @@
version = config.dig(name, version_type)
next unless version

expect(version).to(match(version_regexp),
"#{version} should be format ('X.Y' or '<<next>>') for #{name}.")
expect(version).to(match(version_regexp), "#{version} should be format ('X.Y' or '<<next>>') for #{name}.")
end
end
end
Expand Down Expand Up @@ -89,8 +86,7 @@
fname = File.expand_path('../config/default.yml', __dir__)
content = File.read(fname)
RuboCop::YAMLDuplicationChecker.check(content, fname) do |key1, key2|
raise "#{fname} has duplication of #{key1.value} " \
"on line #{key1.start_line} and line #{key2.start_line}"
raise "#{fname} has duplication of #{key1.value} on line #{key1.start_line} and line #{key2.start_line}"
end
end

Expand Down Expand Up @@ -175,10 +171,7 @@
describe 'body' do
let(:bodies) do
entries.map do |entry|
entry
.gsub(/`[^`]+`/, '``')
.sub(/^\*\s*(?:\[.+?\):\s*)?/, '')
.sub(/\s*\([^)]+\)$/, '')
entry.gsub(/`[^`]+`/, '``').sub(/^\*\s*(?:\[.+?\):\s*)?/, '').sub(/\s*\([^)]+\)$/, '')
end
end

Expand Down
9 changes: 3 additions & 6 deletions spec/rubocop/cop/performance/case_when_splat_spec.rb
Expand Up @@ -76,8 +76,7 @@
RUBY
end

it 'registers an offense for a single when with splat expansion followed ' \
'by another value' do
it 'registers an offense for a single when with splat expansion followed by another value' do
expect_offense(<<~RUBY)
case foo
when *Foo, Bar
Expand Down Expand Up @@ -166,8 +165,7 @@
RUBY
end

it 'registers an offense for a splat on a variable that proceeds a splat ' \
'on an array literal as the last condition' do
it 'registers an offense for a splat on a variable that proceeds a splat on an array literal as the last condition' do
expect_offense(<<~RUBY)
case foo
when *cond
Expand Down Expand Up @@ -293,8 +291,7 @@
RUBY
end

it 'moves multiple out of order splat condition to the end ' \
'of the when conditions' do
it 'moves multiple out of order splat condition to the end of the when conditions' do
new_source = autocorrect_source(<<~RUBY)
case foo
when *cond1
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/performance/casecmp_spec.rb
Expand Up @@ -112,8 +112,7 @@
RUBY
end

it 'registers an offense and corrects string with parens and funny spacing ' \
"eql? str.#{selector}" do
it "registers an offense and corrects string with parens and funny spacing eql? str.#{selector}" do
expect_offense(<<~RUBY, selector: selector)
( 'string' ).eql? str.#{selector}
^^^^^^^^^^^^^^^^^^^^^^^{selector} Use `str.casecmp( 'string' ).zero?` instead of `( 'string' ).eql? str.#{selector}`.
Expand Down

0 comments on commit 5c15788

Please sign in to comment.