Skip to content

Commit a21f4a3

Browse files
Use ::Struct to avoid collisions with cop namespace
We were trying to add a custom cop called `Struct/RequiresTypedStructHelper`, but ran into a problem where we got an error like: ``` undefined method `new' for RuboCop::Cop::Struct:Module /Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:105:in `module:IndexMethod' /Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:6:in `module:Cop' /Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:4:in `module:RuboCop' /Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/mixin/index_method.rb:3:in `top (required)' /Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails_cops.rb:7:in `require_relative' /Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop/cop/rails_cops.rb:7:in `top (required)' /Users/technicalpickles/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rubocop-rails-2.21.1/lib/rubocop-rails.rb:16:in `require_relative' ``` It turned out this module has method that were calling `Struct.new`, but Ruby constant lookup ended up using our `Rubocop::Cop::Struct`, instead of the actual `Struct` class. We were able to work around it by changing the constant name, but `Struct` is always going to be part of the stdlib so a possible name collision.
1 parent e1b1fff commit a21f4a3

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/rubocop/cop/mixin/index_method.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def execute_correction(corrector, node, correction)
102102
end
103103

104104
# Internal helper class to hold match data
105-
Captures = Struct.new(
105+
Captures = ::Struct.new(
106106
:transformed_argname,
107107
:transforming_body_expr
108108
) do
@@ -112,7 +112,7 @@ def noop_transformation?
112112
end
113113

114114
# Internal helper class to hold autocorrect data
115-
Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
115+
Autocorrection = ::Struct.new(:match, :block_node, :leading, :trailing) do
116116
def self.from_each_with_object(node, match)
117117
new(match, node, 0, 0)
118118
end

0 commit comments

Comments
 (0)