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

Cop idea: Redundant safe navigation for basic types #12412

Closed
ydakuka opened this issue Nov 24, 2023 · 2 comments · Fixed by #12418
Closed

Cop idea: Redundant safe navigation for basic types #12412

ydakuka opened this issue Nov 24, 2023 · 2 comments · Fixed by #12418

Comments

@ydakuka
Copy link

ydakuka commented Nov 24, 2023

Describe the solution you'd like

hsh = [['a', '1'], ['b', '2']]
# hsh = nil

# bad
hsh&.to_h { |k, v| [k, v] } || {}
# bad
hsh&.to_h || {}

# good
hsh.to_h { |k,v| [k, v] }
# good
hsh.to_h
arr = ['a', 'b', 'c']
# arr = nil

# bad
arr&.to_a || []

# good
arr.to_a
str = "foo"
# str = nil

# bad
str&.to_s || ''

# good
str.to_s
@koic
Copy link
Member

koic commented Nov 25, 2023

Rather than creating a new cop, extending the existing Lint/RedundantSafeNavigation can be considered. (I'm not yet convinced which is the better option.)

@fatkodima
Copy link
Contributor

I do not think we need a new cop, Lint/RedundantSafeNavigation can be extended, as it was once extended for constants.

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 a pull request may close this issue.

3 participants