Skip to content

Commit

Permalink
Make Lint/UselessAccessModifier aware of Ruby 3.2's Data.define
Browse files Browse the repository at this point in the history
Ruby 3.2 introduced immutable `Data.define`. Since this is similar to `Struct.new`.
This `Lint/UselessAccessModifier` will handle `Struct.new` and `Data.define` in the same way.
  • Loading branch information
koic committed Feb 7, 2023
1 parent 626e9a0 commit a13587b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11546](https://github.com/rubocop/rubocop/pull/11546): Make `Lint/UselessAccessModifier` aware of Ruby 3.2's `Data.define`. ([@koic][])
11 changes: 7 additions & 4 deletions lib/rubocop/cop/lint/useless_access_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,12 @@ def autocorrect(corrector, node)
({block numblock} (send _ {:class_eval :instance_eval}) ...)
PATTERN

# @!method class_or_module_or_struct_new_call?(node)
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
({block numblock} (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
# @!method class_constructor?(node)
def_node_matcher :class_constructor?, <<~PATTERN
({block numblock} {
(send (const {nil? cbase} {:Class :Module :Struct}) :new ...)
(send (const {nil? cbase} :Data) :define ...)
} ...)
PATTERN

def check_node(node)
Expand Down Expand Up @@ -270,7 +273,7 @@ def start_of_new_scope?(child)

def eval_call?(child)
class_or_instance_eval?(child) ||
class_or_module_or_struct_new_call?(child) ||
class_constructor?(child) ||
any_context_creating_methods?(child)
end

Expand Down
42 changes: 42 additions & 0 deletions spec/rubocop/cop/lint/useless_access_modifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,48 @@ def method1
end
end

context '`def` in `Data.define` block', :ruby32 do
%w[protected private].each do |modifier|
it "doesn't register an offense if a method is defined in `Data.define` with block" do
expect_no_offenses(<<~RUBY)
Data.define do
#{modifier}
def foo
end
end
RUBY
end

it 'registers an offense if no method is defined in `Data.define` with block' do
expect_offense(<<~RUBY, modifier: modifier)
Data.define do
%{modifier}
^{modifier} Useless `#{modifier}` access modifier.
end
RUBY
end

it 'registers an offense if no method is defined in `::Data.define` with block' do
expect_offense(<<~RUBY, modifier: modifier)
::Data.define do
%{modifier}
^{modifier} Useless `#{modifier}` access modifier.
end
RUBY
end

it 'registers an offense if no method is defined in `Data.define` with numblock' do
expect_offense(<<~RUBY, modifier: modifier)
Data.define do
%{modifier}
^{modifier} Useless `#{modifier}` access modifier.
do_something(_1)
end
RUBY
end
end
end

%w[module class].each do |keyword|
it_behaves_like('at the top of the body', keyword)
it_behaves_like('non-repeated visibility modifiers', keyword)
Expand Down

0 comments on commit a13587b

Please sign in to comment.