diff --git a/lib/rubocop/cop/internal_affairs.rb b/lib/rubocop/cop/internal_affairs.rb index 31e295a01be7..6a2b399db3a9 100644 --- a/lib/rubocop/cop/internal_affairs.rb +++ b/lib/rubocop/cop/internal_affairs.rb @@ -20,6 +20,7 @@ require_relative 'internal_affairs/processed_source_buffer_name' require_relative 'internal_affairs/redundant_context_config_parameter' require_relative 'internal_affairs/redundant_described_class_as_subject' +require_relative 'internal_affairs/redundant_expect_offense_arguments' require_relative 'internal_affairs/redundant_let_rubocop_config_new' require_relative 'internal_affairs/redundant_location_argument' require_relative 'internal_affairs/redundant_message_argument' diff --git a/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb b/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb new file mode 100644 index 000000000000..4424cb07c491 --- /dev/null +++ b/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module InternalAffairs + # Checks for redundant arguments of `RuboCop::RSpec::ExpectOffense`'s methods. + # + # @example + # + # # bad + # expect_no_offenses('code', keyword: keyword) + # + # # good + # expect_no_offenses('code') + # + class RedundantExpectOffenseArguments < Base + extend AutoCorrector + + MSG = 'Remove the redundant arguments.' + RESTRICT_ON_SEND = %i[expect_no_offenses].freeze + + def on_send(node) + return if node.arguments.one? || !node.arguments[1]&.hash_type? + + range = node.first_argument.source_range.end.join(node.last_argument.source_range.end) + + add_offense(range) do |corrector| + corrector.remove(range) + end + end + end + end + end +end diff --git a/spec/rubocop/cop/internal_affairs/redundant_expect_offense_arguments_spec.rb b/spec/rubocop/cop/internal_affairs/redundant_expect_offense_arguments_spec.rb new file mode 100644 index 000000000000..6431d8e2cf2d --- /dev/null +++ b/spec/rubocop/cop/internal_affairs/redundant_expect_offense_arguments_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +RSpec.describe RuboCop::Cop::InternalAffairs::RedundantExpectOffenseArguments, :config do + it 'registers an offense when using `expect_no_offenses` with string and single keyword arguments' do + expect_offense(<<~RUBY) + expect_no_offenses('code', keyword: keyword) + ^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. + RUBY + + expect_correction(<<~RUBY) + expect_no_offenses('code') + RUBY + end + + it 'registers an offense when using `expect_no_offenses` with heredoc and single keyword arguments' do + expect_offense(<<~RUBY) + expect_no_offenses(<<~CODE, keyword: keyword) + ^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. + CODE + RUBY + + expect_correction(<<~RUBY) + expect_no_offenses(<<~CODE) + CODE + RUBY + end + + it 'registers an offense when using `expect_no_offenses` with heredoc multiple keyword arguments' do + expect_offense(<<~RUBY) + expect_no_offenses(<<~CODE, foo: foo, bar: bar) + ^^^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. + CODE + RUBY + + expect_correction(<<~RUBY) + expect_no_offenses(<<~CODE) + CODE + RUBY + end + + it 'does not register an offense when using `expect_no_offenses` with string argument only' do + expect_no_offenses(<<~RUBY) + expect_no_offenses('code') + RUBY + end + + it 'does not register an offense when using `expect_no_offenses` with heredoc argument only' do + expect_no_offenses(<<~RUBY) + expect_no_offenses(<<~CODE) + CODE + RUBY + end + + it 'does not register an offense when using `expect_no_offenses` with string and positional arguments' do + expect_no_offenses(<<~RUBY) + expect_no_offenses('code', file) + RUBY + end + + it 'does not register an offense when using `expect_no_offenses` with heredoc and positional arguments' do + expect_no_offenses(<<~RUBY) + expect_no_offenses(<<~CODE, file) + CODE + RUBY + end + + it 'does not crash when using `expect_no_offenses` with no arguments' do + expect_no_offenses(<<~RUBY) + expect_no_offenses + CODE + RUBY + end +end diff --git a/spec/rubocop/cop/lint/literal_as_condition_spec.rb b/spec/rubocop/cop/lint/literal_as_condition_spec.rb index 3b4044ff1a02..e7df23557e84 100644 --- a/spec/rubocop/cop/lint/literal_as_condition_spec.rb +++ b/spec/rubocop/cop/lint/literal_as_condition_spec.rb @@ -76,7 +76,7 @@ context '>= Ruby 2.7', :ruby27 do it "accepts an offense for literal #{lit} in case match with a match var" do - expect_no_offenses(<<~RUBY, lit: lit) + expect_no_offenses(<<~RUBY) case %{lit} in x then top end diff --git a/spec/rubocop/cop/lint/non_atomic_file_operation_spec.rb b/spec/rubocop/cop/lint/non_atomic_file_operation_spec.rb index 724f662bbcb7..4b833c2ba1e5 100644 --- a/spec/rubocop/cop/lint/non_atomic_file_operation_spec.rb +++ b/spec/rubocop/cop/lint/non_atomic_file_operation_spec.rb @@ -89,7 +89,7 @@ %i[rm_r rmtree].each do |remove_method| it 'does not register an offense when use `FileTest.exist?` before remove recursive file' do - expect_no_offenses(<<~RUBY, remove_method: remove_method) + expect_no_offenses(<<~RUBY) if FileTest.exist?(path) FileUtils.#{remove_method}(path) end diff --git a/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb b/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb index bffe46667320..220eb3956acc 100644 --- a/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb +++ b/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb @@ -598,13 +598,13 @@ context 'argument count' do it 'ignores when there are not enough block arguments' do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) (1..4).#{method}(0) { |acc| acc.foo } RUBY end it 'ignores when there is a splat argument' do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) values.#{method}(0) { |*x| x[0] + x[1] } RUBY end diff --git a/spec/rubocop/cop/style/access_modifier_declarations_spec.rb b/spec/rubocop/cop/style/access_modifier_declarations_spec.rb index d4581c2e9e2c..2e75a5accd5e 100644 --- a/spec/rubocop/cop/style/access_modifier_declarations_spec.rb +++ b/spec/rubocop/cop/style/access_modifier_declarations_spec.rb @@ -153,7 +153,7 @@ def foo; end end it 'does not register an offense when using #{access_modifier} in a block' do - expect_no_offenses(<<~RUBY, access_modifier: access_modifier) + expect_no_offenses(<<~RUBY) module MyModule singleton_methods.each { |method| #{access_modifier}(method) } end diff --git a/spec/rubocop/cop/style/format_string_token_spec.rb b/spec/rubocop/cop/style/format_string_token_spec.rb index f8d6c902d1ea..e10fd6700c7a 100644 --- a/spec/rubocop/cop/style/format_string_token_spec.rb +++ b/spec/rubocop/cop/style/format_string_token_spec.rb @@ -147,7 +147,7 @@ end else it 'does not register offenses for annotated style' do - expect_no_offenses(<<~RUBY, annotated: annotated, template: template) + expect_no_offenses(<<~RUBY) <<-HEREDOC foo %{template} + bar %{annotated} HEREDOC @@ -166,7 +166,7 @@ "a\#{b}#{template} c\#{d}#{annotated_to_template} e\#{f}" RUBY else - expect_no_offenses(<<~'RUBY', annotated: annotated, template: template) + expect_no_offenses(<<~'RUBY') "a#{b}%{template} c#{d}%{annotated} e#{f}" RUBY end diff --git a/spec/rubocop/cop/style/map_to_hash_spec.rb b/spec/rubocop/cop/style/map_to_hash_spec.rb index 643259239fd9..b7292a988fce 100644 --- a/spec/rubocop/cop/style/map_to_hash_spec.rb +++ b/spec/rubocop/cop/style/map_to_hash_spec.rb @@ -176,7 +176,7 @@ context "`#{method}` without `to_h`" do it 'does not register an offense' do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) foo.#{method} { |x| x * 2 } RUBY end diff --git a/spec/rubocop/cop/style/map_to_set_spec.rb b/spec/rubocop/cop/style/map_to_set_spec.rb index 18b1f7a90fe4..bbbda81591f5 100644 --- a/spec/rubocop/cop/style/map_to_set_spec.rb +++ b/spec/rubocop/cop/style/map_to_set_spec.rb @@ -110,7 +110,7 @@ context "`#{method}` without `to_set`" do it 'does not register an offense' do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) foo.#{method} { |x| x * 2 } RUBY end diff --git a/spec/rubocop/cop/style/symbol_proc_spec.rb b/spec/rubocop/cop/style/symbol_proc_spec.rb index c232f0b2bdbc..ee595e3e36c0 100644 --- a/spec/rubocop/cop/style/symbol_proc_spec.rb +++ b/spec/rubocop/cop/style/symbol_proc_spec.rb @@ -174,7 +174,7 @@ end it "does not register an offense when receiver is a hash literal and using `#{method}` with a block" do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) {foo: 42}.#{method} {|item| item.foo } RUBY end @@ -193,7 +193,7 @@ end it "does not register an offense when receiver is a array literal and using `#{method}` with a block" do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) [1, 2, 3].#{method} {|item| item.foo } RUBY end @@ -372,7 +372,7 @@ end it "does not register an offense when receiver is a hash literal and using `#{method}` with a numblock" do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) {foo: 42}.#{method} { _1.foo } RUBY end @@ -391,7 +391,7 @@ end it "does not register an offense when receiver is a array literal and using `#{method}` with a numblock" do - expect_no_offenses(<<~RUBY, method: method) + expect_no_offenses(<<~RUBY) [1, 2, 3].#{method} { _1.foo } RUBY end