-
Notifications
You must be signed in to change notification settings - Fork 1.6k
mut_from_ref and Pin<&mut> #7749
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
Comments
This looks like a false positive. From the example, I would be guessing that the logic only checks if |
Did some research, and it would appear that this lint isn't taking smart pointers into account. Some relevant code snippets if let FnRetTy::Return(ty) = decl.output {
if let Some((out, Mutability::Mut, _)) = get_rptr_lm(ty) {
let mut immutables = vec![];
for (_, ref mutbl, ref argspan) in decl
.inputs
.iter()
.filter_map(get_rptr_lm)
.filter(|&(lt, _, _)| lt.name == out.name)
{
if *mutbl == Mutability::Mut {
return;
}
immutables.push(*argspan);
}
if immutables.is_empty() {
return;
}
span_lint_and_then(
cx,
MUT_FROM_REF,
ty.span,
"mutable borrow from immutable input(s)",
|diag| {
let ms = MultiSpan::from_spans(immutables);
diag.span_note(ms, "immutable borrow here");
},
);
}
} fn get_rptr_lm<'tcx>(ty: &'tcx Ty<'tcx>) -> Option<(&'tcx Lifetime, Mutability, Span)> {
if let TyKind::Rptr(ref lt, ref m) = ty.kind {
Some((lt, m.mutbl, ty.span))
} else {
None
}
} My first thought about fixing this is to scan for input types that implement |
Also I just realized this lint isn't recursing into tuples, slices, or arrays. |
I found this: https://github.com/rust-lang/rust-clippy/blob/master/clippy_utils/src/ty.rs#L118 which seems useful. I also learned that walking a type returns lifetime information, now I just need to determine a way to make that lifetime information useful in this context. |
fixes #7749. This issue proposes searching for `DerefMut` impls, which is not done here: every lifetime parameter (aka `<'a>`) is the input types is considered to be potentially mutable, and thus deactivates the lint. changelog: [`mut_from_ref`]: Fixes false positive, where lifetimes nested in the type (e.g. `Box<&'a mut T>`) were not considered.
Lint name: mut_from_ref
I tried this code:
I expected to see this happen: No errors
Instead, this happened: Clippy reported the
mut_from_ref
error level lint.The lint documentation says
and the lint does not trigger if it is just
Pin<&mut Self>
by itself, only when it is used in conjunction with another immutable borrow. If there is a reason this is not a false positive, can someone explain the details to me?Meta
Rust version (
rustc -Vv
):@ rustbot label +I-suggestion-causes-error
The text was updated successfully, but these errors were encountered: