Skip to content

Commit 2ab317b

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

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+269
-999
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/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)]
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::hint::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::hint::must_use({ /* compiler built-in */ })
1018+
};
1019+
($fmt:expr, $($args:tt)*) => {
1020+
$crate::hint::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::hint::must_use({ /* compiler built-in */ })
1093+
};
1094+
($name:expr, $error_msg:expr $(,)?) => {
1095+
$crate::hint::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::hint::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::hint::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)]
11961212
#[rustc_builtin_macro]
11971213
#[macro_export]
11981214
macro_rules! concat {
1199-
($($e:expr),* $(,)?) => {{ /* compiler built-in */ }};
1215+
($($e:expr),* $(,)?) => {
1216+
$crate::hint::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)]
12211240
#[rustc_builtin_macro]
12221241
#[macro_export]
12231242
macro_rules! line {
12241243
() => {
1225-
/* compiler built-in */
1244+
$crate::hint::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)]
12601280
#[rustc_builtin_macro]
12611281
#[macro_export]
12621282
macro_rules! column {
12631283
() => {
1264-
/* compiler built-in */
1284+
$crate::hint::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)]
12851306
#[rustc_builtin_macro]
12861307
#[macro_export]
12871308
macro_rules! file {
12881309
() => {
1289-
/* compiler built-in */
1310+
$crate::hint::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)]
13091331
#[rustc_builtin_macro]
13101332
#[macro_export]
13111333
macro_rules! stringify {
13121334
($($t:tt)*) => {
1313-
/* compiler built-in */
1335+
$crate::hint::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::hint::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::hint::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::hint::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]

src/tools/clippy/tests/ui/box_default.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
let impl1: Box<ImplementsDefault> = Box::default();
3737
let vec: Box<Vec<u8>> = Box::default();
3838
let byte: Box<u8> = Box::default();
39-
let vec2: Box<Vec<ImplementsDefault>> = Box::default();
39+
let vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]);
4040
let vec3: Box<Vec<bool>> = Box::default();
4141

4242
let plain_default = Box::default();

src/tools/clippy/tests/ui/box_default.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ error: `Box::new(_)` of default value
3131
LL | let byte: Box<u8> = Box::new(u8::default());
3232
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`
3333

34-
error: `Box::new(_)` of default value
35-
--> tests/ui/box_default.rs:39:45
36-
|
37-
LL | let vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]);
38-
| ^^^^^^^^^^^^^^^^ help: try: `Box::default()`
39-
4034
error: `Box::new(_)` of default value
4135
--> tests/ui/box_default.rs:40:32
4236
|
@@ -61,5 +55,5 @@ error: `Box::new(_)` of default value
6155
LL | Self::x(Box::new(T::default()));
6256
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`
6357

64-
error: aborting due to 10 previous errors
58+
error: aborting due to 9 previous errors
6559

src/tools/clippy/tests/ui/derivable_impls.fixed

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use std::collections::HashMap;
44

5-
#[derive(Default)]
65
struct FooDefault<'a> {
76
a: bool,
87
b: i32,
@@ -18,6 +17,24 @@ struct FooDefault<'a> {
1817
l: &'a [i32],
1918
}
2019

20+
impl std::default::Default for FooDefault<'_> {
21+
fn default() -> Self {
22+
Self {
23+
a: false,
24+
b: 0,
25+
c: 0u64,
26+
d: vec![],
27+
e: Default::default(),
28+
f: FooND2::default(),
29+
g: HashMap::new(),
30+
h: (0, vec![]),
31+
i: [vec![], vec![], vec![]],
32+
j: [0; 5],
33+
k: None,
34+
l: &[],
35+
}
36+
}
37+
}
2138

2239
#[derive(Default)]
2340
struct TupleDefault(bool, i32, u64);

src/tools/clippy/tests/ui/derivable_impls.stderr

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
error: this `impl` can be derived
2-
--> tests/ui/derivable_impls.rs:20:1
3-
|
4-
LL | / impl std::default::Default for FooDefault<'_> {
5-
LL | | fn default() -> Self {
6-
LL | | Self {
7-
LL | | a: false,
8-
... |
9-
LL | | }
10-
LL | | }
11-
| |_^
12-
|
13-
= note: `-D clippy::derivable-impls` implied by `-D warnings`
14-
= help: to override `-D warnings` add `#[allow(clippy::derivable_impls)]`
15-
= help: remove the manual implementation...
16-
help: ...and instead derive it
17-
|
18-
LL + #[derive(Default)]
19-
LL | struct FooDefault<'a> {
20-
|
21-
221
error: this `impl` can be derived
232
--> tests/ui/derivable_impls.rs:41:1
243
|
@@ -29,6 +8,8 @@ LL | | }
298
LL | | }
309
| |_^
3110
|
11+
= note: `-D clippy::derivable-impls` implied by `-D warnings`
12+
= help: to override `-D warnings` add `#[allow(clippy::derivable_impls)]`
3213
= help: remove the manual implementation...
3314
help: ...and instead derive it
3415
|
@@ -143,5 +124,5 @@ LL ~ #[default]
143124
LL ~ Bar,
144125
|
145126

146-
error: aborting due to 8 previous errors
127+
error: aborting due to 7 previous errors
147128

src/tools/clippy/tests/ui/eta.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn main() {
3030
let c = Some(1u8).map(|a| {1+2; foo}(a));
3131
true.then(|| mac!()); // don't lint function in macro expansion
3232
Some(1).map(closure_mac!()); // don't lint closure in macro expansion
33-
let _: Option<Vec<u8>> = true.then(std::vec::Vec::new); // special case vec!
33+
let _: Option<Vec<u8>> = true.then(|| vec![]); // special case vec!
3434
let d = Some(1u8).map(|a| foo(foo2(a))); //is adjusted?
3535
all(&[1, 2, 3], &&2, below); //is adjusted
3636
unsafe {

src/tools/clippy/tests/ui/eta.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ LL | let a = Some(1u8).map(|a| foo(a));
77
= note: `-D clippy::redundant-closure` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure)]`
99

10-
error: redundant closure
11-
--> tests/ui/eta.rs:33:40
12-
|
13-
LL | let _: Option<Vec<u8>> = true.then(|| vec![]); // special case vec!
14-
| ^^^^^^^^^ help: replace the closure with `Vec::new`: `std::vec::Vec::new`
15-
1610
error: redundant closure
1711
--> tests/ui/eta.rs:34:35
1812
|
@@ -202,5 +196,5 @@ error: redundant closure
202196
LL | let x = Box::new(|| None.map(|x| f(x)));
203197
| ^^^^^^^^ help: replace the closure with the function itself: `f`
204198

205-
error: aborting due to 33 previous errors
199+
error: aborting due to 32 previous errors
206200

src/tools/clippy/tests/ui/iter_out_of_bounds.rs

-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ fn main() {
3232
for _ in opaque_empty_iter().skip(1) {}
3333

3434
for _ in vec![1, 2, 3].iter().skip(4) {}
35-
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
3635

3736
for _ in vec![1; 3].iter().skip(4) {}
38-
//~^ ERROR: this `.skip()` call skips more items than the iterator will produce
3937

4038
let x = [1, 2, 3];
4139
for _ in x.iter().skip(4) {}

0 commit comments

Comments
 (0)