-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 lint for mem::replace(.., None)
.
#3195
Add lint for mem::replace(.., None)
.
#3195
Conversation
One thing I’m not sure about is this early check to make sure the function call has two arguments. It seems like a low-cost check, but maybe it’s premature optimization? |
I came across this same suggestion in a reddit thread a while ago: https://www.reddit.com/r/rust/comments/94elrg/blog_some_slight_improvements/ |
It just occurred to me that this lint will match: let mut an_option = Some(1);
let _ = mem::replace(&mut an_option, None); but not: let mut_ref_an_option = &mut an_option;
let _ = mem::replace(mut_ref_an_option, None); Let figure out how to do that and update this PR... |
Oh good catch! I missed that one. The problem is that |
Thanks for the suggestion. I looked through |
or just EDIT: Here's an example on how to use it (it's |
I updated the lint to match both |
Suggest `Option::take()` as an alternative.
Oh the problem was something else. Oops, sorry for the wrong pointers! LGTM |
@flip1995 no problem, you pointed me to something useful I didn't know about. Thanks! |
@Manishearth not sure if necessary, but: I license past and future contributions under the dual MIT/Apache-2.0 license, allowing licensees to chose either at their option. |
Sweet, thanks. I was going to open a separate issue for the new contributors once the other issues closed. |
but this works too |
@JayKickliter could you also leave this comment on #3230 for completeness' sake? |
Done |
This PR adds a lint suggesting
Option::take()
as an alternative tomem::replace(.., None)
:I have run across this in both my company's and third party codebases. At least one guide suggests favoring
take()
as well:Since this is my first Clippy contribution, I'm all ears to any suggestions to improve this PR.