Skip to content

Commit

Permalink
Auto-correct previously undiscovered offenses
Browse files Browse the repository at this point in the history
The bug fix in the previous commit makes the Layout/RedundantLineBreak
cop discover more offenses.
  • Loading branch information
jonas054 authored and bbatsov committed Mar 27, 2022
1 parent f94cd24 commit efaf1b0
Show file tree
Hide file tree
Showing 36 changed files with 81 additions and 185 deletions.
6 changes: 1 addition & 5 deletions lib/rubocop/cop/bundler/duplicated_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ def on_new_investigation

duplicated_gem_nodes.each do |nodes|
nodes[1..-1].each do |node|
register_offense(
node,
node.first_argument.to_a.first,
nodes.first.first_line
)
register_offense(node, node.first_argument.to_a.first, nodes.first.first_line)
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/rubocop/cop/gemspec/duplicated_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ def on_new_investigation

duplicated_assignment_method_nodes.each do |nodes|
nodes[1..-1].each do |node|
register_offense(
node,
node.method_name,
nodes.first.first_line
)
register_offense(node, node.method_name, nodes.first.first_line)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/layout/indentation_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ def select_check_member(member)

def check_members_for_indented_internal_methods_style(members)
each_member(members) do |member, previous_modifier|
check_indentation(previous_modifier, member,
indentation_consistency_style)
check_indentation(previous_modifier, member, indentation_consistency_style)
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/lint/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Syntax < Base
def on_other_file
add_offense_from_error(processed_source.parser_error) if processed_source.parser_error
processed_source.diagnostics.each do |diagnostic|
add_offense_from_diagnostic(diagnostic,
processed_source.ruby_version)
add_offense_from_diagnostic(diagnostic, processed_source.ruby_version)
end
super
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/metrics/utils/abc_size_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ def compound_assignment(node)
children = node.masgn_type? ? node.children[0].children : node.children

will_be_miscounted = children.count do |child|
child.respond_to?(:setter_method?) &&
!child.setter_method?
child.respond_to?(:setter_method?) && !child.setter_method?
end
@assignment += will_be_miscounted

Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ def assignment_rhs(node)

def not_for_this_cop?(node)
node.ancestors.any? do |ancestor|
grouped_expression?(ancestor) ||
inside_arg_list_parentheses?(node, ancestor)
grouped_expression?(ancestor) || inside_arg_list_parentheses?(node, ancestor)
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/style/empty_case_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def on_case(case_node)
branch_bodies = [*case_node.when_branches.map(&:body), case_node.else_branch].compact

return if branch_bodies.any? do |body|
body.return_type? ||
body.each_descendant.any?(&:return_type?)
body.return_type? || body.each_descendant.any?(&:return_type?)
end

add_offense(case_node.loc.keyword) { |corrector| autocorrect(corrector, case_node) }
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/style/redundant_capital_w.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def on_percent_literal(node)

def requires_interpolation?(node)
node.child_nodes.any? do |string|
string.dstr_type? ||
double_quotes_required?(string.source)
string.dstr_type? || double_quotes_required?(string.source)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/style/ternary_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ def infinite_loop?

def unsafe_autocorrect?(condition)
condition.children.any? do |child|
unparenthesized_method_call?(child) ||
below_ternary_precedence?(child)
unparenthesized_method_call?(child) || below_ternary_precedence?(child)
end
end

Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/style/trailing_method_end_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ def on_def(node)
return if node.endless? || !trailing_end?(node)

add_offense(node.loc.end) do |corrector|
corrector.insert_before(
node.loc.end,
"\n#{' ' * node.loc.keyword.column}"
)
corrector.insert_before(node.loc.end, "\n#{' ' * node.loc.keyword.column}")
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/formatter/offense_count_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def report_summary(offense_counts)
output.puts

per_cop_counts.each do |cop_name, count|
output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}" \
"#{cop_name}\n"
output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}#{cop_name}\n"
end
output.puts '--'
output.puts "#{total_count} Total"
Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/formatter/worst_offenders_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ def report_summary(offense_counts)
output.puts

per_file_counts.each do |file_name, count|
output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}" \
"#{file_name}\n"
output.puts "#{count.to_s.ljust(total_count.to_s.length + 2)}#{file_name}\n"
end
output.puts '--'
output.puts "#{total_count} Total"
Expand Down
5 changes: 1 addition & 4 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ def expected
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
3 changes: 1 addition & 2 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,7 @@ def verify_section

describe 'caching' do
let(:cache) do
instance_double(RuboCop::ResultCache, 'valid?' => true,
'load' => cached_offenses)
instance_double(RuboCop::ResultCache, 'valid?' => true, 'load' => cached_offenses)
end
let(:source) { %(puts "Hi"\n) }

Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cli/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,7 @@ def full_description_of_cop(cop)
context 'and offenses come from the cache' do
context 'and a message has binary encoding' do
let(:message_from_cache) do
(+'Cyclomatic complexity for 文 is too high. [8/6]')
.force_encoding('ASCII-8BIT')
(+'Cyclomatic complexity for 文 is too high. [8/6]').force_encoding('ASCII-8BIT')
end
let(:data_from_cache) do
[
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/commissioner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def method
let(:processed_source) { parse_source(source, 'file.rb') }
let(:cop_offenses) { [] }
let(:cop_report) do
RuboCop::Cop::Base::InvestigationReport
.new(nil, processed_source, cop_offenses, nil)
RuboCop::Cop::Base::InvestigationReport.new(nil, processed_source, cop_offenses, nil)
end

around { |example| RuboCop::Cop::Registry.with_temporary_global { example.run } }
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/cop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@
end

before do
allow(processed_source.comment_config).to receive(:cop_enabled_at_line?)
.and_return(false)
allow(processed_source.comment_config).to receive(:cop_enabled_at_line?).and_return(false)
end

context 'ignore_disable_comments is false' do
Expand Down
6 changes: 1 addition & 5 deletions spec/rubocop/cop/generator/require_file_injector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
let(:stdout) { StringIO.new }
let(:root_file_path) { 'lib/root.rb' }
let(:injector) do
described_class.new(
source_path: source_path,
root_file_path: root_file_path,
output: stdout
)
described_class.new(source_path: source_path, root_file_path: root_file_path, output: stdout)
end

around do |example|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ def some_method
['X =', '$x =', '@x =', 'x =', 'x +=', 'x ||='].each do |lhs_and_operator|
context "for assignment with #{lhs_and_operator}" do
let(:aligned_strings) do
[%(#{lhs_and_operator} "a" \\),
"#{' ' * lhs_and_operator.length} 'b'",
''].join("\n")
[%(#{lhs_and_operator} "a" \\), "#{' ' * lhs_and_operator.length} 'b'", ''].join("\n")
end

it 'accepts aligned strings' do
Expand Down Expand Up @@ -263,9 +261,7 @@ def some_method
['X =', '$x =', '@x =', 'x =', 'x +=', 'x ||='].each do |lhs_and_operator|
context "for assignment with #{lhs_and_operator}" do
let(:indented_strings) do
[%(#{lhs_and_operator} "a" \\),
" 'b'",
''].join("\n")
[%(#{lhs_and_operator} "a" \\), " 'b'", ''].join("\n")
end

it 'accepts indented strings' do
Expand Down
60 changes: 30 additions & 30 deletions spec/rubocop/cop/layout/redundant_line_break_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,39 +123,39 @@

it 'registers an offense for a method without parentheses on multiple lines' do
expect_offense(<<~RUBY)
def resolve_inheritance_from_gems(hash)
gems = hash.delete('inherit_gem')
(gems || {}).each_pair do |gem_name, config_path|
if gem_name == 'rubocop'
raise ArgumentError,
^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
"can't inherit configuration from the rubocop gem"
end
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
hash['inherit_from'].unshift gem_config_path(gem_name, path)
end
end
end
def resolve_inheritance_from_gems(hash)
gems = hash.delete('inherit_gem')
(gems || {}).each_pair do |gem_name, config_path|
if gem_name == 'rubocop'
raise ArgumentError,
^^^^^^^^^^^^^^^^^^^^ Redundant line break detected.
"can't inherit configuration from the rubocop gem"
end
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
hash['inherit_from'].unshift gem_config_path(gem_name, path)
end
end
end
RUBY

expect_correction(<<~RUBY)
def resolve_inheritance_from_gems(hash)
gems = hash.delete('inherit_gem')
(gems || {}).each_pair do |gem_name, config_path|
if gem_name == 'rubocop'
raise ArgumentError, "can't inherit configuration from the rubocop gem"
end
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
hash['inherit_from'].unshift gem_config_path(gem_name, path)
end
end
end
def resolve_inheritance_from_gems(hash)
gems = hash.delete('inherit_gem')
(gems || {}).each_pair do |gem_name, config_path|
if gem_name == 'rubocop'
raise ArgumentError, "can't inherit configuration from the rubocop gem"
end
hash['inherit_from'] = Array(hash['inherit_from'])
Array(config_path).reverse_each do |path|
# Put gem configuration first so local configuration overrides it.
hash['inherit_from'].unshift gem_config_path(gem_name, path)
end
end
end
RUBY
end
end
Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cop/lint/multiple_comparison_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
end

%w[< > <= >=].repeated_permutation(2) do |operator1, operator2|
include_examples 'Check to use two comparison operator',
operator1, operator2
include_examples 'Check to use two comparison operator', operator1, operator2
end

it 'accepts to use one compare operator' do
Expand Down
12 changes: 2 additions & 10 deletions spec/rubocop/cop/lint/void_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@

context 'when checking for methods with no side effects' do
let(:config) do
RuboCop::Config.new(
'Lint/Void' => {
'CheckForMethodsWithNoSideEffects' => true
}
)
RuboCop::Config.new('Lint/Void' => { 'CheckForMethodsWithNoSideEffects' => true })
end

it 'registers an offense if not on last line' do
Expand All @@ -129,11 +125,7 @@

context 'when not checking for methods with no side effects' do
let(:config) do
RuboCop::Config.new(
'Lint/Void' => {
'CheckForMethodsWithNoSideEffects' => false
}
)
RuboCop::Config.new('Lint/Void' => { 'CheckForMethodsWithNoSideEffects' => false })
end

it 'does not register an offense for void nonmutating methods' do
Expand Down
10 changes: 2 additions & 8 deletions spec/rubocop/cop/message_annotator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@

context 'when StyleGuide is set in the config' do
let(:config) do
RuboCop::Config.new(
'Cop/Cop' => { 'StyleGuide' => 'http://example.org/styleguide' }
)
RuboCop::Config.new('Cop/Cop' => { 'StyleGuide' => 'http://example.org/styleguide' })
end

it 'adds style guide url' do
Expand Down Expand Up @@ -158,11 +156,7 @@
describe '#urls' do
let(:urls) { annotator.urls }
let(:config) do
RuboCop::Config.new(
'AllCops' => {
'StyleGuideBaseURL' => 'http://example.org/styleguide'
}
)
RuboCop::Config.new('AllCops' => { 'StyleGuideBaseURL' => 'http://example.org/styleguide' })
end

it 'returns an empty array without StyleGuide URL' do
Expand Down
20 changes: 4 additions & 16 deletions spec/rubocop/cop/naming/file_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,7 @@ module A

context 'when CheckDefinitionPathHierarchy is false' do
let(:cop_config) do
super().merge(
'ExpectMatchingDefinition' => true,
'CheckDefinitionPathHierarchy' => false
)
super().merge('ExpectMatchingDefinition' => true, 'CheckDefinitionPathHierarchy' => false)
end

context 'on a file with a matching class' do
Expand Down Expand Up @@ -417,10 +414,7 @@ module Foo

context 'with acronym namespace' do
let(:cop_config) do
super().merge(
'ExpectMatchingDefinition' => true,
'AllowedAcronyms' => ['CLI']
)
super().merge('ExpectMatchingDefinition' => true, 'AllowedAcronyms' => ['CLI'])
end

it 'does not register an offense' do
Expand All @@ -437,10 +431,7 @@ class AdminUser

context 'with acronym class name' do
let(:cop_config) do
super().merge(
'ExpectMatchingDefinition' => true,
'AllowedAcronyms' => ['CLI']
)
super().merge('ExpectMatchingDefinition' => true, 'AllowedAcronyms' => ['CLI'])
end

it 'does not register an offense' do
Expand All @@ -455,10 +446,7 @@ class CLI

context 'with include acronym name' do
let(:cop_config) do
super().merge(
'ExpectMatchingDefinition' => true,
'AllowedAcronyms' => ['HTTP']
)
super().merge('ExpectMatchingDefinition' => true, 'AllowedAcronyms' => ['HTTP'])
end

it 'does not register an offense' do
Expand Down
Loading

0 comments on commit efaf1b0

Please sign in to comment.