feat(unnecessary_unwrap_unchecked): new lint#16252
feat(unnecessary_unwrap_unchecked): new lint#16252ada4a wants to merge 2 commits intorust-lang:masterfrom
Conversation
|
rustbot has assigned @samueltardieu. Use |
There was a problem hiding this comment.
I would restrict this lint to the following cases:
- an
_uncheckedfunction exists - with the same inputs types (or even the same input names if they are available, I haven't checked if they are for non-local functions), and an unwrapped output type (unwrapping can be from
OptionorResult) - which is
unsafe
And in this case, I would even suggest a replacement with MaybeIncorrect (since we can't be certain that the semantics will match), except maybe when it comes from the standard library from which we can expect a safe _unchecked meaning.
That should limit false positives a lot.
We could even later add a configuration option to check for .unwrap(), but that would probably be best to discuss this separately.
What do you think?
|
All very sensible suggestions. One question about this part:
How can I find that out? I see that |
I'm not sure this can be done easily, as the user can even come with their own version of core/alloc/std, or run in |
068c26a to
c88d136
Compare
This comment has been minimized.
This comment has been minimized.
|
This ended up requiring quite a lot of refactoring. Hopefully the commits are small enough to be reviewable but not too small as to be annoying to deal with. I think it makes sense to squash them after the review (apart from the |
| && let Some((unchecked, unchecked_ident)) = | ||
| find_unchecked_sibling_method(cx, checked_def_id, checked_ident) | ||
| && same_functions_modulo_safety(cx, checked_def_id, unchecked.def_id, expected_ret_ty) | ||
| && (cx.tcx.visibility(unchecked.def_id)).is_at_least(cx.tcx.visibility(checked_def_id), cx.tcx) => |
There was a problem hiding this comment.
This check immediately follows the "same functions" check in all three cases. I thought about moving it into the latter, but wasn't sure how clean that would be. WDYT?
There was a problem hiding this comment.
You could import it into same_functions_modulo_safety(), maybe renaming the function, or just adding a doc comment saying that visibility is also checked.
There is little chance that the "unchecked" variant has a greater visibility than the "checked" one, you could just compare the visibility for equality, this would even better fit same_functions_modulo_safety().
1155425 to
45c37c3
Compare
|
Oh, right, I also wanted to add a label pointing to the definition site of the unchecked counterpart – would be nice, no? I'll mark the PR as draft for now so that I don't forget to address this |
Not always, as it might give different results depending on whether the rust-src package is installed or not. And in tests it leads to churn when the line of the standard library function changes. |
|
I'm surprised there isn't a Oh well, let's undraft this then |
| // XXX: look into methods from other impls as well? | ||
| // would need to make sure that the impl generics etc. match |
There was a problem hiding this comment.
I agree that this is probably not worth it. Don't let it as a XXX, this is not a todo, if someone wants to add this later, they can.
You can maybe add a note in the lint definition saying that the unchecked methods are proposed only if they come from the same impl block.
| && let Some((unchecked, unchecked_ident)) = | ||
| find_unchecked_sibling_method(cx, checked_def_id, checked_ident) | ||
| && same_functions_modulo_safety(cx, checked_def_id, unchecked.def_id, expected_ret_ty) | ||
| && (cx.tcx.visibility(unchecked.def_id)).is_at_least(cx.tcx.visibility(checked_def_id), cx.tcx) => |
There was a problem hiding this comment.
You could import it into same_functions_modulo_safety(), maybe renaming the function, or just adding a doc comment saying that visibility is also checked.
There is little chance that the "unchecked" variant has a greater visibility than the "checked" one, you could just compare the visibility for equality, this would even better fit same_functions_modulo_safety().
45c37c3 to
c3dec12
Compare
This comment has been minimized.
This comment has been minimized.
c3dec12 to
e9f927a
Compare
|
I've squashed the existing commits, addressed review comments in a new commit, and did some reordering in yet another commit, to hopefully ease review EDIT: argh, no, accidentally left the visibility thing in the squashed commit... I don't think I'll bother digging it out now |
e9f927a to
7527242
Compare
This comment has been minimized.
This comment has been minimized.
..and add a bunch more cases
7527242 to
3bdef2b
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
Co-authored-by: Catherine Flores <catherine.3.flores@gmail.com>
3bdef2b to
c14f21e
Compare
Closes #11108
Resurrects #11139
changelog: [
unnecessary_unwrap_unchecked]: new lint