From e9f1e591878ac6f8a4e34b4d332da8a85f745aaa Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Fri, 20 Oct 2023 16:20:46 -0400 Subject: [PATCH] Ignore offenses inside redundant parentheses rubocop/rubocop#11739 updated `Style/RedundantParentheses` to detect this, so we can now consider it out of scope of these cops, as was done in https://github.com/rubocop/rubocop-minitest/pull/248/files#r1150046949. --- ...change_ignore_offenses_inside_redundant.md | 1 + lib/rubocop/cop/minitest/assert_empty.rb | 9 +++---- lib/rubocop/cop/minitest/refute_empty.rb | 9 +++---- lib/rubocop/cop/mixin/minitest_cop_rule.rb | 25 ++++--------------- .../mixin/predicate_assertion_handleable.rb | 22 +++++----------- .../rubocop/cop/minitest/assert_empty_test.rb | 14 +++-------- .../cop/minitest/assert_includes_test.rb | 14 +++-------- .../cop/minitest/assert_predicate_test.rb | 14 +++-------- .../cop/minitest/assert_respond_to_test.rb | 12 ++------- .../rubocop/cop/minitest/refute_empty_test.rb | 12 ++------- .../cop/minitest/refute_includes_test.rb | 14 +++-------- .../rubocop/cop/minitest/refute_match_test.rb | 2 +- .../cop/minitest/refute_predicate_test.rb | 14 +++-------- .../cop/minitest/refute_respond_to_test.rb | 14 +++-------- 14 files changed, 43 insertions(+), 133 deletions(-) create mode 100644 changelog/change_ignore_offenses_inside_redundant.md diff --git a/changelog/change_ignore_offenses_inside_redundant.md b/changelog/change_ignore_offenses_inside_redundant.md new file mode 100644 index 00000000..dc16f2be --- /dev/null +++ b/changelog/change_ignore_offenses_inside_redundant.md @@ -0,0 +1 @@ +* [#270](https://github.com/rubocop/rubocop-minitest/pull/270): Ignore offenses inside redundant parentheses. ([@sambostock][]) diff --git a/lib/rubocop/cop/minitest/assert_empty.rb b/lib/rubocop/cop/minitest/assert_empty.rb index 9091a080..d30828bb 100644 --- a/lib/rubocop/cop/minitest/assert_empty.rb +++ b/lib/rubocop/cop/minitest/assert_empty.rb @@ -22,12 +22,11 @@ class AssertEmpty < Base remove_method :on_send def on_send(node) return unless node.method?(:assert) - return unless (arguments = peel_redundant_parentheses_from(node.arguments)) - return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:empty?) - return unless arguments.first.arguments.empty? + return unless node.arguments.first.respond_to?(:method?) && node.arguments.first.method?(:empty?) + return unless node.arguments.first.arguments.empty? - add_offense(node, message: offense_message(arguments)) do |corrector| - autocorrect(corrector, node, arguments) + add_offense(node, message: offense_message(node.arguments)) do |corrector| + autocorrect(corrector, node, node.arguments) end end end diff --git a/lib/rubocop/cop/minitest/refute_empty.rb b/lib/rubocop/cop/minitest/refute_empty.rb index ed5dc2ac..eb4bd229 100644 --- a/lib/rubocop/cop/minitest/refute_empty.rb +++ b/lib/rubocop/cop/minitest/refute_empty.rb @@ -22,12 +22,11 @@ class RefuteEmpty < Base remove_method :on_send def on_send(node) return unless node.method?(:refute) - return unless (arguments = peel_redundant_parentheses_from(node.arguments)) - return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:empty?) - return unless arguments.first.arguments.empty? + return unless node.arguments.first.respond_to?(:method?) && node.arguments.first.method?(:empty?) + return unless node.arguments.first.arguments.empty? - add_offense(node, message: offense_message(arguments)) do |corrector| - autocorrect(corrector, node, arguments) + add_offense(node, message: offense_message(node.arguments)) do |corrector| + autocorrect(corrector, node, node.arguments) end end end diff --git a/lib/rubocop/cop/mixin/minitest_cop_rule.rb b/lib/rubocop/cop/mixin/minitest_cop_rule.rb index c1f3fe00..227bc28f 100644 --- a/lib/rubocop/cop/mixin/minitest_cop_rule.rb +++ b/lib/rubocop/cop/mixin/minitest_cop_rule.rb @@ -45,13 +45,12 @@ def define_rule(assertion_method, target_method:, preferred_method: nil, inverse def on_send(node) return unless node.method?(:#{assertion_method}) - return unless (arguments = peel_redundant_parentheses_from(node.arguments)) - return unless arguments.first&.call_type? - return if arguments.first.arguments.empty? || - #{target_methods}.none? { |target_method| arguments.first.method?(target_method) } + return unless node.arguments.first&.call_type? + return if node.arguments.first.arguments.empty? || + #{target_methods}.none? { |target_method| node.arguments.first.method?(target_method) } - add_offense(node, message: offense_message(arguments)) do |corrector| - autocorrect(corrector, node, arguments) + add_offense(node, message: offense_message(node.arguments)) do |corrector| + autocorrect(corrector, node, node.arguments) end end @@ -60,21 +59,11 @@ def autocorrect(corrector, node, arguments) new_arguments = new_arguments(arguments).join(', ') - if enclosed_in_redundant_parentheses?(node) - new_arguments = '(' + new_arguments + ')' - end - corrector.replace(node.first_argument, new_arguments) end private - def peel_redundant_parentheses_from(arguments) - return arguments unless arguments.first&.begin_type? - - peel_redundant_parentheses_from(arguments.first.children) - end - def offense_message(arguments) message_argument = arguments.last if arguments.first != arguments.last @@ -103,10 +92,6 @@ def new_arguments(arguments) new_arguments end - def enclosed_in_redundant_parentheses?(node) - node.arguments.first.begin_type? - end - def correct_receiver(receiver) receiver ? receiver.source : 'self' end diff --git a/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb b/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb index 59b41851..e52cc25c 100644 --- a/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb +++ b/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb @@ -9,17 +9,13 @@ module PredicateAssertionHandleable MSG = 'Prefer using `%s_predicate(%s)`.' def on_send(node) - return unless (arguments = peel_redundant_parentheses_from(node.arguments)) + return unless node.first_argument + return if node.first_argument.block_type? || node.first_argument.numblock_type? + return unless predicate_method?(node.first_argument) + return unless node.first_argument.arguments.count.zero? - first_argument = arguments.first - - return unless first_argument - return if first_argument.block_type? || first_argument.numblock_type? - return unless predicate_method?(first_argument) - return unless first_argument.arguments.count.zero? - - add_offense(node, message: offense_message(arguments)) do |corrector| - autocorrect(corrector, node, arguments) + add_offense(node, message: offense_message(node.arguments)) do |corrector| + autocorrect(corrector, node, node.arguments) end end @@ -33,12 +29,6 @@ def autocorrect(corrector, node, arguments) private - def peel_redundant_parentheses_from(arguments) - return arguments unless arguments.first&.begin_type? - - peel_redundant_parentheses_from(arguments.first.children) - end - def predicate_method?(first_argument) first_argument.respond_to?(:predicate_method?) && first_argument.predicate_method? end diff --git a/test/rubocop/cop/minitest/assert_empty_test.rb b/test/rubocop/cop/minitest/assert_empty_test.rb index 78df7617..25b1e7f3 100644 --- a/test/rubocop/cop/minitest/assert_empty_test.rb +++ b/test/rubocop/cop/minitest/assert_empty_test.rb @@ -66,20 +66,12 @@ def test_do_something RUBY end - def test_registers_offense_when_using_assert_with_empty_in_redundant_parentheses - assert_offense(<<~RUBY) + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. + def test_does_not_register_offense_when_using_assert_with_empty_in_redundant_parentheses + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something assert((somestuff.empty?)) - ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_empty(somestuff)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - assert_empty((somestuff)) end end RUBY diff --git a/test/rubocop/cop/minitest/assert_includes_test.rb b/test/rubocop/cop/minitest/assert_includes_test.rb index dc583f50..7f6d6fb2 100644 --- a/test/rubocop/cop/minitest/assert_includes_test.rb +++ b/test/rubocop/cop/minitest/assert_includes_test.rb @@ -66,20 +66,12 @@ def test_do_something RUBY end - def test_registers_offense_when_using_assert_with_include_in_redundant_parentheses - assert_offense(<<~RUBY) + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. + def test_does_not_register_offense_when_using_assert_with_include_in_redundant_parentheses + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something assert((collection.include?(object))) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_includes(collection, object)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - assert_includes((collection, object)) end end RUBY diff --git a/test/rubocop/cop/minitest/assert_predicate_test.rb b/test/rubocop/cop/minitest/assert_predicate_test.rb index b3e6362d..8c4fe557 100644 --- a/test/rubocop/cop/minitest/assert_predicate_test.rb +++ b/test/rubocop/cop/minitest/assert_predicate_test.rb @@ -64,20 +64,12 @@ def test_do_something RUBY end - def test_registers_offense_when_using_assert_with_predicate_method_in_redundant_parentheses - assert_offense(<<~RUBY) + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. + def test_does_not_register_offense_when_using_assert_with_predicate_method_in_redundant_parentheses + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something assert((obj.one?)) - ^^^^^^^^^^^^^^^^^^ Prefer using `assert_predicate(obj, :one?)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - assert_predicate(obj, :one?) end end RUBY diff --git a/test/rubocop/cop/minitest/assert_respond_to_test.rb b/test/rubocop/cop/minitest/assert_respond_to_test.rb index b6445f00..2c9c6ebf 100644 --- a/test/rubocop/cop/minitest/assert_respond_to_test.rb +++ b/test/rubocop/cop/minitest/assert_respond_to_test.rb @@ -85,20 +85,12 @@ def test_do_something RUBY end + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. def test_registers_offense_when_using_assert_with_respond_to_in_redundant_parentheses - assert_offense(<<~RUBY) + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something assert((object.respond_to?(:do_something))) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_respond_to(object, :do_something)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - assert_respond_to((object, :do_something)) end end RUBY diff --git a/test/rubocop/cop/minitest/refute_empty_test.rb b/test/rubocop/cop/minitest/refute_empty_test.rb index 5aad10f7..bd452efb 100644 --- a/test/rubocop/cop/minitest/refute_empty_test.rb +++ b/test/rubocop/cop/minitest/refute_empty_test.rb @@ -66,20 +66,12 @@ def test_do_something RUBY end + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. def test_registers_offense_when_using_refute_with_empty_in_redundant_parentheses - assert_offense(<<~RUBY) + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something refute((somestuff.empty?)) - ^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - refute_empty((somestuff)) end end RUBY diff --git a/test/rubocop/cop/minitest/refute_includes_test.rb b/test/rubocop/cop/minitest/refute_includes_test.rb index 69a3ebfc..754cff65 100644 --- a/test/rubocop/cop/minitest/refute_includes_test.rb +++ b/test/rubocop/cop/minitest/refute_includes_test.rb @@ -66,20 +66,12 @@ def test_do_something RUBY end - def test_registers_offense_when_using_refute_with_include_in_redundant_parentheses - assert_offense(<<~RUBY) + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. + def test_does_not_register_offense_when_using_refute_with_include_in_redundant_parentheses + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something refute((collection.include?(object))) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_includes(collection, object)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - refute_includes((collection, object)) end end RUBY diff --git a/test/rubocop/cop/minitest/refute_match_test.rb b/test/rubocop/cop/minitest/refute_match_test.rb index 05cbf947..bd5936f2 100644 --- a/test/rubocop/cop/minitest/refute_match_test.rb +++ b/test/rubocop/cop/minitest/refute_match_test.rb @@ -106,7 +106,7 @@ def test_do_something end # Redundant parentheses should be removed in another cop. - define_method("test_registers_offense_when_using_refute_with_#{matcher}_in_redundant_parentheses") do + define_method("test_does_not_register_offense_when_using_refute_with_#{matcher}_in_redundant_parentheses") do assert_no_offenses(<<~RUBY, matcher: matcher) class FooTest < Minitest::Test def test_do_something diff --git a/test/rubocop/cop/minitest/refute_predicate_test.rb b/test/rubocop/cop/minitest/refute_predicate_test.rb index 0a509acc..83cd099f 100644 --- a/test/rubocop/cop/minitest/refute_predicate_test.rb +++ b/test/rubocop/cop/minitest/refute_predicate_test.rb @@ -64,20 +64,12 @@ def test_do_something RUBY end - def test_registers_offense_when_using_refute_with_predicate_method_in_redundant_parentheses - assert_offense(<<~RUBY) + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. + def test_does_not_register_offense_when_using_refute_with_predicate_method_in_redundant_parentheses + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something refute((obj.one?)) - ^^^^^^^^^^^^^^^^^^ Prefer using `refute_predicate(obj, :one?)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - refute_predicate(obj, :one?) end end RUBY diff --git a/test/rubocop/cop/minitest/refute_respond_to_test.rb b/test/rubocop/cop/minitest/refute_respond_to_test.rb index 7af55e93..69b90d20 100644 --- a/test/rubocop/cop/minitest/refute_respond_to_test.rb +++ b/test/rubocop/cop/minitest/refute_respond_to_test.rb @@ -85,20 +85,12 @@ def test_do_something RUBY end - def test_registers_offense_when_using_refute_with_respond_to_in_redundant_parentheses - assert_offense(<<~RUBY) + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. + def test_does_not_register_offense_when_using_refute_with_respond_to_in_redundant_parentheses + assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test def test_do_something refute((object.respond_to?(:do_something))) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_respond_to(object, :do_something)`. - end - end - RUBY - - assert_correction(<<~RUBY) - class FooTest < Minitest::Test - def test_do_something - refute_respond_to((object, :do_something)) end end RUBY