-
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
borrowed_box should not be emitted for mutable boxed trait object #1845
Comments
I think you need to do something like |
And probably use an explicit map of |
The following trigger the same error: fn get_mut<'a>(map: &'a mut HashMap<String, Box<Display>>, key: &str) -> Option<&'a mut Display> {
map.get_mut(key).as_mut()
.map(|x| &mut ***x)
} I found out the following works, but I don't think it is the same: fn get_mut(map: &'static mut HashMap<String, Box<Display>>, key: &str) -> Option<&'static mut Display> {
map.get_mut(key)
.map(AsMut::as_mut)
} |
fn get_mut<'a>(map: &'a mut HashMap<String, Box<Display + 'static >>, key: &str) -> Option<&'a mut (Display + 'static)> {
map.get_mut(key).map(|x| &mut **x)
} |
Again, this has changed the requirements of the function. fn get<'a>(map: &'a HashMap<String, Box<Display>>, key: &str) -> Option<&'a Box<Display>> {
map.get(key)
} to fn get<'a>(map: &'a HashMap<String, Box<Display>>, key: &str) -> Option<&'a Display> {
map.get(key)
.map(AsRef::as_ref)
} but not when the reference is mutable. |
Variance. And iirc in the box case there was an implicit static bound. |
Indeed, @Manishearth's suggestion is the correct version. |
FYI, this problem doesn't occur at the moment as the lint ignores every mutable references (#5491). However, if someone implements the enhancement suggested here (#5491 (comment)), it will reappear. |
Hello.
The
borrowed_box
suggestion can be a false positive for mutable boxed trait object, because it seems there's no way to apply the suggestion (it only works for immutable boxed trait object).For example, the following code triggers this warning:
But if I try to fix it by:
I get a compilation error:
(To be honest, I don't understand why I get this error for returning a mutable reference, but not for an immutable reference.)
Thanks to remove the warning in this case.
The text was updated successfully, but these errors were encountered: