From c907e1c61650b8cc19409efc8f33e84f7f8e620c Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 30 Dec 2023 08:47:50 +1300 Subject: [PATCH] [Fix #287] don't error on empty blocks when linting `assert_raises` --- changelog/fix_don_t_error_on_empty_blocks_when_linting.md | 1 + lib/rubocop/cop/minitest/assert_raises_compound_body.rb | 2 +- .../cop/minitest/assert_raises_compound_body_test.rb | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_don_t_error_on_empty_blocks_when_linting.md diff --git a/changelog/fix_don_t_error_on_empty_blocks_when_linting.md b/changelog/fix_don_t_error_on_empty_blocks_when_linting.md new file mode 100644 index 00000000..9b0ecf23 --- /dev/null +++ b/changelog/fix_don_t_error_on_empty_blocks_when_linting.md @@ -0,0 +1 @@ +* [#287](https://github.com/rubocop/rubocop-minitest/issues/287): Don't error on empty blocks when linting `assert_raises`. ([@G-Rath][]) diff --git a/lib/rubocop/cop/minitest/assert_raises_compound_body.rb b/lib/rubocop/cop/minitest/assert_raises_compound_body.rb index 3aac8fae..16156a11 100644 --- a/lib/rubocop/cop/minitest/assert_raises_compound_body.rb +++ b/lib/rubocop/cop/minitest/assert_raises_compound_body.rb @@ -37,7 +37,7 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler private def multi_statement_begin?(node) - node.begin_type? && node.children.size > 1 + node&.begin_type? && node.children.size > 1 end end end diff --git a/test/rubocop/cop/minitest/assert_raises_compound_body_test.rb b/test/rubocop/cop/minitest/assert_raises_compound_body_test.rb index 1128c6e3..91ba0773 100644 --- a/test/rubocop/cop/minitest/assert_raises_compound_body_test.rb +++ b/test/rubocop/cop/minitest/assert_raises_compound_body_test.rb @@ -13,6 +13,14 @@ def test_registers_offense_when_multi_statement_bodies RUBY end + def test_does_not_register_offense_when_empty_bodies + assert_no_offenses(<<~RUBY) + assert_raises(MyError) do + # nothing to see here... + end + RUBY + end + def test_does_not_register_offense_when_single_statement_bodies assert_no_offenses(<<~RUBY) assert_raises(MyError) do