diff --git a/changelog/fix_error_skip_ensure.md b/changelog/fix_error_skip_ensure.md new file mode 100644 index 0000000..6ee410f --- /dev/null +++ b/changelog/fix_error_skip_ensure.md @@ -0,0 +1 @@ +* [#314](https://github.com/rubocop/rubocop-minitest/pull/314): Fix an error for `Minitest/SkipEnsure` when only `ensure` has a body. ([@earlopain][]) diff --git a/lib/rubocop/cop/minitest/skip_ensure.rb b/lib/rubocop/cop/minitest/skip_ensure.rb index 8a52c11..9ff6bd2 100644 --- a/lib/rubocop/cop/minitest/skip_ensure.rb +++ b/lib/rubocop/cop/minitest/skip_ensure.rb @@ -75,7 +75,9 @@ def on_ensure(node) private def find_skip(node) - node.node_parts.first.descendants.detect { |n| n.send_type? && n.receiver.nil? && n.method?(:skip) } + return unless (body = node.node_parts.first) + + body.descendants.detect { |n| n.send_type? && n.receiver.nil? && n.method?(:skip) } end def use_skip_in_rescue?(skip_method) diff --git a/test/rubocop/cop/minitest/skip_ensure_test.rb b/test/rubocop/cop/minitest/skip_ensure_test.rb index e80be68..dc2ccd9 100644 --- a/test/rubocop/cop/minitest/skip_ensure_test.rb +++ b/test/rubocop/cop/minitest/skip_ensure_test.rb @@ -119,4 +119,13 @@ def test_skip_with_receiver end RUBY end + + def test_registers_no_offese_when_ensure_only + assert_no_offenses(<<~RUBY) + def test_ensure_only + ensure + do_something + end + RUBY + end end