Skip to content

Commit

Permalink
Mark some macros with must_use hint
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas committed Jul 9, 2024
1 parent 99b7134 commit bc3e32b
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 60 deletions.
10 changes: 5 additions & 5 deletions library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),+])
)
))
);
}

Expand Down
6 changes: 6 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
62 changes: 44 additions & 18 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
};
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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 */ })
};
}

Expand Down Expand Up @@ -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 */)
};
}

Expand All @@ -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 */)
};
}

Expand All @@ -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 */)
};
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 */)
};
}

Expand Down
2 changes: 1 addition & 1 deletion library/core/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ fn num_to_digit(_1: char) -> u32 {
let _2: std::option::Option<u32>;
scope 2 (inlined Option::<u32>::is_some) {
let mut _3: isize;
scope 3 (inlined must_use::<bool>) {
}
}
}
scope 3 (inlined #[track_caller] Option::<u32>::unwrap) {
scope 4 (inlined #[track_caller] Option::<u32>::unwrap) {
let mut _5: isize;
let mut _6: !;
scope 4 {
scope 5 {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ fn num_to_digit(_1: char) -> u32 {
let _2: std::option::Option<u32>;
scope 2 (inlined Option::<u32>::is_some) {
let mut _3: isize;
scope 3 (inlined must_use::<bool>) {
}
}
}
scope 3 (inlined #[track_caller] Option::<u32>::unwrap) {
scope 4 (inlined #[track_caller] Option::<u32>::unwrap) {
let mut _5: isize;
let mut _6: !;
scope 4 {
scope 5 {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::<bool>(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::<bool>(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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
}
scope 7 (inlined Option::<u16>::is_none) {
scope 8 (inlined Option::<u16>::is_some) {
scope 9 (inlined must_use::<bool>) {
}
}
}
scope 9 (inlined core::num::<impl u16>::wrapping_add) {
scope 10 (inlined core::num::<impl u16>::wrapping_add) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
}
scope 7 (inlined Option::<u16>::is_none) {
scope 8 (inlined Option::<u16>::is_some) {
scope 9 (inlined must_use::<bool>) {
}
}
}
scope 9 (inlined core::num::<impl u16>::wrapping_add) {
scope 10 (inlined core::num::<impl u16>::wrapping_add) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::<bool>) {
}

bb0: {
_2 = discriminant(_1);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/box/unit/unique-create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub fn main() {
}

fn vec() {
vec![0];
let _ = vec![0];
}
Loading

0 comments on commit bc3e32b

Please sign in to comment.