Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ pub(crate) enum InvalidNullArgumentsDiag {
#[diag(
"calling this function with a null pointer is undefined behavior, even if the result of the function is unused"
)]
#[help(
#[note(
"for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>"
)]
NullPtrInline {
Expand All @@ -756,7 +756,7 @@ pub(crate) enum InvalidNullArgumentsDiag {
#[diag(
"calling this function with a null pointer is undefined behavior, even if the result of the function is unused"
)]
#[help(
#[note(
"for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>"
)]
NullPtrThroughBinding {
Expand Down Expand Up @@ -851,6 +851,9 @@ pub(crate) enum UseLetUnderscoreIgnoreSuggestion {
// drop_forget_useless.rs
#[derive(Diagnostic)]
#[diag("calls to `std::mem::drop` with a reference instead of an owned value does nothing")]
#[note(
"for more information about `std::mem::drop`, see <https://doc.rust-lang.org/std/mem/fn.drop.html>"
)]
pub(crate) struct DropRefDiag<'a> {
pub arg_ty: Ty<'a>,
#[label("argument has type `{$arg_ty}`")]
Expand All @@ -861,6 +864,9 @@ pub(crate) struct DropRefDiag<'a> {

#[derive(Diagnostic)]
#[diag("calls to `std::mem::drop` with a value that implements `Copy` does nothing")]
#[note(
"for more information about `std::mem::drop`, see <https://doc.rust-lang.org/std/mem/fn.drop.html>"
)]
pub(crate) struct DropCopyDiag<'a> {
pub arg_ty: Ty<'a>,
#[label("argument has type `{$arg_ty}`")]
Expand Down Expand Up @@ -937,7 +943,7 @@ pub(crate) enum InvalidFromUtf8Diag {
#[diag("mutation of an interior mutable `const` item with call to `{$method_name}`")]
#[note("each usage of a `const` item creates a new temporary")]
#[note("only the temporaries and never the original `const {$const_name}` will be modified")]
#[help(
#[note(
"for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>"
)]
pub(crate) struct ConstItemInteriorMutationsDiag<'tcx> {
Expand Down Expand Up @@ -1911,10 +1917,10 @@ impl<'a> Diagnostic<'a, ()> for DropGlue<'_> {
#[help(
"if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`"
)]
#[help(
#[note(
"for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>"
)]
#[help(
#[note(
"for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>"
)]
pub(crate) struct IntegerToPtrTransmutes<'tcx> {
Expand Down
27 changes: 25 additions & 2 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,21 +977,44 @@ pub const fn replace<T>(dest: &mut T, src: T) -> T {
/// println!("{}", *borrow);
/// ```
///
/// Integers and other types implementing [`Copy`] are unaffected by `drop`.
/// Integers, shared reference `&T` and other types implementing [`Copy`] are unaffected by `drop`.
///
/// ```
/// # #![allow(dropping_copy_types)]
/// # #![allow(dropping_references)]
/// #[derive(Copy, Clone)]
/// struct Foo(u8);
///
/// let x = 1;
/// let ref_x = &x;
/// let y = Foo(2);
/// drop(x); // a copy of `x` is moved and dropped
/// drop(ref_x); // a copy of `ref_x` is moved and dropped
/// drop(y); // a copy of `y` is moved and dropped
///
/// println!("x: {}, y: {}", x, y.0); // still available
/// println!("x: {}, ref_x: {}, y: {}", x, ref_x, y.0); // still available
/// ```
///
/// Dropping a reference never affects the value it points to. However, calling `drop` on the reference variable behaves differently:
/// - Shared reference `&T`: It implements [`Copy`]. It is copied into `drop`,
/// so the original reference can still be used. The value it points to is not dropped.
/// - Mutable reference `&mut T`: It does not implement [`Copy`]. It is moved
/// into `drop`, so the original reference cannot be used anymore. The value it points to is not dropped.
/// ```
/// # #![allow(dropping_references)]
///
/// let mut x = 42;
///
/// let ref_x = &x;
/// drop(ref_x); // `ref_x` is copied. `x` is not affected.
/// assert_eq!(*ref_x, 42); // `ref_x` can still be used.
/// assert_eq!(x, 42); // `x` can still be used directly.
///
/// let mut_ref_x = &mut x;
/// drop(mut_ref_x); // `mut_ref_x` is moved. `x` is not affected.
/// // *mut_ref_x = 24; // Error: use of moved value.
/// assert_eq!(x, 42); // `x` can still be used directly.
/// ```
/// [`RefCell`]: crate::cell::RefCell
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ LL | () => drop(5 % 3), // No side effects
| |
| argument has type `i32`
|
= note: for more information about `std::mem::drop`, see <https://doc.rust-lang.org/std/mem/fn.drop.html>
= note: use `let _ = ...` to ignore the expression or result
= note: `#[warn(dropping_copy_types)]` on by default

Expand Down
Loading
Loading