Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Lint/UselessAccessModifier aware of Ruby 3.2's Data.define #11546

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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