diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index 8c6a367869ce0..82f2df6088dfe 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -38,21 +38,21 @@ #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "vec_macro"] -#[allow_internal_unstable(rustc_attrs, liballoc_internals)] +#[allow_internal_unstable(rustc_attrs, liballoc_internals, hint_must_use)] macro_rules! vec { () => ( - $crate::vec::Vec::new() + $crate::__export::must_use($crate::vec::Vec::new()) ); ($elem:expr; $n:expr) => ( - $crate::vec::from_elem($elem, $n) + $crate::__export::must_use($crate::vec::from_elem($elem, $n)) ); ($($x:expr),+ $(,)?) => ( - <[_]>::into_vec( + $crate::__export::must_use(<[_]>::into_vec( // This rustc_box is not required, but it produces a dramatic improvement in compile // time when constructing arrays with many elements. #[rustc_box] $crate::boxed::Box::new([$($x),+]) - ) + )) ); } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 0f82f01e57a71..9e8fab10a1951 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -467,4 +467,10 @@ pub mod simd { pub use crate::core_simd::simd::*; } +#[doc(hidden)] +#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")] +pub mod __export { + pub use core::hint::must_use; +} + include!("primitive_docs.rs"); diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 0d4ca4d5f01e4..64c16d2badb95 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -451,13 +451,16 @@ pub macro debug_assert_matches($($arg:tt)*) { /// ``` #[macro_export] #[stable(feature = "matches_macro", since = "1.42.0")] +#[allow_internal_unstable(hint_must_use, liballoc_internals)] #[cfg_attr(not(test), rustc_diagnostic_item = "matches_macro")] macro_rules! matches { ($expression:expr, $pattern:pat $(if $guard:expr)? $(,)?) => { - match $expression { - $pattern $(if $guard)? => true, - _ => false - } + $crate::__export::must_use({ + match $expression { + $pattern $(if $guard)? => true, + _ => false + } + }) }; } @@ -1010,8 +1013,12 @@ pub(crate) mod builtin { #[rustc_builtin_macro] #[macro_export] macro_rules! format_args { - ($fmt:expr) => {{ /* compiler built-in */ }}; - ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }}; + ($fmt:expr) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; + ($fmt:expr, $($args:tt)*) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; } /// Same as [`format_args`], but can be used in some const contexts. @@ -1081,8 +1088,12 @@ pub(crate) mod builtin { #[macro_export] #[rustc_diagnostic_item = "env_macro"] // useful for external lints macro_rules! env { - ($name:expr $(,)?) => {{ /* compiler built-in */ }}; - ($name:expr, $error_msg:expr $(,)?) => {{ /* compiler built-in */ }}; + ($name:expr $(,)?) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; + ($name:expr, $error_msg:expr $(,)?) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; } /// Optionally inspects an environment variable at compile time. @@ -1112,7 +1123,9 @@ pub(crate) mod builtin { #[macro_export] #[rustc_diagnostic_item = "option_env_macro"] // useful for external lints macro_rules! option_env { - ($name:expr $(,)?) => {{ /* compiler built-in */ }}; + ($name:expr $(,)?) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; } /// Concatenates identifiers into one identifier. @@ -1174,7 +1187,9 @@ pub(crate) mod builtin { #[rustc_builtin_macro] #[macro_export] macro_rules! concat_bytes { - ($($e:literal),+ $(,)?) => {{ /* compiler built-in */ }}; + ($($e:literal),+ $(,)?) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; } /// Concatenates literals into a static string slice. @@ -1193,10 +1208,13 @@ pub(crate) mod builtin { /// assert_eq!(s, "test10btrue"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[allow_internal_unstable(hint_must_use, liballoc_internals)] #[rustc_builtin_macro] #[macro_export] macro_rules! concat { - ($($e:expr),* $(,)?) => {{ /* compiler built-in */ }}; + ($($e:expr),* $(,)?) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; } /// Expands to the line number on which it was invoked. @@ -1218,11 +1236,12 @@ pub(crate) mod builtin { /// println!("defined on line: {current_line}"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[allow_internal_unstable(hint_must_use, liballoc_internals)] #[rustc_builtin_macro] #[macro_export] macro_rules! line { () => { - /* compiler built-in */ + $crate::__export::must_use({ /* compiler built-in */ }) }; } @@ -1257,11 +1276,12 @@ pub(crate) mod builtin { /// assert_ne!(b, c); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[allow_internal_unstable(hint_must_use, liballoc_internals)] #[rustc_builtin_macro] #[macro_export] macro_rules! column { () => { - /* compiler built-in */ + $crate::__export::must_use(/* compiler built-in */) }; } @@ -1282,11 +1302,12 @@ pub(crate) mod builtin { /// println!("defined in file: {this_file}"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[allow_internal_unstable(hint_must_use, liballoc_internals)] #[rustc_builtin_macro] #[macro_export] macro_rules! file { () => { - /* compiler built-in */ + $crate::__export::must_use(/* compiler built-in */) }; } @@ -1306,11 +1327,12 @@ pub(crate) mod builtin { /// assert_eq!(one_plus_one, "1 + 1"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] + #[allow_internal_unstable(hint_must_use, liballoc_internals)] #[rustc_builtin_macro] #[macro_export] macro_rules! stringify { ($($t:tt)*) => { - /* compiler built-in */ + $crate::__export::must_use(/* compiler built-in */) }; } @@ -1351,7 +1373,9 @@ pub(crate) mod builtin { #[macro_export] #[cfg_attr(not(test), rustc_diagnostic_item = "include_str_macro")] macro_rules! include_str { - ($file:expr $(,)?) => {{ /* compiler built-in */ }}; + ($file:expr $(,)?) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; } /// Includes a file as a reference to a byte array. @@ -1391,7 +1415,9 @@ pub(crate) mod builtin { #[macro_export] #[cfg_attr(not(test), rustc_diagnostic_item = "include_bytes_macro")] macro_rules! include_bytes { - ($file:expr $(,)?) => {{ /* compiler built-in */ }}; + ($file:expr $(,)?) => { + $crate::__export::must_use({ /* compiler built-in */ }) + }; } /// Expands to a string that represents the current module path. @@ -1449,7 +1475,7 @@ pub(crate) mod builtin { #[macro_export] macro_rules! cfg { ($($cfg:tt)*) => { - /* compiler built-in */ + $crate::__export::must_use(/* compiler built-in */) }; } diff --git a/library/core/tests/macros.rs b/library/core/tests/macros.rs index 09994fbcbdb78..3357edb7a68d6 100644 --- a/library/core/tests/macros.rs +++ b/library/core/tests/macros.rs @@ -39,7 +39,7 @@ fn assert_ne_trailing_comma() { #[rustfmt::skip] #[test] fn matches_leading_pipe() { - matches!(1, | 1 | 2 | 3); + let _ = matches!(1, | 1 | 2 | 3); } #[test] diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir index 8da56d59aaa60..6525e21520d7e 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir @@ -8,12 +8,14 @@ fn num_to_digit(_1: char) -> u32 { let _2: std::option::Option; scope 2 (inlined Option::::is_some) { let mut _3: isize; + scope 3 (inlined must_use::) { + } } } - scope 3 (inlined #[track_caller] Option::::unwrap) { + scope 4 (inlined #[track_caller] Option::::unwrap) { let mut _5: isize; let mut _6: !; - scope 4 { + scope 5 { } } diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir index 61bc09d901cbf..bd09b49b0434e 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir @@ -8,12 +8,14 @@ fn num_to_digit(_1: char) -> u32 { let _2: std::option::Option; scope 2 (inlined Option::::is_some) { let mut _3: isize; + scope 3 (inlined must_use::) { + } } } - scope 3 (inlined #[track_caller] Option::::unwrap) { + scope 4 (inlined #[track_caller] Option::::unwrap) { let mut _5: isize; let mut _6: !; - scope 4 { + scope 5 { } } diff --git a/tests/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff b/tests/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff index 052e2e126643e..4a082bedec2b4 100644 --- a/tests/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff +++ b/tests/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff @@ -5,47 +5,58 @@ debug bar => _1; let mut _0: (); let mut _2: bool; - let mut _3: isize; -+ let mut _4: isize; + let mut _3: bool; + let mut _4: isize; ++ let mut _5: isize; bb0: { StorageLive(_2); - _3 = discriminant(_1); -- switchInt(move _3) -> [0: bb2, otherwise: bb1]; -+ StorageLive(_4); -+ _4 = move _3; -+ _2 = Eq(_4, const 0_isize); -+ StorageDead(_4); -+ switchInt(move _2) -> [0: bb2, otherwise: bb1]; + StorageLive(_3); + _4 = discriminant(_1); +- switchInt(move _4) -> [0: bb2, otherwise: bb1]; ++ StorageLive(_5); ++ _5 = move _4; ++ _3 = Eq(_5, const 0_isize); ++ StorageDead(_5); ++ _2 = must_use::(move _3) -> [return: bb1, unwind continue]; } bb1: { -- _2 = const false; -+ _0 = (); - goto -> bb3; +- _3 = const false; +- goto -> bb3; ++ switchInt(move _2) -> [0: bb3, otherwise: bb2]; } bb2: { -- _2 = const true; -+ _0 = const (); - goto -> bb3; - } - - bb3: { -- switchInt(move _2) -> [0: bb5, otherwise: bb4]; +- _3 = const true; +- goto -> bb3; - } - -- bb4: { -- _0 = (); -- goto -> bb6; +- bb3: { +- _2 = must_use::(move _3) -> [return: bb4, unwind continue]; - } - -- bb5: { -- _0 = const (); -- goto -> bb6; +- bb4: { +- switchInt(move _2) -> [0: bb6, otherwise: bb5]; - } - +- bb5: { + StorageDead(_3); + _0 = (); +- goto -> bb7; ++ goto -> bb4; + } + - bb6: { ++ bb3: { + StorageDead(_3); + _0 = const (); +- goto -> bb7; ++ goto -> bb4; + } + +- bb7: { ++ bb4: { StorageDead(_2); return; } diff --git a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir index 69c11ebcacced..d56195850defd 100644 --- a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir @@ -23,9 +23,11 @@ fn step_forward(_1: u16, _2: usize) -> u16 { } scope 7 (inlined Option::::is_none) { scope 8 (inlined Option::::is_some) { + scope 9 (inlined must_use::) { + } } } - scope 9 (inlined core::num::::wrapping_add) { + scope 10 (inlined core::num::::wrapping_add) { } } diff --git a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir index e6ea6c510019c..114b891a3b1be 100644 --- a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir @@ -23,9 +23,11 @@ fn step_forward(_1: u16, _2: usize) -> u16 { } scope 7 (inlined Option::::is_none) { scope 8 (inlined Option::::is_some) { + scope 9 (inlined must_use::) { + } } } - scope 9 (inlined core::num::::wrapping_add) { + scope 10 (inlined core::num::::wrapping_add) { } } diff --git a/tests/mir-opt/pre-codegen/matches_macro.issue_77355_opt.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/matches_macro.issue_77355_opt.PreCodegen.after.mir index d41135c6a4fa3..7a5d5d1d579f6 100644 --- a/tests/mir-opt/pre-codegen/matches_macro.issue_77355_opt.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/matches_macro.issue_77355_opt.PreCodegen.after.mir @@ -4,6 +4,8 @@ fn issue_77355_opt(_1: Foo) -> u64 { debug num => _1; let mut _0: u64; let mut _2: isize; + scope 1 (inlined must_use::) { + } bb0: { _2 = discriminant(_1); diff --git a/tests/ui/box/unit/unique-create.rs b/tests/ui/box/unit/unique-create.rs index bf3826156b1d4..a9691f068ab16 100644 --- a/tests/ui/box/unit/unique-create.rs +++ b/tests/ui/box/unit/unique-create.rs @@ -7,5 +7,5 @@ pub fn main() { } fn vec() { - vec![0]; + let _ = vec![0]; } diff --git a/tests/ui/issues/issue-13446.stderr b/tests/ui/issues/issue-13446.stderr index 28c459e6e62ce..63e23a549dff8 100644 --- a/tests/ui/issues/issue-13446.stderr +++ b/tests/ui/issues/issue-13446.stderr @@ -2,10 +2,20 @@ error[E0308]: mismatched types --> $DIR/issue-13446.rs:3:26 | LL | static VEC: [u32; 256] = vec![]; - | ^^^^^^ expected `[u32; 256]`, found `Vec<_>` + | ^^^^^^ + | | + | expected `[u32; 256]`, found `Vec<_>` + | arguments to this function are incorrect | = note: expected array `[u32; 256]` found struct `Vec<_>` +help: the return type of this call is `Vec<_>` due to the type of the argument passed + --> $DIR/issue-13446.rs:3:26 + | +LL | static VEC: [u32; 256] = vec![]; + | ^^^^^^ this argument influences the return type of `$crate` +note: function defined here + --> $SRC_DIR/core/src/hint.rs:LL:COL = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error diff --git a/tests/ui/rfcs/rfc-1857-stabilize-drop-order/drop-order.rs b/tests/ui/rfcs/rfc-1857-stabilize-drop-order/drop-order.rs index d5f6628e0dbc1..4495c193bd759 100644 --- a/tests/ui/rfcs/rfc-1857-stabilize-drop-order/drop-order.rs +++ b/tests/ui/rfcs/rfc-1857-stabilize-drop-order/drop-order.rs @@ -195,7 +195,7 @@ fn test_drop_list() { let dropped_fields = Rc::new(RefCell::new(Vec::new())); let cloned = AssertUnwindSafe(dropped_fields.clone()); panic::catch_unwind(|| { - vec![ + let _ = vec![ PushOnDrop::new(2, cloned.clone()), PushOnDrop::new(1, cloned.clone()), panic!("this panic is caught :D") diff --git a/tests/ui/rust-2018/remove-extern-crate.fixed b/tests/ui/rust-2018/remove-extern-crate.fixed index 19b1dc6fb0130..a6aa842238487 100644 --- a/tests/ui/rust-2018/remove-extern-crate.fixed +++ b/tests/ui/rust-2018/remove-extern-crate.fixed @@ -28,7 +28,7 @@ fn main() { with_visibility::foo(); remove_extern_crate::foo!(); bar!(); - alloc::vec![5]; + let _ = alloc::vec![5]; } mod another { diff --git a/tests/ui/rust-2018/remove-extern-crate.rs b/tests/ui/rust-2018/remove-extern-crate.rs index 88ef858da147f..ce194644f7ef5 100644 --- a/tests/ui/rust-2018/remove-extern-crate.rs +++ b/tests/ui/rust-2018/remove-extern-crate.rs @@ -28,7 +28,7 @@ fn main() { with_visibility::foo(); remove_extern_crate::foo!(); bar!(); - alloc::vec![5]; + let _ = alloc::vec![5]; } mod another {