Git Product home page Git Product logo

Comments (11)

Urgau avatar Urgau commented on August 11, 2024

This is expected, dropping a reference doesn't drop the underline value, therefore calling drop on it is misleading.

If you want to ignore the reference, to make inaccessible or avoid an unused lint, I suggest following the lint suggestion, and replace the drop(mutref); by let _ = mutref;. It has the same effect as it renders the mutref binding inaccessible but doesn't imply that the underline value is dropped.

@rustbot labels -needs-triage -C-bug +C-discussion +A-lint

from rust.

Rua avatar Rua commented on August 11, 2024

Yes, my intent was to drop the reference, for soundness reasons in unsafe code. Is binding it to _ guaranteed to drop it?

from rust.

Urgau avatar Urgau commented on August 11, 2024

The reason the lint exist is to notify users that the drop call does nothing, while the user might have though that it did something, otherwise there wouldn't be drop call.

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 Mutex and calling drop(ref_mutexguard) instead of drop(mutexguard) where this can lead to bug because the mutex can live longer than expected.

Yes, my intent was to drop the reference, for soundness reasons in unsafe code. Is binding it to _ guaranteed to drop it?

Dropping a reference does nothing. A reference doesn't have a Drop impl.

let _ = <expr>; is guaranteed to drop immediately, but as I said, dropping a reference does nothing, but it still renders the previous binding inaccessible (for non-Copy types), if that is your intent.

from rust.

zachs18 avatar zachs18 commented on August 11, 2024

let _ = <expr>; does not move/copy out of <expr>, so if it is a place expression (e.g. a variable) it will not be dropped, and will still be accessible later even if it does not implement Copy.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=97306071db0da28259341950da233f4a

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 <expr>;, e.g, this doesn't compile

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;
}

from rust.

tbu- avatar tbu- commented on August 11, 2024

to make inaccessible or avoid an unused lint, I suggest following the lint suggestion, and replace the drop(mutref); by let _ = mutref;. I has the same effect as it renders the mutref binding inaccessible but doesn't imply that the underline value is dropped.

This is incorrect. let _ = variable; has no effect other than silencing the unused variable lint. It does not make variable inaccessible.

from rust.

Urgau avatar Urgau commented on August 11, 2024

Yeah, sorry about that, the lint confused me between let _ = mutref; and let _a = mutref;.
The last one which does move the value (at least for non-Copy types, which &mut <type> is).

from rust.

tbu- avatar tbu- commented on August 11, 2024

Is there a way to make the variable inaccessible and not trigger this lint?

from rust.

Urgau avatar Urgau commented on August 11, 2024

Is there a way to make the variable inaccessible and not trigger this lint?

For mutable reference there is let _a = mutref;, let _ = { mutref }; or mutref; just to name a few.
For immutable reference, I'm not sure, since they are Copy-able.

from rust.

tbu- avatar tbu- commented on August 11, 2024

For mutable reference there is let _a = mutref;

Doesn't really work, it's still accessible through _a.

let _ = { mutref }; or mutref;

Interesting. Maybe the lint should mention these as alternatives to make the variable inaccessible?

from rust.

Urgau avatar Urgau commented on August 11, 2024

let _ = { mutref }; or mutref;

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 drop users want to drop the underline value, not making it inaccessible; and if the goal was to silence the unused variable lint, using let _ = ...; is still valid.

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.

from rust.

tbu- avatar tbu- commented on August 11, 2024

I'll try to make the error message more useful.

@rustbot claim

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.