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

Add a cop to detect safe navigation uses with respond_to? #8668

Closed
tas50 opened this issue Sep 8, 2020 · 0 comments · Fixed by #8838
Closed

Add a cop to detect safe navigation uses with respond_to? #8668

tas50 opened this issue Sep 8, 2020 · 0 comments · Fixed by #8838

Comments

@tas50
Copy link
Contributor

tas50 commented Sep 8, 2020

Is your feature request related to a problem? Please describe.

I noticed autocorrecting some of our older code that the suggested code by RuboCop is actually totally unnecessary and it seems like a place where codebases could be simplified.

Our original code was:

next unless attrs && attrs.respond_to?(:[]) 

RuboCop suggested:

next unless attrs&.respond_to?(:[]) 

There's really no need for the &. safe navigation though because even nil has respond_to?. This can just be autocorrected to:

next unless attrs.respond_to?(:[]) 

Describe the solution you'd like

A new cop to remove unnecessary safe navigation

Describe alternatives you've considered

🤷

Additional context

🚫

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

Successfully merging a pull request may close this issue.

2 participants