Title: Style/ClassAndModuleChildren not safe within module or class · Issue #14476 · rubocop/rubocop · GitHub
Open Graph Title: Style/ClassAndModuleChildren not safe within module or class · Issue #14476 · rubocop/rubocop
X Title: Style/ClassAndModuleChildren not safe within module or class · Issue #14476 · rubocop/rubocop
Description: The Style/ClassAndModuleChildren cop is not safe to correct when the code occurs within another scope. This led to a bug in JRuby's build when using automated corrections. Nested module access will not walk up to the toplevel to look for...
Open Graph Description: The Style/ClassAndModuleChildren cop is not safe to correct when the code occurs within another scope. This led to a bug in JRuby's build when using automated corrections. Nested module access will...
X Description: The Style/ClassAndModuleChildren cop is not safe to correct when the code occurs within another scope. This led to a bug in JRuby's build when using automated corrections. Nested module access ...
Opengraph URL: https://github.com/rubocop/rubocop/issues/14476
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Style/ClassAndModuleChildren not safe within module or class","articleBody":"The Style/ClassAndModuleChildren cop is not safe to correct when the code occurs within another scope. This led to a bug in JRuby's build when using automated corrections.\n\nNested module access will not walk up to the toplevel to look for modules or classes to open, while path-based access does do this.\n\n--------\n\n## Expected behavior\n\nCode below should behave this way before or after correction, or else it should be considered an unsafe correction.\n\nOriginal code:\n\n```ruby\nmodule X\n module Y\n puts object_id\n end\nend\nmodule Z\n module X::Y\n puts object_id\n end\nend\n# prints same object id for Y, because path-based access uses the toplevel X\n# and reopens the existing modules\n```\n\n## Actual behavior\n\nWhen corrected to nested modules, the reopen of X creates a new module tree rather than accessing the toplevel X.\n\nCorrected code:\n\n```ruby\n...\nmodule Z\n module X\n module Y\n puts object_id\n end\n end\nend\n# Reopen of X is a new module and Y is also new with a different object_id\n```\n\nFull script example of the behavior:\n\n```ruby\nmodule X\n module Y\n puts object_id\n end\nend\nmodule Z\n module X::Y\n puts object_id\n end\nend\nmodule Q\n module X\n module Y\n puts object_id\n end\n end\nend\n```\n\nOutput from example, showing that the correction is not safe:\n\n```\n$ cx 3.4 ruby -v blah.rb\nruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]\n16\n16\n24\n```\n\n## RuboCop version\n\n```\n$ rubocop -V\n1.80.0 (using Parser 3.3.9.0, rubocop-ast 1.46.0, analyzing as Ruby 2.7, running on jruby 3.1.7) [java]\n```","author":{"url":"https://github.com/headius","@type":"Person","name":"headius"},"datePublished":"2025-08-25T18:12:52.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/14476/rubocop/issues/14476"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:fdd0471c-2d39-85cd-70c8-5c01c4c6bff0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B090:ADA4B:752C608:9F74FD7:69960242 |
| html-safe-nonce | d43e1246b02f5843f80ff9fb8efc5bf6e534e5394569ad7efa0606404a8081b5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDkwOkFEQTRCOjc1MkM2MDg6OUY3NEZENzo2OTk2MDI0MiIsInZpc2l0b3JfaWQiOiI0OTkwODc2MjcwMzA3MTgxMTIzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6a2dab4250889e7f3035c4c265388011f93e6ee4e39f0b4ec6add4de0e93aa5e |
| hovercard-subject-tag | issue:3352756861 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/rubocop/rubocop/14476/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fb5d61eeffd4b828bc7781e3576d5c8c724a3a05271e43fefb5361bff0d826d0/rubocop/rubocop/issues/14476 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fb5d61eeffd4b828bc7781e3576d5c8c724a3a05271e43fefb5361bff0d826d0/rubocop/rubocop/issues/14476 |
| og:image:alt | The Style/ClassAndModuleChildren cop is not safe to correct when the code occurs within another scope. This led to a bug in JRuby's build when using automated corrections. Nested module access will... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | headius |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7490a2fd98baca866acf503b81aa249491b42e5b4bab7f7a2801be81175c8845 |
| turbo-cache-control | no-preview |
| go-import | github.com/rubocop/rubocop git https://github.com/rubocop/rubocop.git |
| octolytics-dimension-user_id | 10871348 |
| octolytics-dimension-user_login | rubocop |
| octolytics-dimension-repository_id | 4095287 |
| octolytics-dimension-repository_nwo | rubocop/rubocop |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 4095287 |
| octolytics-dimension-repository_network_root_nwo | rubocop/rubocop |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5814362c11fe63ca811caa7c470897ee2b3e593e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width