From 3a542a96fb59c254d68348c54e04af8fdcab7b4e Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 21 Jan 2023 00:50:05 +0900 Subject: [PATCH] Fix an error for `Style/Semicolon` This PR fixes the following error for `Style/Semicolon` when using a comment containing a semicolon before a block: ```ruby # ; foo { } ``` ```console % bundle exec rubocop --only Style/Semicolon (snip) NoMethodError: undefined method `semicolon?' for nil:NilClass ``` No changelog entry because this is a bug by #11465 and not released. --- lib/rubocop/cop/style/semicolon.rb | 2 +- spec/rubocop/cop/style/semicolon_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rubocop/cop/style/semicolon.rb b/lib/rubocop/cop/style/semicolon.rb index ce2709d33681..5b4dee95cff3 100644 --- a/lib/rubocop/cop/style/semicolon.rb +++ b/lib/rubocop/cop/style/semicolon.rb @@ -100,7 +100,7 @@ def exist_semicolon_before_right_curly_brace?(tokens) end def exist_semicolon_after_left_curly_brace?(tokens) - tokens[1]&.left_curly_brace? && tokens[2].semicolon? + tokens[1]&.left_curly_brace? && tokens[2]&.semicolon? end def register_semicolon(line, column, after_expression, token_before_semicolon = nil) diff --git a/spec/rubocop/cop/style/semicolon_spec.rb b/spec/rubocop/cop/style/semicolon_spec.rb index b7e1b00ae284..927b22110798 100644 --- a/spec/rubocop/cop/style/semicolon_spec.rb +++ b/spec/rubocop/cop/style/semicolon_spec.rb @@ -142,6 +142,14 @@ module Foo; end RUBY end + it 'does not register an offense when using a comment containing a semicolon before a block' do + expect_no_offenses(<<~RUBY) + # ; + foo { + } + RUBY + end + it 'registers an offense for range (`1..42`) with semicolon' do expect_offense(<<~RUBY) 1..42;