-
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
dropping_references
lint triggers even for mutable references
#125972
Comments
This is expected, dropping a reference doesn't drop the underline value, therefore calling If you want to ignore the reference, to make inaccessible or avoid an unused lint, I suggest following the lint suggestion, and replace the @rustbot labels -needs-triage -C-bug +C-discussion +A-lint |
Yes, my intent was to drop the reference, for soundness reasons in unsafe code. Is binding it to |
The reason the lint exist is to notify users that the This is particularly relevant when the reference is not as obvious, but is hidden behind several lines of codes. A prime example where it matters is when using a
Dropping a reference does nothing. A reference doesn't have a
|
fn main() {
let mut x = 42;
let y = &mut x;
// drop(y); // doesn't compile
let _ = y; // compiles
*y = 37;
} One thing that does move/copy out of place expressions is a "trivial" expression-statement like fn main() {
let mut x = 42;
let y = &mut x;
y;
*y = 37;
} You can also use an inline block to force a move, e.g. this doesn't compile fn main() {
let mut x = 42;
let y = &mut x;
let _ = { y }; // or just `{ y };`
*y = 37;
} |
This is incorrect. |
Yeah, sorry about that, the lint confused me between |
Is there a way to make the variable inaccessible and not trigger this lint? |
For mutable reference there is |
Doesn't really work, it's still accessible through
Interesting. Maybe the lint should mention these as alternatives to make the variable inaccessible? |
I don't know. That's not really the goal of the lint, most of the time when calling I'm worried suggesting either one will only make the suggestion more awkward for users, in particular new users, which may not understand the implication of those (subtle) syntax differences. |
I'll try to make the error message more useful. @rustbot claim |
When I call
std::mem::drop
on a mutable reference, this is supposed to consume the reference and make it unavailable to following code. And indeed it does, because any following uses trigger a "use of moved value" error.But when I do this, I get a warning from the compiler:
Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: