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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
667ef40
[Fix #7680] Add new `Lint/StructNewOverride` cop
ybiquitous 609d199
Check argument types for `Struct.new` more strictly
ybiquitous 5d9c937
Fix `Metrics/CyclomaticComplexity` violation for `#on_send(node)`
ybiquitous 76f7dd5
Try to make the cop message milder
ybiquitous 7996480
Fix message and `VersionAdded`
ybiquitous bf2866c
Fix changelog
ybiquitous File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks unexpected overrides of the `Struct` built-in methods | ||
# via `Struct.new`. | ||
# | ||
# @example | ||
# # bad | ||
# Bad = Struct.new(:members, :clone, :count) | ||
# b = Bad.new([], true, 1) | ||
# b.members #=> [] (overriding `Struct#members`) | ||
# b.clone #=> true (overriding `Object#clone`) | ||
# b.count #=> 1 (overriding `Enumerable#count`) | ||
# | ||
# # good | ||
# Good = Struct.new(:id, :name) | ||
# g = Good.new(1, "foo") | ||
# g.members #=> [:id, :name] | ||
# g.clone #=> #<struct Good id=1, name="foo"> | ||
# g.count #=> 2 | ||
# | ||
class StructNewOverride < Cop | ||
MSG = '`%<member_name>s` member overrides `Struct#%<method_name>s`' \ | ||
' and it may be unexpected.' | ||
|
||
STRUCT_METHOD_NAMES = Struct.instance_methods | ||
STRUCT_MEMBER_NAME_TYPES = %i[sym str].freeze | ||
|
||
def_node_matcher :struct_new, <<~PATTERN | ||
(send | ||
(const ${nil? cbase} :Struct) :new ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless struct_new(node) do | ||
node.arguments.each_with_index do |arg, index| | ||
# Ignore if the first argument is a class name | ||
next if index.zero? && arg.str_type? | ||
|
||
# Ignore if the argument is not a member name | ||
next unless STRUCT_MEMBER_NAME_TYPES.include?(arg.type) | ||
|
||
member_name = arg.value | ||
|
||
next unless STRUCT_METHOD_NAMES.include?(member_name.to_sym) | ||
|
||
message = format(MSG, member_name: member_name.inspect, | ||
method_name: member_name.to_s) | ||
add_offense(arg, message: message) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::StructNewOverride do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense using `Struct.new(symbol)`' do | ||
expect_offense(<<~RUBY) | ||
Bad = Struct.new(:members) | ||
^^^^^^^^ `:members` member overrides `Struct#members` and it may be unexpected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense using `::Struct.new(symbol)`' do | ||
expect_offense(<<~RUBY) | ||
Bad = ::Struct.new(:members) | ||
^^^^^^^^ `:members` member overrides `Struct#members` and it may be unexpected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense using `Struct.new(string, ...symbols)`' do | ||
expect_offense(<<~RUBY) | ||
Struct.new('Bad', :members, :name) | ||
^^^^^^^^ `:members` member overrides `Struct#members` and it may be unexpected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense using `Struct.new(...symbols)`' do | ||
expect_offense(<<~RUBY) | ||
Bad = Struct.new(:name, :members, :address) | ||
^^^^^^^^ `:members` member overrides `Struct#members` and it may be unexpected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense using `Struct.new(symbol, string)`' do | ||
expect_offense(<<~RUBY) | ||
Bad = Struct.new(:name, "members") | ||
^^^^^^^^^ `"members"` member overrides `Struct#members` and it may be unexpected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense using `Struct.new(...)` with a block' do | ||
expect_offense(<<~RUBY) | ||
Struct.new(:members) do | ||
^^^^^^^^ `:members` member overrides `Struct#members` and it may be unexpected. | ||
def members? | ||
!members.empty? | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense using `Struct.new(...)` with multiple overrides' do | ||
expect_offense(<<~RUBY) | ||
Struct.new(:members, :clone, :zip) | ||
^^^^ `:zip` member overrides `Struct#zip` and it may be unexpected. | ||
^^^^^^ `:clone` member overrides `Struct#clone` and it may be unexpected. | ||
^^^^^^^^ `:members` member overrides `Struct#members` and it may be unexpected. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense using `Struct.new(...)` with an option argument' do | ||
expect_offense(<<~RUBY) | ||
Struct.new(:members, keyword_init: true) | ||
^^^^^^^^ `:members` member overrides `Struct#members` and it may be unexpected. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with no overrides' do | ||
expect_no_offenses(<<~RUBY) | ||
Good = Struct.new(:id, :name) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense with an override within a given block' do | ||
expect_no_offenses(<<~RUBY) | ||
Good = Struct.new(:id, :name) do | ||
def members | ||
super.tap { |ret| pp "members: " + ret.to_s } | ||
end | ||
end | ||
RUBY | ||
end | ||
end |
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 thanStructNewOverride
. 👍I'll fix it.
There was a problem hiding this comment.
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:I'm a bit concerned that
StructBuiltInMethodOverride
may be considered as any overrides of the built-in methods. 🤔There was a problem hiding this comment.
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! 🙏
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. Thanks! 👍