-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Borrow checker gets confused by a conditionally returned borrows #12147
Comments
cc @flaper87 |
This is not a bug, actually. Or, put another way, it's a dup of #6393. |
The problem is that the borrow |
Here is another case of this bug in action: /// Try to fetch a resource. If the resource has not been loaded yet, block
/// until it is loaded.
fn fetch_block<'a>(&'a mut self, path: &str) -> Result<&'a Vec<u8>, E> {
loop {
match self.fetch(path) {
Ok(Some(x)) => { return Ok(x); },
Ok(None) => { continue; },
Err(e) => { return Err(e); }
}
}
}
|
This is a dup of #6393. What is happening is that the borrow extends to the end of the scope -- which is outside of the fn as a whole. Since the returns in all these cases are conditional, the borrow is considered to remain in force for the remainder of the fn. |
…or, r=llogiq Find function path references early in the same lint pass This removes a visitor that existed to collect paths to functions in a context where the exact signature is required in order to cancel the lint. E.g. when there's a `let _: fn(&mut i32) = path_to_fn_ref_mut_i32;` statement somewhere in the crate, we shouldn't suggest removing the mutable reference in the function signature. It was doing a whole pass through the crate at the end, which seems unnecessary. It seems like we should be able to add entries to the map in the same lint pass. The map is untouched all the way until `check_crate_post` (at which point it will be populated by the visitor and finally checked), so it doesn't seem like this changes behavior: it will only be fully populated by the time we reach `check_crate_post` no matter what. I don't think this will have a significant perf impact but it did show up in a profile with 0.5% for a crate I was looking into and looked like a low hanging fruit. changelog: none
Can be worked around by going via
*
with some unsafe:The text was updated successfully, but these errors were encountered: