Skip to content

added downsides to "known problems" for get_unwrap lint #3368

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

Merged
merged 2 commits into from
Oct 31, 2018

Conversation

m-rutter
Copy link
Contributor

As a beginner I found this lint to be confusing because I was not sure how the Option type disappeared as conceptually I know that my .get() and Index could fail. Initially I thought maybe the compiler or clippy was smart enough to understand that it was impossible for my .get() to fail in this particular case, but it was explained to me that using the Index syntax is just shorthand for directly unwrapping the value:

https://doc.rust-lang.org/src/std/collections/hash/map.rs.html#1547

For beginners or users trying to iterate quickly it seems common to litter your code with unwrap or except as placeholders for where some explicit error handling might need to take place. I think it should be warned that using Index is merely more concise, but doesn't at all reduce the risk of panics and might in fact cause you to miss handling them in a future refactor.

@phansch phansch added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 28, 2018
@flip1995
Copy link
Member

bors try

bors bot added a commit that referenced this pull request Oct 31, 2018
@bors
Copy link
Contributor

bors bot commented Oct 31, 2018

@flip1995
Copy link
Member

Thanks! That's definitely good advise for beginners.

bors r+

bors bot added a commit that referenced this pull request Oct 31, 2018
3368: added downsides to "known problems" for get_unwrap lint r=flip1995 a=humean

As a beginner I found this lint to be confusing because I was not sure how the `Option` type disappeared as conceptually I know that my `.get()` and Index could fail. Initially I thought maybe the compiler or clippy was smart enough to understand that it was impossible for my `.get()` to fail in this particular case, but it was explained to me that using the Index syntax is just shorthand for directly unwrapping the value:

https://doc.rust-lang.org/src/std/collections/hash/map.rs.html#1547

For beginners or users trying to iterate quickly it seems common to litter your code with `unwrap` or `except` as placeholders for where some explicit error handling might need to take place. I think it should be warned that using Index is merely more concise, but doesn't at all reduce the risk of panics and might in fact cause you to miss handling them in a future refactor.

Co-authored-by: Michael Rutter <[email protected]>
Co-authored-by: Michael Rutter <[email protected]>
@bors
Copy link
Contributor

bors bot commented Oct 31, 2018

@bors bors bot merged commit 232a483 into rust-lang:master Oct 31, 2018
@m-rutter m-rutter deleted the get-unwrap-downsides branch November 8, 2018 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants