-
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
let_and_return: avoid "does not live long enough" errors #5680
Conversation
r? @Manishearth (rust_highfive has picked a reviewer for you, use r? to override) |
I wonder if we really need the full set of checks here: MIR lints are trickier in general, perhaps just checking if the let-and-return statement contains any borrowed data is enough? This leads to some false negatives, but that's okay IMO. |
thoughts, @flip1995 ? |
Can we do this easily without the MIR? Can we check for borrows in e.g. a method chain in the HIR? If there is a short and simple way to do this, I would prefer it for simplicity. But I can't think of how to do this right now. |
We can just check the type for lifetime params, no? |
It may be relevant to the discussion that the same fix (regardless of the implementation choice in the end) can be applied to @Manishearth TBH I did not think of that possibility, I guess it could be valid but I kinda like the possibility of being more precise and avoid the false negatives. Also, (maybe due to inexperience with the code base) I'm not sure if that approach would be simpler to implement, if I'm not mistaken it would imply walking the RHS expression looking for function and method calls, and then checking the definition to see if the return value type has lifetimes (at least, maybe I'm missing other cases?) Just wanted to give my 2 cents, but of course I'll implement whatever we decide in the end :) |
Yeah, but MIR stuff is hard to maintain and I'd avoid it if we can. A couple false negatives is no biggie imo. You don't need to walk anything, you just need to check the type of the returned value for any lifetime substitutions |
@Manishearth which return type do you mean? In this example: fn read_line() -> String {
let stdin = io::stdin();
let line = stdin.lock().lines().next().unwrap().unwrap();
line
} The return type of the function is just Am I missing something? |
oh, I see. yeah, my bad, my model of the issue was incorrect. |
@Manishearth I've written the solution we discussed to avoid using the MIR, it should be ready for review :) |
@bors r+ hmm, this has to go through every expression, so it's more likely that there will be false negatives. but i guess this is okay, @flip1995 do you have opinions on this vs the more complicated but more correct MIR approach? (we can land that later if folks feel the MIR approach was also okay) |
📌 Commit dac8a3c has been approved by |
As you already wrote, MIR lints are harder to maintain. I don't have experience with MIR, so reviewing code is already kind of hard. So if there is a nearly equivalent solution (a few more FNs) without MIR, I guess it's better than the MIR version. But in cases where MIR is necessary to prevent FPs and still have an effective lint (e.g. |
For this case especially, I prefer the solution with just HIR, since it at least didn't introduce FNs in our tests. |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
@flip1995 yeah the current solution fixes FPs, but introduces some FNs. I care less about FNs. |
EDIT: Add #3324 to the list of fixes
Description of old impl
Avoid suggesting turning the RHS expression of the last statement into the block tail expression if a temporary borrows from a local that would be destroyed before.
This is my first incursion into MIR so there's probably room for improvement!
Avoid linting if the return type of some method or function called in the last statement has a lifetime parameter.
changelog: Fix false positive in [
let_and_return
]Fixes #3792
Fixes #3324