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 2ab317b
Show file tree
Hide file tree
Showing 51 changed files with 269 additions and 999 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
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)]
#[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::hint::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::hint::must_use({ /* compiler built-in */ })
};
($fmt:expr, $($args:tt)*) => {
$crate::hint::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::hint::must_use({ /* compiler built-in */ })
};
($name:expr, $error_msg:expr $(,)?) => {
$crate::hint::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::hint::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::hint::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)]
#[rustc_builtin_macro]
#[macro_export]
macro_rules! concat {
($($e:expr),* $(,)?) => {{ /* compiler built-in */ }};
($($e:expr),* $(,)?) => {
$crate::hint::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)]
#[rustc_builtin_macro]
#[macro_export]
macro_rules! line {
() => {
/* compiler built-in */
$crate::hint::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)]
#[rustc_builtin_macro]
#[macro_export]
macro_rules! column {
() => {
/* compiler built-in */
$crate::hint::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)]
#[rustc_builtin_macro]
#[macro_export]
macro_rules! file {
() => {
/* compiler built-in */
$crate::hint::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)]
#[rustc_builtin_macro]
#[macro_export]
macro_rules! stringify {
($($t:tt)*) => {
/* compiler built-in */
$crate::hint::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::hint::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::hint::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::hint::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
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/box_default.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
let impl1: Box<ImplementsDefault> = Box::default();
let vec: Box<Vec<u8>> = Box::default();
let byte: Box<u8> = Box::default();
let vec2: Box<Vec<ImplementsDefault>> = Box::default();
let vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]);
let vec3: Box<Vec<bool>> = Box::default();

let plain_default = Box::default();
Expand Down
8 changes: 1 addition & 7 deletions src/tools/clippy/tests/ui/box_default.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ error: `Box::new(_)` of default value
LL | let byte: Box<u8> = Box::new(u8::default());
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`

error: `Box::new(_)` of default value
--> tests/ui/box_default.rs:39:45
|
LL | let vec2: Box<Vec<ImplementsDefault>> = Box::new(vec![]);
| ^^^^^^^^^^^^^^^^ help: try: `Box::default()`

error: `Box::new(_)` of default value
--> tests/ui/box_default.rs:40:32
|
Expand All @@ -61,5 +55,5 @@ error: `Box::new(_)` of default value
LL | Self::x(Box::new(T::default()));
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()`

error: aborting due to 10 previous errors
error: aborting due to 9 previous errors

19 changes: 18 additions & 1 deletion src/tools/clippy/tests/ui/derivable_impls.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::collections::HashMap;

#[derive(Default)]
struct FooDefault<'a> {
a: bool,
b: i32,
Expand All @@ -18,6 +17,24 @@ struct FooDefault<'a> {
l: &'a [i32],
}

impl std::default::Default for FooDefault<'_> {
fn default() -> Self {
Self {
a: false,
b: 0,
c: 0u64,
d: vec![],
e: Default::default(),
f: FooND2::default(),
g: HashMap::new(),
h: (0, vec![]),
i: [vec![], vec![], vec![]],
j: [0; 5],
k: None,
l: &[],
}
}
}

#[derive(Default)]
struct TupleDefault(bool, i32, u64);
Expand Down
25 changes: 3 additions & 22 deletions src/tools/clippy/tests/ui/derivable_impls.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
error: this `impl` can be derived
--> tests/ui/derivable_impls.rs:20:1
|
LL | / impl std::default::Default for FooDefault<'_> {
LL | | fn default() -> Self {
LL | | Self {
LL | | a: false,
... |
LL | | }
LL | | }
| |_^
|
= note: `-D clippy::derivable-impls` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::derivable_impls)]`
= help: remove the manual implementation...
help: ...and instead derive it
|
LL + #[derive(Default)]
LL | struct FooDefault<'a> {
|

error: this `impl` can be derived
--> tests/ui/derivable_impls.rs:41:1
|
Expand All @@ -29,6 +8,8 @@ LL | | }
LL | | }
| |_^
|
= note: `-D clippy::derivable-impls` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::derivable_impls)]`
= help: remove the manual implementation...
help: ...and instead derive it
|
Expand Down Expand Up @@ -143,5 +124,5 @@ LL ~ #[default]
LL ~ Bar,
|

error: aborting due to 8 previous errors
error: aborting due to 7 previous errors

2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/eta.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
let c = Some(1u8).map(|a| {1+2; foo}(a));
true.then(|| mac!()); // don't lint function in macro expansion
Some(1).map(closure_mac!()); // don't lint closure in macro expansion
let _: Option<Vec<u8>> = true.then(std::vec::Vec::new); // special case vec!
let _: Option<Vec<u8>> = true.then(|| vec![]); // special case vec!
let d = Some(1u8).map(|a| foo(foo2(a))); //is adjusted?
all(&[1, 2, 3], &&2, below); //is adjusted
unsafe {
Expand Down
8 changes: 1 addition & 7 deletions src/tools/clippy/tests/ui/eta.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ LL | let a = Some(1u8).map(|a| foo(a));
= note: `-D clippy::redundant-closure` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure)]`

error: redundant closure
--> tests/ui/eta.rs:33:40
|
LL | let _: Option<Vec<u8>> = true.then(|| vec![]); // special case vec!
| ^^^^^^^^^ help: replace the closure with `Vec::new`: `std::vec::Vec::new`

error: redundant closure
--> tests/ui/eta.rs:34:35
|
Expand Down Expand Up @@ -202,5 +196,5 @@ error: redundant closure
LL | let x = Box::new(|| None.map(|x| f(x)));
| ^^^^^^^^ help: replace the closure with the function itself: `f`

error: aborting due to 33 previous errors
error: aborting due to 32 previous errors

2 changes: 0 additions & 2 deletions src/tools/clippy/tests/ui/iter_out_of_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ fn main() {
for _ in opaque_empty_iter().skip(1) {}

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

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

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

0 comments on commit 2ab317b

Please sign in to comment.