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
New cop: Detect unexpected overwriting of Struct methods #7680
Comments
Sounds like a good Lint cop to me. PRs welcome! |
What name is best for us? For example, |
I think linting for the use of I'm not so sure about linting for methods on |
Good catch. 👍 What about the way that we pick up some confusing methods? "Confusing" may differ from person to person, but I think we should draw a line somewhere. For example, we can pick up from the following candidates:
|
I've thought about this issue for a while, but now I feel it hard that we draw the line on what methods are detected by this cop. For example, please assume that someone writes the following code: Commit = Struct.new(:id, :hash)
a_commit = Commit.new(123, "9a4f0ee")
a_commit.hash #=> "9a4f0ee". Not an integer! The code above violates the And, I don't think that almost programmers write Commit = Struct.new(:id) do
def hash
# own calculation of a hash...
end
end Therefore, I think that it's valuable to check all the methods including Also, I think that it's a discussion-required and hard task to pick up what methods we can allow overriding. Every time a method would be added to What do you think about this opinion? FYI, here is another interesting example (from this API doc): Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.zip #=> 12345
AnotherCustomer = Struct.new(:name, :address)
tom = AnotherCustomer.new("Tom Smith", "123 Maple, Anytown NC")
tom.zip #=> [["Tom Smith"], ["123 Maple, Anytown NC"]]
|
I've opened the PR #7699, but I think the discussion is open yet. 😃 I hope the PR could promote the discussion about #7680 (comment) or #7680 (comment)! |
@ybiquitous Thanks for the great explanation! It is really helpful to understand the problem. I had the same concern with @Drenmi. It is below.
But now I think we don't need to lint regular classes. Because of the @ybiquitous's comment.
Yes, I agree that picking up methods is a hard task. And I guess anyone can't choose proper methods, because the proper methods depend on cases. So I propose we make the cop to add an offense for all overrides. But we can decrease the severity of the cop, or make the message milder. |
@pocke Thanks for your feedback!
I agree completely! 👍
Looks good! I will try to change the PR. 👍 |
Is your feature request related to a problem? Please describe.
I've encountered an weird behavior on using
Struct
class:This is caused by unexpectedly overwriting the
Struct#members
method.Describe the solution you'd like
Add a new cop to detect unexpected overwriting such as the example above.
Describe alternatives you've considered
None.
Additional context
Struct
includesEnumerable
and has many methods e.g.length
,count
,min
,max
, orsum
, so it seems that some people might use such methods as aStruct
member unexpectedly.The text was updated successfully, but these errors were encountered: