Add RuboCop cop enforcing Data.define over Struct.new - #2879
Merged
Merged
Conversation
Requested in review on PR #2855: prefer Data.define over Struct.new for immutable value objects. The custom cop flags Struct.new sends without autocorrection (mutability and keyword semantics differ, so each hit needs a human decision) and is required from .rubocop.yml. Converts the seven Struct.new test doubles in spec/helpers/email_header_helper_spec.rb to Data.define — the only usages in the repo. Data.define accepts the same positional arguments here. Refs #2878
CI eager-loads the app (config.eager_load = ENV['CI'].present? in test.rb), and autoload_lib walked lib/rubocop, requiring the cop before rubocop was loaded: 'uninitialized constant RuboCop::Cop::Style::Base'. RuboCop cops are loaded by RuboCop itself via the require in .rubocop.yml, not by Rails; exclude the directory from autoloading, as the autoload_lib comment invites.
mroderick
marked this pull request as ready for review
September 15, 2026 10:46
olleolleolle
approved these changes
Sep 15, 2026
Review feedback on #2879: the spec's member stand-ins should be RSpec doubles rather than Data.define instances. instance_double(Member) verifies against the real class and satisfies RSpec/VerifiedDoubles. The helper only reads member.id and member.email.
mroderick
enabled auto-merge
September 15, 2026 15:17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2878.
Adds a custom RuboCop cop,
Style/PreferDataDefine, that flagsStruct.newsends with no autocorrection. The sevenStruct.newtest doubles inspec/helpers/email_header_helper_spec.rbare now RSpec verifying doubles (instance_double(Member, ...)), per review feedback — the repo's onlyStruct.newusages are gone. Follows Olle's review request on #2855.The cop is required from
.rubocop.yml, sobundle exec rubocopand CI enforce the preference from now on. A review round also fixed Zeitwerk eager-loading oflib/rubocopunder CI. No autocorrection on purpose: theStruct.new->Data.definerewrite isn't safely mechanical (mutability and keyword semantics differ), so each future hit gets a human decision.Review notes
ConstNode#valueno longer exists in rubocop-ast 1.50, the cop usesconst_name == 'Struct'instead (verified for bothStructand::Struct; namespaced receivers likeOther::Struct.neware correctly ignored).AllCops.Exclude(db/config/bin) is not covered; a future--regenerate-todowhile offenses exist would add a todo entry and suppress the cop; name-based indirection (s = Struct; s.new) bypasses it.