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

Rails/ReflectionClassName false positive with Ruby 3.1 shorthand hashes #757

Closed
shkm opened this issue Aug 24, 2022 · 3 comments · Fixed by #758
Closed

Rails/ReflectionClassName false positive with Ruby 3.1 shorthand hashes #757

shkm opened this issue Aug 24, 2022 · 3 comments · Fixed by #758
Labels
bug Something isn't working

Comments

@shkm
Copy link

shkm commented Aug 24, 2022

Expected behavior

RuboCop Rails should not raise an offence

Actual behavior

It does raise an offence :)

Steps to reproduce the problem

# frozen_string_literal: true

# Author
class Author < ApplicationRecord
  class_name = 'Book'
  has_many :books, dependent: :destroy, class_name:
end
rubocop app/models/author.rb
Inspecting 1 file
C

Offenses:

app/models/author.rb:6:41: C: Rails/ReflectionClassName: Use a string value for class_name.
  has_many :books, dependent: :destroy, class_name:
                                        ^^^^^^^^^^^

I've created a minimal example here: https://github.com/shkm/rubocop-rails-shorthand-hash-bug

RuboCop version

RuboCop Rails current master (0d0767d)

1.35.1 (using Parser 3.1.2.1, rubocop-ast 1.21.0, running on ruby 3.1.2 x86_64-linux)
  - rubocop-rails 2.15.2

This issue may be related to #106

@koic
Copy link
Member

koic commented Aug 25, 2022

This is the Hash shorthand syntax introduced in Ruby 3.1. Please see the Style/HashSyntax documentation for more information in this case. Thank you.
https://docs.rubocop.org/rubocop/1.34/cops_style.html#enforcedshorthandsyntax-always-default

@koic koic closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2022
@shkm
Copy link
Author

shkm commented Aug 25, 2022

Hi @koic,

Indeed it's the shorthand syntax, but shouldn't rubocop-rails work with it? The complaint is coming from rubocop-rails, not rubocop. We have EnforcedShorthandSyntax set to true (default) and don't see any problems elsewhere; it's specifically on a has_many.

@koic
Copy link
Member

koic commented Aug 25, 2022

Oops, You are right! This is a RuboCop Rails issue 🙇

@koic koic reopened this Aug 25, 2022
@koic koic added the bug Something isn't working label Aug 25, 2022
koic added a commit to koic/rubocop-rails that referenced this issue Aug 26, 2022
Fixes rubocop#757.

This PR fixes a false positive for `Rails/ReflectionClassName`
when using Ruby 3.1's hash shorthand syntax.
koic added a commit to koic/rubocop-rails that referenced this issue Aug 29, 2022
Fixes rubocop#757.

This PR fixes a false positive for `Rails/ReflectionClassName`
when using Ruby 3.1's hash shorthand syntax.
@koic koic closed this as completed in #758 Sep 6, 2022
koic added a commit that referenced this issue Sep 6, 2022
…ection_class_name

[Fix #757] Fix a false positive for `Rails/ReflectionClassName`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants