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

[Fix #7680] Add new Lint/StructNewOverride cop #7699

Merged
merged 6 commits into from Mar 21, 2020

Conversation

ybiquitous
Copy link
Contributor

@ybiquitous ybiquitous commented Feb 8, 2020

Fix #7680

This adds a new Lint/StructNewOverride cop to check unexpected overrides of Struct original methods.

I originally planned to name it "Lint/StructOverride", but I think that disallowing all the overrides is too strict, so I rename it to Lint/StructNewOverride. I intend that this cop limits to overrides via arguments in Struct.new(...).

However, the cop name may need to be discussed more.

Furthermore, as I commented on #7680 (comment), it may need more discussion on whether the cop should check all the methods (from Object or Enumerable) or not.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@ybiquitous
Copy link
Contributor Author

ybiquitous commented Feb 9, 2020

By following #7680 (comment), I've mad the cop message milder via f10bc60.

E.g.

:members member overrides Struct#members and it may be unexpected.

# g.clone #=> #<struct Good id=1, name="foo">
# g.count #=> 2
#
class StructNewOverride < Cop
Copy link
Collaborator

@bbatsov bbatsov Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then we can name this StructBuiltInMethodOverride which is more descriptive IMO.

Copy link
Contributor Author

@ybiquitous ybiquitous Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I also think that StructBuiltInMethodOverride is easier to understand than StructNewOverride. 👍
I'll fix it.

Copy link
Contributor Author

@ybiquitous ybiquitous Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that this cop detects some overrides by arguments of Struct.new, not normal overrides as follows:

Foo = Struct.new(:name) do
  def to_s
    name.to_s
  end
end

I'm a bit concerned that StructBuiltInMethodOverride may be considered as any overrides of the built-in methods. 🤔

Copy link
Contributor Author

@ybiquitous ybiquitous Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, StructBuiltInMethodOverrideByConstructor may be more descriptive but too long. 😓
I may think too much... Please give me any help! 🙏

Copy link
Collaborator

@bbatsov bbatsov Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is hard! :D Let's keep the shorter name and down the road we can maybe extend this cop to cover dangerous overrides by explicit definitions as well.

Copy link
Contributor Author

@ybiquitous ybiquitous Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense. Thanks! 👍

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 21, 2020

Apart from my small remarks - great work!

manual/cops_lint.md Outdated Show resolved Hide resolved
@ybiquitous
Copy link
Contributor Author

ybiquitous commented Mar 21, 2020

I've addressed all the reviews. Please review again if you have time. 🙏

@bbatsov bbatsov merged commit 4dacec0 into rubocop:master Mar 21, 2020
23 of 24 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 21, 2020

🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants