Skip to content

Commit bc3e32b

Browse files
author
lukas
committed
Mark some macros with must_use hint
1 parent 99b7134 commit bc3e32b

15 files changed

+123
-60
lines changed

library/alloc/src/macros.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@
3838
#[macro_export]
3939
#[stable(feature = "rust1", since = "1.0.0")]
4040
#[rustc_diagnostic_item = "vec_macro"]
41-
#[allow_internal_unstable(rustc_attrs, liballoc_internals)]
41+
#[allow_internal_unstable(rustc_attrs, liballoc_internals, hint_must_use)]
4242
macro_rules! vec {
4343
() => (
44-
$crate::vec::Vec::new()
44+
$crate::__export::must_use($crate::vec::Vec::new())
4545
);
4646
($elem:expr; $n:expr) => (
47-
$crate::vec::from_elem($elem, $n)
47+
$crate::__export::must_use($crate::vec::from_elem($elem, $n))
4848
);
4949
($($x:expr),+ $(,)?) => (
50-
<[_]>::into_vec(
50+
$crate::__export::must_use(<[_]>::into_vec(
5151
// This rustc_box is not required, but it produces a dramatic improvement in compile
5252
// time when constructing arrays with many elements.
5353
#[rustc_box]
5454
$crate::boxed::Box::new([$($x),+])
55-
)
55+
))
5656
);
5757
}
5858

library/core/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,10 @@ pub mod simd {
467467
pub use crate::core_simd::simd::*;
468468
}
469469

470+
#[doc(hidden)]
471+
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
472+
pub mod __export {
473+
pub use core::hint::must_use;
474+
}
475+
470476
include!("primitive_docs.rs");

library/core/src/macros/mod.rs

+44-18
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,16 @@ pub macro debug_assert_matches($($arg:tt)*) {
451451
/// ```
452452
#[macro_export]
453453
#[stable(feature = "matches_macro", since = "1.42.0")]
454+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
454455
#[cfg_attr(not(test), rustc_diagnostic_item = "matches_macro")]
455456
macro_rules! matches {
456457
($expression:expr, $pattern:pat $(if $guard:expr)? $(,)?) => {
457-
match $expression {
458-
$pattern $(if $guard)? => true,
459-
_ => false
460-
}
458+
$crate::__export::must_use({
459+
match $expression {
460+
$pattern $(if $guard)? => true,
461+
_ => false
462+
}
463+
})
461464
};
462465
}
463466

@@ -1010,8 +1013,12 @@ pub(crate) mod builtin {
10101013
#[rustc_builtin_macro]
10111014
#[macro_export]
10121015
macro_rules! format_args {
1013-
($fmt:expr) => {{ /* compiler built-in */ }};
1014-
($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
1016+
($fmt:expr) => {
1017+
$crate::__export::must_use({ /* compiler built-in */ })
1018+
};
1019+
($fmt:expr, $($args:tt)*) => {
1020+
$crate::__export::must_use({ /* compiler built-in */ })
1021+
};
10151022
}
10161023

10171024
/// Same as [`format_args`], but can be used in some const contexts.
@@ -1081,8 +1088,12 @@ pub(crate) mod builtin {
10811088
#[macro_export]
10821089
#[rustc_diagnostic_item = "env_macro"] // useful for external lints
10831090
macro_rules! env {
1084-
($name:expr $(,)?) => {{ /* compiler built-in */ }};
1085-
($name:expr, $error_msg:expr $(,)?) => {{ /* compiler built-in */ }};
1091+
($name:expr $(,)?) => {
1092+
$crate::__export::must_use({ /* compiler built-in */ })
1093+
};
1094+
($name:expr, $error_msg:expr $(,)?) => {
1095+
$crate::__export::must_use({ /* compiler built-in */ })
1096+
};
10861097
}
10871098

10881099
/// Optionally inspects an environment variable at compile time.
@@ -1112,7 +1123,9 @@ pub(crate) mod builtin {
11121123
#[macro_export]
11131124
#[rustc_diagnostic_item = "option_env_macro"] // useful for external lints
11141125
macro_rules! option_env {
1115-
($name:expr $(,)?) => {{ /* compiler built-in */ }};
1126+
($name:expr $(,)?) => {
1127+
$crate::__export::must_use({ /* compiler built-in */ })
1128+
};
11161129
}
11171130

11181131
/// Concatenates identifiers into one identifier.
@@ -1174,7 +1187,9 @@ pub(crate) mod builtin {
11741187
#[rustc_builtin_macro]
11751188
#[macro_export]
11761189
macro_rules! concat_bytes {
1177-
($($e:literal),+ $(,)?) => {{ /* compiler built-in */ }};
1190+
($($e:literal),+ $(,)?) => {
1191+
$crate::__export::must_use({ /* compiler built-in */ })
1192+
};
11781193
}
11791194

11801195
/// Concatenates literals into a static string slice.
@@ -1193,10 +1208,13 @@ pub(crate) mod builtin {
11931208
/// assert_eq!(s, "test10btrue");
11941209
/// ```
11951210
#[stable(feature = "rust1", since = "1.0.0")]
1211+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
11961212
#[rustc_builtin_macro]
11971213
#[macro_export]
11981214
macro_rules! concat {
1199-
($($e:expr),* $(,)?) => {{ /* compiler built-in */ }};
1215+
($($e:expr),* $(,)?) => {
1216+
$crate::__export::must_use({ /* compiler built-in */ })
1217+
};
12001218
}
12011219

12021220
/// Expands to the line number on which it was invoked.
@@ -1218,11 +1236,12 @@ pub(crate) mod builtin {
12181236
/// println!("defined on line: {current_line}");
12191237
/// ```
12201238
#[stable(feature = "rust1", since = "1.0.0")]
1239+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
12211240
#[rustc_builtin_macro]
12221241
#[macro_export]
12231242
macro_rules! line {
12241243
() => {
1225-
/* compiler built-in */
1244+
$crate::__export::must_use({ /* compiler built-in */ })
12261245
};
12271246
}
12281247

@@ -1257,11 +1276,12 @@ pub(crate) mod builtin {
12571276
/// assert_ne!(b, c);
12581277
/// ```
12591278
#[stable(feature = "rust1", since = "1.0.0")]
1279+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
12601280
#[rustc_builtin_macro]
12611281
#[macro_export]
12621282
macro_rules! column {
12631283
() => {
1264-
/* compiler built-in */
1284+
$crate::__export::must_use(/* compiler built-in */)
12651285
};
12661286
}
12671287

@@ -1282,11 +1302,12 @@ pub(crate) mod builtin {
12821302
/// println!("defined in file: {this_file}");
12831303
/// ```
12841304
#[stable(feature = "rust1", since = "1.0.0")]
1305+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
12851306
#[rustc_builtin_macro]
12861307
#[macro_export]
12871308
macro_rules! file {
12881309
() => {
1289-
/* compiler built-in */
1310+
$crate::__export::must_use(/* compiler built-in */)
12901311
};
12911312
}
12921313

@@ -1306,11 +1327,12 @@ pub(crate) mod builtin {
13061327
/// assert_eq!(one_plus_one, "1 + 1");
13071328
/// ```
13081329
#[stable(feature = "rust1", since = "1.0.0")]
1330+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
13091331
#[rustc_builtin_macro]
13101332
#[macro_export]
13111333
macro_rules! stringify {
13121334
($($t:tt)*) => {
1313-
/* compiler built-in */
1335+
$crate::__export::must_use(/* compiler built-in */)
13141336
};
13151337
}
13161338

@@ -1351,7 +1373,9 @@ pub(crate) mod builtin {
13511373
#[macro_export]
13521374
#[cfg_attr(not(test), rustc_diagnostic_item = "include_str_macro")]
13531375
macro_rules! include_str {
1354-
($file:expr $(,)?) => {{ /* compiler built-in */ }};
1376+
($file:expr $(,)?) => {
1377+
$crate::__export::must_use({ /* compiler built-in */ })
1378+
};
13551379
}
13561380

13571381
/// Includes a file as a reference to a byte array.
@@ -1391,7 +1415,9 @@ pub(crate) mod builtin {
13911415
#[macro_export]
13921416
#[cfg_attr(not(test), rustc_diagnostic_item = "include_bytes_macro")]
13931417
macro_rules! include_bytes {
1394-
($file:expr $(,)?) => {{ /* compiler built-in */ }};
1418+
($file:expr $(,)?) => {
1419+
$crate::__export::must_use({ /* compiler built-in */ })
1420+
};
13951421
}
13961422

13971423
/// Expands to a string that represents the current module path.
@@ -1449,7 +1475,7 @@ pub(crate) mod builtin {
14491475
#[macro_export]
14501476
macro_rules! cfg {
14511477
($($cfg:tt)*) => {
1452-
/* compiler built-in */
1478+
$crate::__export::must_use(/* compiler built-in */)
14531479
};
14541480
}
14551481

library/core/tests/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn assert_ne_trailing_comma() {
3939
#[rustfmt::skip]
4040
#[test]
4141
fn matches_leading_pipe() {
42-
matches!(1, | 1 | 2 | 3);
42+
let _ = matches!(1, | 1 | 2 | 3);
4343
}
4444

4545
#[test]

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ fn num_to_digit(_1: char) -> u32 {
88
let _2: std::option::Option<u32>;
99
scope 2 (inlined Option::<u32>::is_some) {
1010
let mut _3: isize;
11+
scope 3 (inlined must_use::<bool>) {
12+
}
1113
}
1214
}
13-
scope 3 (inlined #[track_caller] Option::<u32>::unwrap) {
15+
scope 4 (inlined #[track_caller] Option::<u32>::unwrap) {
1416
let mut _5: isize;
1517
let mut _6: !;
16-
scope 4 {
18+
scope 5 {
1719
}
1820
}
1921

tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ fn num_to_digit(_1: char) -> u32 {
88
let _2: std::option::Option<u32>;
99
scope 2 (inlined Option::<u32>::is_some) {
1010
let mut _3: isize;
11+
scope 3 (inlined must_use::<bool>) {
12+
}
1113
}
1214
}
13-
scope 3 (inlined #[track_caller] Option::<u32>::unwrap) {
15+
scope 4 (inlined #[track_caller] Option::<u32>::unwrap) {
1416
let mut _5: isize;
1517
let mut _6: !;
16-
scope 4 {
18+
scope 5 {
1719
}
1820
}
1921

tests/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff

+36-25
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,58 @@
55
debug bar => _1;
66
let mut _0: ();
77
let mut _2: bool;
8-
let mut _3: isize;
9-
+ let mut _4: isize;
8+
let mut _3: bool;
9+
let mut _4: isize;
10+
+ let mut _5: isize;
1011

1112
bb0: {
1213
StorageLive(_2);
13-
_3 = discriminant(_1);
14-
- switchInt(move _3) -> [0: bb2, otherwise: bb1];
15-
+ StorageLive(_4);
16-
+ _4 = move _3;
17-
+ _2 = Eq(_4, const 0_isize);
18-
+ StorageDead(_4);
19-
+ switchInt(move _2) -> [0: bb2, otherwise: bb1];
14+
StorageLive(_3);
15+
_4 = discriminant(_1);
16+
- switchInt(move _4) -> [0: bb2, otherwise: bb1];
17+
+ StorageLive(_5);
18+
+ _5 = move _4;
19+
+ _3 = Eq(_5, const 0_isize);
20+
+ StorageDead(_5);
21+
+ _2 = must_use::<bool>(move _3) -> [return: bb1, unwind continue];
2022
}
2123

2224
bb1: {
23-
- _2 = const false;
24-
+ _0 = ();
25-
goto -> bb3;
25+
- _3 = const false;
26+
- goto -> bb3;
27+
+ switchInt(move _2) -> [0: bb3, otherwise: bb2];
2628
}
2729

2830
bb2: {
29-
- _2 = const true;
30-
+ _0 = const ();
31-
goto -> bb3;
32-
}
33-
34-
bb3: {
35-
- switchInt(move _2) -> [0: bb5, otherwise: bb4];
31+
- _3 = const true;
32+
- goto -> bb3;
3633
- }
3734
-
38-
- bb4: {
39-
- _0 = ();
40-
- goto -> bb6;
35+
- bb3: {
36+
- _2 = must_use::<bool>(move _3) -> [return: bb4, unwind continue];
4137
- }
4238
-
43-
- bb5: {
44-
- _0 = const ();
45-
- goto -> bb6;
39+
- bb4: {
40+
- switchInt(move _2) -> [0: bb6, otherwise: bb5];
4641
- }
4742
-
43+
- bb5: {
44+
StorageDead(_3);
45+
_0 = ();
46+
- goto -> bb7;
47+
+ goto -> bb4;
48+
}
49+
4850
- bb6: {
51+
+ bb3: {
52+
StorageDead(_3);
53+
_0 = const ();
54+
- goto -> bb7;
55+
+ goto -> bb4;
56+
}
57+
58+
- bb7: {
59+
+ bb4: {
4960
StorageDead(_2);
5061
return;
5162
}

tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
2323
}
2424
scope 7 (inlined Option::<u16>::is_none) {
2525
scope 8 (inlined Option::<u16>::is_some) {
26+
scope 9 (inlined must_use::<bool>) {
27+
}
2628
}
2729
}
28-
scope 9 (inlined core::num::<impl u16>::wrapping_add) {
30+
scope 10 (inlined core::num::<impl u16>::wrapping_add) {
2931
}
3032
}
3133

tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
2323
}
2424
scope 7 (inlined Option::<u16>::is_none) {
2525
scope 8 (inlined Option::<u16>::is_some) {
26+
scope 9 (inlined must_use::<bool>) {
27+
}
2628
}
2729
}
28-
scope 9 (inlined core::num::<impl u16>::wrapping_add) {
30+
scope 10 (inlined core::num::<impl u16>::wrapping_add) {
2931
}
3032
}
3133

tests/mir-opt/pre-codegen/matches_macro.issue_77355_opt.PreCodegen.after.mir

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ fn issue_77355_opt(_1: Foo) -> u64 {
44
debug num => _1;
55
let mut _0: u64;
66
let mut _2: isize;
7+
scope 1 (inlined must_use::<bool>) {
8+
}
79

810
bb0: {
911
_2 = discriminant(_1);

tests/ui/box/unit/unique-create.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ pub fn main() {
77
}
88

99
fn vec() {
10-
vec![0];
10+
let _ = vec![0];
1111
}

0 commit comments

Comments
 (0)