Skip to content

Commit

Permalink
Fix Lint/Void cop for __ENCODING__ constant
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jun 30, 2023
1 parent 4f83077 commit c7821e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog/fix_lint_void_cop_for_encoding_constant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12002](https://github.com/rubocop/rubocop/pull/12002): Fix `Lint/Void` cop for `__ENCODING__` constant. ([@fatkodima][])
28 changes: 11 additions & 17 deletions lib/rubocop/cop/lint/void.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,31 @@ def check_void_op(node)
def check_var(node)
return unless node.variable? || node.const_type?

add_offense(node.loc.name,
message: format(VAR_MSG, var: node.loc.name.source)) do |corrector|
autocorrect_void_var(corrector, node)
if node.const_type? && node.special_keyword?
add_offense(node, message: format(VAR_MSG, var: node.source)) do |corrector|
autocorrect_void_expression(corrector, node)
end
else
add_offense(node.loc.name,
message: format(VAR_MSG, var: node.loc.name.source)) do |corrector|
autocorrect_void_expression(corrector, node)
end
end
end

def check_literal(node)
return if !node.literal? || node.xstr_type? || node.range_type?

add_offense(node, message: format(LIT_MSG, lit: node.source)) do |corrector|
autocorrect_void_literal(corrector, node)
autocorrect_void_expression(corrector, node)
end
end

def check_self(node)
return unless node.self_type?

add_offense(node, message: SELF_MSG) do |corrector|
autocorrect_void_self(corrector, node)
autocorrect_void_expression(corrector, node)
end
end

Expand Down Expand Up @@ -181,18 +187,6 @@ def autocorrect_void_op(corrector, node)
end
end

def autocorrect_void_var(corrector, node)
corrector.remove(range_with_surrounding_space(range: node.loc.name, side: :left))
end

def autocorrect_void_literal(corrector, node)
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
end

def autocorrect_void_self(corrector, node)
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
end

def autocorrect_void_expression(corrector, node)
corrector.remove(range_with_surrounding_space(range: node.source_range, side: :left))
end
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/lint/void_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,20 @@ def foo
end
RUBY
end

it 'registers offense when using special __ENCODING__' do
expect_offense(<<~RUBY)
def foo
__ENCODING__
^^^^^^^^^^^^ Variable `__ENCODING__` used in void context.
42
end
RUBY

expect_correction(<<~RUBY)
def foo
42
end
RUBY
end
end

0 comments on commit c7821e2

Please sign in to comment.