Skip to content

Commit 9f41780

Browse files
author
lukas
committed
Mark format! with must_use hint
1 parent 51917ba commit 9f41780

File tree

14 files changed

+104
-100
lines changed

14 files changed

+104
-100
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

+17-17
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
//! Some examples of the [`format!`] extension are:
1313
//!
1414
//! ```
15-
//! format!("Hello"); // => "Hello"
16-
//! format!("Hello, {}!", "world"); // => "Hello, world!"
17-
//! format!("The number is {}", 1); // => "The number is 1"
18-
//! format!("{:?}", (3, 4)); // => "(3, 4)"
19-
//! format!("{value}", value=4); // => "4"
15+
//! let _ = format!("Hello"); // => "Hello"
16+
//! let _ = format!("Hello, {}!", "world"); // => "Hello, world!"
17+
//! let _ = format!("The number is {}", 1); // => "The number is 1"
18+
//! let _ = format!("{:?}", (3, 4)); // => "(3, 4)"
19+
//! let _ = format!("{value}", value=4); // => "4"
2020
//! let people = "Rustaceans";
21-
//! format!("Hello {people}!"); // => "Hello Rustaceans!"
22-
//! format!("{} {}", 1, 2); // => "1 2"
23-
//! format!("{:04}", 42); // => "0042" with leading zeros
24-
//! format!("{:#?}", (100, 200)); // => "(
25-
//! // 100,
26-
//! // 200,
27-
//! // )"
21+
//! let _ = format!("Hello {people}!"); // => "Hello Rustaceans!"
22+
//! let _ = format!("{} {}", 1, 2); // => "1 2"
23+
//! let _ = format!("{:04}", 42); // => "0042" with leading zeros
24+
//! let _ = format!("{:#?}", (100, 200)); // => "(
25+
//! // 100,
26+
//! // 200,
27+
//! // )"
2828
//! ```
2929
//!
3030
//! From these, you can see that the first argument is a format string. It is
@@ -50,7 +50,7 @@
5050
//! the iterator advances. This leads to behavior like this:
5151
//!
5252
//! ```
53-
//! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
53+
//! let _ = format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
5454
//! ```
5555
//!
5656
//! The internal iterator over the argument has not been advanced by the time
@@ -77,17 +77,17 @@
7777
//! For example, the following [`format!`] expressions all use named arguments:
7878
//!
7979
//! ```
80-
//! format!("{argument}", argument = "test"); // => "test"
81-
//! format!("{name} {}", 1, name = 2); // => "2 1"
82-
//! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
80+
//! let _ = format!("{argument}", argument = "test"); // => "test"
81+
//! let _ = format!("{name} {}", 1, name = 2); // => "2 1"
82+
//! let _ = format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
8383
//! ```
8484
//!
8585
//! If a named parameter does not appear in the argument list, `format!` will
8686
//! reference a variable with that name in the current scope.
8787
//!
8888
//! ```
8989
//! let argument = 2 + 2;
90-
//! format!("{argument}"); // => "4"
90+
//! let _ = format!("{argument}"); // => "4"
9191
//!
9292
//! fn make_string(a: u32, b: &str) -> String {
9393
//! format!("{b} {a}")

library/alloc/src/lib.rs

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

261262
#[cfg(test)]

library/alloc/src/macros.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,21 @@ macro_rules! vec {
111111
/// # Examples
112112
///
113113
/// ```
114-
/// format!("test"); // => "test"
115-
/// format!("hello {}", "world!"); // => "hello world!"
116-
/// format!("x = {}, y = {val}", 10, val = 30); // => "x = 10, y = 30"
114+
/// let _ = format!("test"); // => "test"
115+
/// let _ = format!("hello {}", "world!"); // => "hello world!"
116+
/// let _ = format!("x = {}, y = {val}", 10, val = 30); // => "x = 10, y = 30"
117117
/// let (x, y) = (1, 2);
118-
/// format!("{x} + {y} = 3"); // => "1 + 2 = 3"
118+
/// let _ = format!("{x} + {y} = 3"); // => "1 + 2 = 3"
119119
/// ```
120120
#[macro_export]
121121
#[stable(feature = "rust1", since = "1.0.0")]
122+
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
122123
#[cfg_attr(not(test), rustc_diagnostic_item = "format_macro")]
123124
macro_rules! format {
124-
($($arg:tt)*) => {{
125-
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
126-
res
127-
}}
125+
($($arg:tt)*) => {
126+
$crate::__export::must_use({
127+
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
128+
res
129+
})
130+
}
128131
}

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
@@ -755,7 +755,7 @@ fn configure_cmake(
755755
}
756756

757757
if builder.config.llvm_clang_cl.is_some() {
758-
cflags.push(&format!(" --target={target}"));
758+
cflags.push(format!(" --target={target}"));
759759
}
760760
cfg.define("CMAKE_C_FLAGS", cflags);
761761
let mut cxxflags: OsString = builder
@@ -774,7 +774,7 @@ fn configure_cmake(
774774
cxxflags.push(s);
775775
}
776776
if builder.config.llvm_clang_cl.is_some() {
777-
cxxflags.push(&format!(" --target={target}"));
777+
cxxflags.push(format!(" --target={target}"));
778778
}
779779
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
780780
if let Some(ar) = builder.ar(target) {
@@ -915,7 +915,7 @@ impl Step for Lld {
915915
// Find clang's runtime library directory and push that as a search path to the
916916
// cmake linker flags.
917917
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
918-
ldflags.push_all(&format!("/libpath:{}", clang_rt_dir.display()));
918+
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
919919
}
920920
}
921921

src/bootstrap/src/core/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2527,7 +2527,7 @@ impl Cargo {
25272527

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

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
}
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
11
error: invalid format string: field access isn't supported
2-
--> $DIR/struct-field-as-captured-argument.rs:11:15
2+
--> $DIR/struct-field-as-captured-argument.rs:11:23
33
|
4-
LL | format!("{foo.field}");
5-
| ^^^^^^^^^ not supported in format string
4+
LL | let _ = format!("{foo.field}");
5+
| ^^^^^^^^^ not supported in format string
66
|
77
help: consider using a positional formatting argument instead
88
|
9-
LL | format!("{0}", foo.field);
10-
| ~ +++++++++++
9+
LL | let _ = format!("{0}", foo.field);
10+
| ~ +++++++++++
1111

1212
error: invalid format string: field access isn't supported
13-
--> $DIR/struct-field-as-captured-argument.rs:12:15
13+
--> $DIR/struct-field-as-captured-argument.rs:12:23
1414
|
15-
LL | format!("{foo.field} {} {bar}", "aa");
16-
| ^^^^^^^^^ not supported in format string
15+
LL | let _ = format!("{foo.field} {} {bar}", "aa");
16+
| ^^^^^^^^^ not supported in format string
1717
|
1818
help: consider using a positional formatting argument instead
1919
|
20-
LL | format!("{1} {} {bar}", "aa", foo.field);
21-
| ~ +++++++++++
20+
LL | let _ = format!("{1} {} {bar}", "aa", foo.field);
21+
| ~ +++++++++++
2222

2323
error: invalid format string: field access isn't supported
24-
--> $DIR/struct-field-as-captured-argument.rs:13:15
24+
--> $DIR/struct-field-as-captured-argument.rs:13:23
2525
|
26-
LL | format!("{foo.field} {} {1} {bar}", "aa", "bb");
27-
| ^^^^^^^^^ not supported in format string
26+
LL | let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb");
27+
| ^^^^^^^^^ not supported in format string
2828
|
2929
help: consider using a positional formatting argument instead
3030
|
31-
LL | format!("{2} {} {1} {bar}", "aa", "bb", foo.field);
32-
| ~ +++++++++++
31+
LL | let _ = format!("{2} {} {1} {bar}", "aa", "bb", foo.field);
32+
| ~ +++++++++++
3333

3434
error: invalid format string: field access isn't supported
35-
--> $DIR/struct-field-as-captured-argument.rs:14:15
35+
--> $DIR/struct-field-as-captured-argument.rs:14:23
3636
|
37-
LL | format!("{foo.field} {} {baz}", "aa", baz = 3);
38-
| ^^^^^^^^^ not supported in format string
37+
LL | let _ = format!("{foo.field} {} {baz}", "aa", baz = 3);
38+
| ^^^^^^^^^ not supported in format string
3939
|
4040
help: consider using a positional formatting argument instead
4141
|
42-
LL | format!("{1} {} {baz}", "aa", foo.field, baz = 3);
43-
| ~ +++++++++++
42+
LL | let _ = format!("{1} {} {baz}", "aa", foo.field, baz = 3);
43+
| ~ +++++++++++
4444

4545
error: invalid format string: field access isn't supported
46-
--> $DIR/struct-field-as-captured-argument.rs:15:15
46+
--> $DIR/struct-field-as-captured-argument.rs:15:23
4747
|
48-
LL | format!("{foo.field:?} {} {baz}", "aa", baz = 3);
49-
| ^^^^^^^^^ not supported in format string
48+
LL | let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3);
49+
| ^^^^^^^^^ not supported in format string
5050
|
5151
help: consider using a positional formatting argument instead
5252
|
53-
LL | format!("{1:?} {} {baz}", "aa", foo.field, baz = 3);
54-
| ~ +++++++++++
53+
LL | let _ = format!("{1:?} {} {baz}", "aa", foo.field, baz = 3);
54+
| ~ +++++++++++
5555

5656
error: invalid format string: field access isn't supported
57-
--> $DIR/struct-field-as-captured-argument.rs:16:15
57+
--> $DIR/struct-field-as-captured-argument.rs:16:23
5858
|
59-
LL | format!("{foo.field:#?} {} {baz}", "aa", baz = 3);
60-
| ^^^^^^^^^ not supported in format string
59+
LL | let _ = format!("{foo.field:#?} {} {baz}", "aa", baz = 3);
60+
| ^^^^^^^^^ not supported in format string
6161
|
6262
help: consider using a positional formatting argument instead
6363
|
64-
LL | format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3);
65-
| ~ +++++++++++
64+
LL | let _ = format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3);
65+
| ~ +++++++++++
6666

6767
error: invalid format string: field access isn't supported
68-
--> $DIR/struct-field-as-captured-argument.rs:17:15
68+
--> $DIR/struct-field-as-captured-argument.rs:17:23
6969
|
70-
LL | format!("{foo.field:.3} {} {baz}", "aa", baz = 3);
71-
| ^^^^^^^^^ not supported in format string
70+
LL | let _ = format!("{foo.field:.3} {} {baz}", "aa", baz = 3);
71+
| ^^^^^^^^^ not supported in format string
7272
|
7373
help: consider using a positional formatting argument instead
7474
|
75-
LL | format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3);
76-
| ~ +++++++++++
75+
LL | let _ = format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3);
76+
| ~ +++++++++++
7777

7878
error: aborting due to 7 previous errors
7979

tests/ui/issues/issue-20676.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ use std::fmt;
88

99
fn main() {
1010
let a: &dyn fmt::Debug = &1;
11-
format!("{:?}", a);
11+
let _ = format!("{:?}", a);
1212
}

0 commit comments

Comments
 (0)