-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Enhance Lint/RedundantSafeNavigation
to handle conversion methods with defaults
#12418
Enhance Lint/RedundantSafeNavigation
to handle conversion methods with defaults
#12418
Conversation
# foo&.to_h || {} | ||
# | ||
# # good | ||
# foo.to_h |
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.
It would be useful to document other literals as well.
range = range_between(node.lhs.loc.dot.begin_pos, node.source_range.end_pos) | ||
|
||
add_offense(range) do |corrector| | ||
corrector.replace(node, "#{receiver.source}.#{method_name}") |
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.
corrector.replace(node, "#{receiver.source}.#{method_name}") | |
corrector.replace(range, ".#{method_name}") |
it 'registers an offense and corrects when `.&` is used in `.to_h` coversion with default' do | ||
expect_offense(<<~RUBY) | ||
foo&.to_h || {} | ||
^^^^^^^^^^^^ Redundant safe navigation detected. |
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.
In such cases, adjusting the message a bit would likely prevent user confusion. e.g.:
^^^^^^^^^^^^ Redundant safe navigation detected. | |
^^^^^^^^^^^^ Redundant safe navigation with default literal value detected. |
397ce9b
to
2429636
Compare
Updated. |
The example is missing: # bad
hsh&.to_h { |k, v| [k, v] } || {}
# good
hsh.to_h { |k, v| [k, v] }
This should fix the following offenses: https://github.com/gitlabhq/gitlabhq/blob/master/lib/security/ci_configuration/sast_build_action.rb#L24 |
2429636
to
fe5ed60
Compare
Thanks. Let's proceed with this first. |
Fixes #12412.
With these changes I found a few offenses in the gitlab's codebase.