Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::forget_non_drop: call to `std::mem::forget` with a value that does not implement `Drop`. Forgetting such a type is the same as dropping it
What it meansChecks for calls to std::mem::forget with a value that does not implement Drop. It is in the suspicious group and warns by default.
What you see
warning: call to `std::mem::forget` with a value that does not implement `Drop`. Forgetting such a type is the same as dropping it --> main.rs:5:1 | 5 | std::mem::forget(x); | ^^^^^^^^^^^^^^^^^^^ | note: argument has type `main::Foo` --> main.rs:5:18 | 5 | std::mem::forget(x); | ^ = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#forget_non_drop = note: `#[warn(clippy::forget_non_drop)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Calling std::mem::forget is no different than dropping such a type. A different value may have been intended.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
struct Foo;
let x = Foo;
std::mem::forget(x);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::forget_non_drop)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
forget_non_drop = "allow"One run
cargo clippy -- -A clippy::forget-non-dropThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for calls to std::mem::forget with a value that does not implement Drop.
Why is this bad?
Calling std::mem::forget is no different than dropping such a type. A different value may
have been intended.
Example
struct Foo;
let x = Foo;
std::mem::forget(x);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- empty_loopempty `loop {}` wastes CPU cycles
- float_equality_without_absfloat equality check without `.abs()`
- for_unbounded_rangefor loop on unbounded range
- four_forward_slashesthis item has comments with 4 forward slashes (`////`). These look like doc comments, but they aren't
- from_raw_with_void_ptrChecks if we’re passing a c_void raw pointer to {Box,Rc,Arc,Weak}::from_raw(_)
- incompatible_msrvThis lint checks that no function newer than the defined MSRV (minimum supported rust version) is used in the crate
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::empty_loopempty `loop {}` wastes CPU cyclesRead it next →Did this answer your question?