Skip to content

Commit 5b6eb28

Browse files
authored
Rollup merge of #127355 - aceArt-GmbH:126475, r=oli-obk
Mark format! with must_use hint Uses unstable feature #94745 Part of #126475 First contribution to rust, please let me know if the blessing of tests is correct Thanks `@bjorn3` for the help
2 parents c4ee2df + 3e9c9a0 commit 5b6eb28

22 files changed

+115
-99
lines changed

library/alloc/src/collections/btree/map/tests.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1796,18 +1796,18 @@ fn test_ord_absence() {
17961796
}
17971797

17981798
fn map_debug<K: Debug>(mut map: BTreeMap<K, ()>) {
1799-
format!("{map:?}");
1800-
format!("{:?}", map.iter());
1801-
format!("{:?}", map.iter_mut());
1802-
format!("{:?}", map.keys());
1803-
format!("{:?}", map.values());
1804-
format!("{:?}", map.values_mut());
1799+
let _ = format!("{map:?}");
1800+
let _ = format!("{:?}", map.iter());
1801+
let _ = format!("{:?}", map.iter_mut());
1802+
let _ = format!("{:?}", map.keys());
1803+
let _ = format!("{:?}", map.values());
1804+
let _ = format!("{:?}", map.values_mut());
18051805
if true {
1806-
format!("{:?}", map.into_iter());
1806+
let _ = format!("{:?}", map.into_iter());
18071807
} else if true {
1808-
format!("{:?}", map.into_keys());
1808+
let _ = format!("{:?}", map.into_keys());
18091809
} else {
1810-
format!("{:?}", map.into_values());
1810+
let _ = format!("{:?}", map.into_values());
18111811
}
18121812
}
18131813

library/alloc/src/collections/btree/set/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,9 @@ fn test_ord_absence() {
705705
}
706706

707707
fn set_debug<K: Debug>(set: BTreeSet<K>) {
708-
format!("{set:?}");
709-
format!("{:?}", set.iter());
710-
format!("{:?}", set.into_iter());
708+
let _ = format!("{set:?}");
709+
let _ = format!("{:?}", set.iter());
710+
let _ = format!("{:?}", set.into_iter());
711711
}
712712

713713
fn set_clone<K: Clone>(mut set: BTreeSet<K>) {

library/alloc/src/fmt.rs

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! Some examples of the [`format!`] extension are:
1313
//!
1414
//! ```
15+
//! # #![allow(unused_must_use)]
1516
//! format!("Hello"); // => "Hello"
1617
//! format!("Hello, {}!", "world"); // => "Hello, world!"
1718
//! format!("The number is {}", 1); // => "The number is 1"
@@ -50,6 +51,7 @@
5051
//! the iterator advances. This leads to behavior like this:
5152
//!
5253
//! ```
54+
//! # #![allow(unused_must_use)]
5355
//! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
5456
//! ```
5557
//!
@@ -77,6 +79,7 @@
7779
//! For example, the following [`format!`] expressions all use named arguments:
7880
//!
7981
//! ```
82+
//! # #![allow(unused_must_use)]
8083
//! format!("{argument}", argument = "test"); // => "test"
8184
//! format!("{name} {}", 1, name = 2); // => "2 1"
8285
//! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
@@ -86,6 +89,7 @@
8689
//! reference a variable with that name in the current scope.
8790
//!
8891
//! ```
92+
//! # #![allow(unused_must_use)]
8993
//! let argument = 2 + 2;
9094
//! format!("{argument}"); // => "4"
9195
//!

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ pub mod vec;
257257
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
258258
pub mod __export {
259259
pub use core::format_args;
260+
pub use core::hint::must_use;
260261
}
261262

262263
#[cfg(test)]

library/alloc/src/macros.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ macro_rules! vec {
111111
/// # Examples
112112
///
113113
/// ```
114+
/// # #![allow(unused_must_use)]
114115
/// format!("test"); // => "test"
115116
/// format!("hello {}", "world!"); // => "hello world!"
116117
/// format!("x = {}, y = {val}", 10, val = 30); // => "x = 10, y = 30"
@@ -119,10 +120,13 @@ macro_rules! vec {
119120
/// ```
120121
#[macro_export]
121122
#[stable(feature = "rust1", since = "1.0.0")]
123+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
122124
#[cfg_attr(not(test), rustc_diagnostic_item = "format_macro")]
123125
macro_rules! format {
124-
($($arg:tt)*) => {{
125-
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
126-
res
127-
}}
126+
($($arg:tt)*) => {
127+
$crate::__export::must_use({
128+
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
129+
res
130+
})
131+
}
128132
}

library/alloc/tests/fmt.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,19 @@ fn test_format_macro_interface() {
217217

218218
// make sure that format! doesn't move out of local variables
219219
let a = Box::new(3);
220-
format!("{a}");
221-
format!("{a}");
220+
let _ = format!("{a}");
221+
let _ = format!("{a}");
222222

223223
// make sure that format! doesn't cause spurious unused-unsafe warnings when
224224
// it's inside of an outer unsafe block
225225
unsafe {
226226
let a: isize = ::std::mem::transmute(3_usize);
227-
format!("{a}");
227+
let _ = format!("{a}");
228228
}
229229

230230
// test that trailing commas are acceptable
231-
format!("{}", "test",);
232-
format!("{foo}", foo = "test",);
231+
let _ = format!("{}", "test",);
232+
let _ = format!("{foo}", foo = "test",);
233233
}
234234

235235
// Basic test to make sure that we can invoke the `write!` macro with an

library/core/tests/fmt/builders.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ mod debug_map {
441441
}
442442
}
443443

444-
format!("{Foo:?}");
444+
let _ = format!("{Foo:?}");
445445
}
446446

447447
#[test]
@@ -455,7 +455,7 @@ mod debug_map {
455455
}
456456
}
457457

458-
format!("{Foo:?}");
458+
let _ = format!("{Foo:?}");
459459
}
460460

461461
#[test]
@@ -469,7 +469,7 @@ mod debug_map {
469469
}
470470
}
471471

472-
format!("{Foo:?}");
472+
let _ = format!("{Foo:?}");
473473
}
474474
}
475475

src/bootstrap/src/core/build_steps/llvm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ fn configure_cmake(
753753
}
754754

755755
if builder.config.llvm_clang_cl.is_some() {
756-
cflags.push(&format!(" --target={target}"));
756+
cflags.push(format!(" --target={target}"));
757757
}
758758
cfg.define("CMAKE_C_FLAGS", cflags);
759759
let mut cxxflags: OsString = builder
@@ -772,7 +772,7 @@ fn configure_cmake(
772772
cxxflags.push(s);
773773
}
774774
if builder.config.llvm_clang_cl.is_some() {
775-
cxxflags.push(&format!(" --target={target}"));
775+
cxxflags.push(format!(" --target={target}"));
776776
}
777777
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
778778
if let Some(ar) = builder.ar(target) {
@@ -913,7 +913,7 @@ impl Step for Lld {
913913
// Find clang's runtime library directory and push that as a search path to the
914914
// cmake linker flags.
915915
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
916-
ldflags.push_all(&format!("/libpath:{}", clang_rt_dir.display()));
916+
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
917917
}
918918
}
919919

src/bootstrap/src/core/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2526,7 +2526,7 @@ impl Cargo {
25262526

25272527
if let Some(target_linker) = builder.linker(target) {
25282528
let target = crate::envify(&target.triple);
2529-
self.command.env(&format!("CARGO_TARGET_{target}_LINKER"), target_linker);
2529+
self.command.env(format!("CARGO_TARGET_{target}_LINKER"), target_linker);
25302530
}
25312531
// We want to set -Clinker using Cargo, therefore we only call `linker_flags` and not
25322532
// `linker_args` here.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn or_fun_call() {
9898

9999
let opt = Some(1);
100100
let hello = "Hello";
101-
let _ = opt.ok_or(format!("{} world.", hello));
101+
let _ = opt.ok_or_else(|| format!("{} world.", hello));
102102

103103
// index
104104
let map = HashMap::<u64, u64>::new();

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ error: use of `unwrap_or` to construct default value
100100
LL | let _ = stringy.unwrap_or(String::new());
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
102102

103+
error: use of `ok_or` followed by a function call
104+
--> tests/ui/or_fun_call.rs:101:17
105+
|
106+
LL | let _ = opt.ok_or(format!("{} world.", hello));
107+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ok_or_else(|| format!("{} world.", hello))`
108+
103109
error: use of `unwrap_or` followed by a function call
104110
--> tests/ui/or_fun_call.rs:105:21
105111
|
@@ -190,5 +196,5 @@ error: use of `unwrap_or_else` to construct default value
190196
LL | let _ = stringy.unwrap_or_else(String::new);
191197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_default()`
192198

193-
error: aborting due to 31 previous errors
199+
error: aborting due to 32 previous errors
194200

src/tools/miri/tests/pass/intptrcast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn cast_dangling() {
3535
fn format() {
3636
// Pointer string formatting! We can't check the output as it changes when libstd changes,
3737
// but we can make sure Miri does not error.
38-
format!("{:?}", &mut 13 as *mut _);
38+
let _ = format!("{:?}", &mut 13 as *mut _);
3939
}
4040

4141
fn transmute() {
@@ -52,7 +52,7 @@ fn ptr_bitops1() {
5252
let one = bytes.as_ptr().wrapping_offset(1);
5353
let three = bytes.as_ptr().wrapping_offset(3);
5454
let res = (one as usize) | (three as usize);
55-
format!("{}", res);
55+
let _ = format!("{}", res);
5656
}
5757

5858
fn ptr_bitops2() {

src/tools/miri/tests/pass/packed_struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn test_derive() {
138138
assert_eq!(x.partial_cmp(&y).unwrap(), x.cmp(&y));
139139
x.hash(&mut DefaultHasher::new());
140140
P::default();
141-
format!("{:?}", x);
141+
let _ = format!("{:?}", x);
142142
}
143143

144144
fn main() {

src/tools/miri/tests/pass/shims/fs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn test_errors() {
202202
// Opening a non-existing file should fail with a "not found" error.
203203
assert_eq!(ErrorKind::NotFound, File::open(&path).unwrap_err().kind());
204204
// Make sure we can also format this.
205-
format!("{0}: {0:?}", File::open(&path).unwrap_err());
205+
let _ = format!("{0}: {0:?}", File::open(&path).unwrap_err());
206206
// Removing a non-existing file should fail with a "not found" error.
207207
assert_eq!(ErrorKind::NotFound, remove_file(&path).unwrap_err().kind());
208208
// Reading the metadata of a non-existing file should fail with a "not found" error.
@@ -301,5 +301,5 @@ fn test_from_raw_os_error() {
301301
let error = Error::from_raw_os_error(code);
302302
assert!(matches!(error.kind(), ErrorKind::Uncategorized));
303303
// Make sure we can also format this.
304-
format!("{error:?}");
304+
let _ = format!("{error:?}");
305305
}

src/tools/miri/tests/pass/shims/io.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ fn main() {
1515
panic!("unsupported OS")
1616
};
1717
let err = io::Error::from_raw_os_error(raw_os_error);
18-
format!("{err}: {err:?}");
18+
let _ = format!("{err}: {err:?}");
1919
}

src/tools/miri/tests/pass/vecdeque.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn main() {
3131
}
3232

3333
// Regression test for Debug impl's
34-
format!("{:?} {:?}", dst, dst.iter());
35-
format!("{:?}", VecDeque::<u32>::new().iter());
34+
let _ = format!("{:?} {:?}", dst, dst.iter());
35+
let _ = format!("{:?}", VecDeque::<u32>::new().iter());
3636

3737
for a in dst {
3838
assert_eq!(*a, 2);

tests/pretty/issue-4264.pp

+11-10
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@
2929

3030

3131

32-
({
33-
let res =
34-
((::alloc::fmt::format as
35-
for<'a> fn(Arguments<'a>) -> String {format})(((format_arguments::new_const
36-
as
37-
fn(&[&'static str; 1]) -> Arguments<'_> {Arguments::<'_>::new_const::<1>})((&([("test"
38-
as &str)] as [&str; 1]) as &[&str; 1])) as Arguments<'_>))
39-
as String);
40-
(res as String)
41-
} as String);
32+
((::alloc::__export::must_use as
33+
fn(String) -> String {must_use::<String>})(({
34+
let res =
35+
((::alloc::fmt::format as
36+
for<'a> fn(Arguments<'a>) -> String {format})(((format_arguments::new_const
37+
as
38+
fn(&[&'static str; 1]) -> Arguments<'_> {Arguments::<'_>::new_const::<1>})((&([("test"
39+
as &str)] as [&str; 1]) as &[&str; 1])) as Arguments<'_>))
40+
as String);
41+
(res as String)
42+
} as String)) as String);
4243
} as ())
4344
type Foo = [i32; (3 as usize)];
4445
struct Bar {

tests/ui/deriving/deriving-in-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ pub fn main() {
99
}
1010

1111
let f = Foo { foo: 10 };
12-
format!("{:?}", f);
12+
let _ = format!("{:?}", f);
1313
}

tests/ui/fmt/struct-field-as-captured-argument.fixed

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ struct Foo {
88
fn main() {
99
let foo = Foo { field: 0 };
1010
let bar = 3;
11-
format!("{0}", foo.field); //~ ERROR invalid format string: field access isn't supported
12-
format!("{1} {} {bar}", "aa", foo.field); //~ ERROR invalid format string: field access isn't supported
13-
format!("{2} {} {1} {bar}", "aa", "bb", foo.field); //~ ERROR invalid format string: field access isn't supported
14-
format!("{1} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
15-
format!("{1:?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
16-
format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
17-
format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
11+
let _ = format!("{0}", foo.field); //~ ERROR invalid format string: field access isn't supported
12+
let _ = format!("{1} {} {bar}", "aa", foo.field); //~ ERROR invalid format string: field access isn't supported
13+
let _ = format!("{2} {} {1} {bar}", "aa", "bb", foo.field); //~ ERROR invalid format string: field access isn't supported
14+
let _ = format!("{1} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
15+
let _ = format!("{1:?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
16+
let _ = format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
17+
let _ = format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
1818
}

tests/ui/fmt/struct-field-as-captured-argument.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ struct Foo {
88
fn main() {
99
let foo = Foo { field: 0 };
1010
let bar = 3;
11-
format!("{foo.field}"); //~ ERROR invalid format string: field access isn't supported
12-
format!("{foo.field} {} {bar}", "aa"); //~ ERROR invalid format string: field access isn't supported
13-
format!("{foo.field} {} {1} {bar}", "aa", "bb"); //~ ERROR invalid format string: field access isn't supported
14-
format!("{foo.field} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
15-
format!("{foo.field:?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
16-
format!("{foo.field:#?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
17-
format!("{foo.field:.3} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
11+
let _ = format!("{foo.field}"); //~ ERROR invalid format string: field access isn't supported
12+
let _ = format!("{foo.field} {} {bar}", "aa"); //~ ERROR invalid format string: field access isn't supported
13+
let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb"); //~ ERROR invalid format string: field access isn't supported
14+
let _ = format!("{foo.field} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
15+
let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
16+
let _ = format!("{foo.field:#?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
17+
let _ = format!("{foo.field:.3} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
1818
}

0 commit comments

Comments
 (0)