From 40cbe03f8c35541aa2798b0e16d42f5baa84204c Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 28 Sep 2022 01:56:25 +0900 Subject: [PATCH] Fix an incorrect autocorrect for `Minitest/RefuteMatch` Follow up #181 and #184. This PR fixes an incorrect autocorrect for `Minitest/AssertMatch` when `refute` with `match` and RHS is a regexp literal. A regular expression literal must be the first argument to `refute_match`. `TypeError: no implicit conversion of Regexp into String` will occur if it is passed as the second argument. ```ruby refute_match(object, /regexp/) #=> TypeError: no implicit conversion of Regexp into String ``` --- ...t_autocorrect_for_minitest_refute_match.md | 1 + lib/rubocop/cop/minitest/refute_match.rb | 2 +- .../rubocop/cop/minitest/refute_match_test.rb | 58 +++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_incorrect_autocorrect_for_minitest_refute_match.md diff --git a/changelog/fix_an_incorrect_autocorrect_for_minitest_refute_match.md b/changelog/fix_an_incorrect_autocorrect_for_minitest_refute_match.md new file mode 100644 index 00000000..10e76e22 --- /dev/null +++ b/changelog/fix_an_incorrect_autocorrect_for_minitest_refute_match.md @@ -0,0 +1 @@ +* [#185](https://github.com/rubocop/rubocop-minitest/pull/185): Fix an incorrect autocorrect for `Minitest/RefuteMatch` when `refute` with `match` and RHS is a regexp literal. ([@koic][]) diff --git a/lib/rubocop/cop/minitest/refute_match.rb b/lib/rubocop/cop/minitest/refute_match.rb index 797c192c..9fa165d9 100644 --- a/lib/rubocop/cop/minitest/refute_match.rb +++ b/lib/rubocop/cop/minitest/refute_match.rb @@ -18,7 +18,7 @@ module Minitest class RefuteMatch < Base extend MinitestCopRule - define_rule :refute, target_method: :match + define_rule :refute, target_method: :match, inverse: 'regexp_type?' end end end diff --git a/test/rubocop/cop/minitest/refute_match_test.rb b/test/rubocop/cop/minitest/refute_match_test.rb index 8e8799ed..a6178a6e 100644 --- a/test/rubocop/cop/minitest/refute_match_test.rb +++ b/test/rubocop/cop/minitest/refute_match_test.rb @@ -22,6 +22,44 @@ def test_do_something RUBY end + def test_registers_offense_when_using_refute_with_match_and_lhs_is_regexp_literal + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute(/regexp/.match(object)) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_match(/regexp/, object)`. + end + end + RUBY + + assert_correction(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_match(/regexp/, object) + end + end + RUBY + end + + def test_registers_offense_when_using_refute_with_match_and_rhs_is_regexp_literal + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute(object.match(/regexp/)) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_match(/regexp/, object)`. + end + end + RUBY + + assert_correction(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_match(/regexp/, object) + end + end + RUBY + end + def test_registers_offense_when_using_refute_with_match_and_message assert_offense(<<~RUBY) class FooTest < Minitest::Test @@ -94,4 +132,24 @@ def test_do_something end RUBY end + + def test_does_not_register_offense_when_using_refute_with_no_arguments_match_call + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute(matcher.match) + end + end + RUBY + end + + def test_does_not_register_offense_when_using_refute_with_no_arguments_match_safe_navigation_call + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute(matcher&.match) + end + end + RUBY + end end