Skip to content

Commit

Permalink
Mark format! with must_use hint
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas committed Jul 5, 2024
1 parent 51917ba commit 21650b1
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 111 deletions.
18 changes: 9 additions & 9 deletions library/alloc/src/collections/btree/map/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1796,18 +1796,18 @@ fn test_ord_absence() {
}

fn map_debug<K: Debug>(mut map: BTreeMap<K, ()>) {
format!("{map:?}");
format!("{:?}", map.iter());
format!("{:?}", map.iter_mut());
format!("{:?}", map.keys());
format!("{:?}", map.values());
format!("{:?}", map.values_mut());
let _ = format!("{map:?}");
let _ = format!("{:?}", map.iter());
let _ = format!("{:?}", map.iter_mut());
let _ = format!("{:?}", map.keys());
let _ = format!("{:?}", map.values());
let _ = format!("{:?}", map.values_mut());
if true {
format!("{:?}", map.into_iter());
let _ = format!("{:?}", map.into_iter());
} else if true {
format!("{:?}", map.into_keys());
let _ = format!("{:?}", map.into_keys());
} else {
format!("{:?}", map.into_values());
let _ = format!("{:?}", map.into_values());
}
}

Expand Down
6 changes: 3 additions & 3 deletions library/alloc/src/collections/btree/set/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ fn test_ord_absence() {
}

fn set_debug<K: Debug>(set: BTreeSet<K>) {
format!("{set:?}");
format!("{:?}", set.iter());
format!("{:?}", set.into_iter());
let _ = format!("{set:?}");
let _ = format!("{:?}", set.iter());
let _ = format!("{:?}", set.into_iter());
}

fn set_clone<K: Clone>(mut set: BTreeSet<K>) {
Expand Down
34 changes: 17 additions & 17 deletions library/alloc/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
//! Some examples of the [`format!`] extension are:
//!
//! ```
//! format!("Hello"); // => "Hello"
//! format!("Hello, {}!", "world"); // => "Hello, world!"
//! format!("The number is {}", 1); // => "The number is 1"
//! format!("{:?}", (3, 4)); // => "(3, 4)"
//! format!("{value}", value=4); // => "4"
//! let _ = format!("Hello"); // => "Hello"
//! let _ = format!("Hello, {}!", "world"); // => "Hello, world!"
//! let _ = format!("The number is {}", 1); // => "The number is 1"
//! let _ = format!("{:?}", (3, 4)); // => "(3, 4)"
//! let _ = format!("{value}", value=4); // => "4"
//! let people = "Rustaceans";
//! format!("Hello {people}!"); // => "Hello Rustaceans!"
//! format!("{} {}", 1, 2); // => "1 2"
//! format!("{:04}", 42); // => "0042" with leading zeros
//! format!("{:#?}", (100, 200)); // => "(
//! // 100,
//! // 200,
//! // )"
//! let _ = format!("Hello {people}!"); // => "Hello Rustaceans!"
//! let _ = format!("{} {}", 1, 2); // => "1 2"
//! let _ = format!("{:04}", 42); // => "0042" with leading zeros
//! let _ = format!("{:#?}", (100, 200)); // => "(
//! // 100,
//! // 200,
//! // )"
//! ```
//!
//! From these, you can see that the first argument is a format string. It is
Expand All @@ -50,7 +50,7 @@
//! the iterator advances. This leads to behavior like this:
//!
//! ```
//! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
//! let _ = format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
//! ```
//!
//! The internal iterator over the argument has not been advanced by the time
Expand All @@ -77,17 +77,17 @@
//! For example, the following [`format!`] expressions all use named arguments:
//!
//! ```
//! format!("{argument}", argument = "test"); // => "test"
//! format!("{name} {}", 1, name = 2); // => "2 1"
//! format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
//! let _ = format!("{argument}", argument = "test"); // => "test"
//! let _ = format!("{name} {}", 1, name = 2); // => "2 1"
//! let _ = format!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
//! ```
//!
//! If a named parameter does not appear in the argument list, `format!` will
//! reference a variable with that name in the current scope.
//!
//! ```
//! let argument = 2 + 2;
//! format!("{argument}"); // => "4"
//! let _ = format!("{argument}"); // => "4"
//!
//! fn make_string(a: u32, b: &str) -> String {
//! format!("{b} {a}")
Expand Down
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub mod vec;
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
pub mod __export {
pub use core::format_args;
pub use core::hint::must_use;
}

#[cfg(test)]
Expand Down
19 changes: 11 additions & 8 deletions library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,21 @@ macro_rules! vec {
/// # Examples
///
/// ```
/// format!("test"); // => "test"
/// format!("hello {}", "world!"); // => "hello world!"
/// format!("x = {}, y = {val}", 10, val = 30); // => "x = 10, y = 30"
/// let _ = format!("test"); // => "test"
/// let _ = format!("hello {}", "world!"); // => "hello world!"
/// let _ = format!("x = {}, y = {val}", 10, val = 30); // => "x = 10, y = 30"
/// let (x, y) = (1, 2);
/// format!("{x} + {y} = 3"); // => "1 + 2 = 3"
/// let _ = format!("{x} + {y} = 3"); // => "1 + 2 = 3"
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable(hint_must_use, liballoc_internals)]
#[cfg_attr(not(test), rustc_diagnostic_item = "format_macro")]
macro_rules! format {
($($arg:tt)*) => {{
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
res
}}
($($arg:tt)*) => {
$crate::__export::must_use({
let res = $crate::fmt::format($crate::__export::format_args!($($arg)*));
res
})
}
}
10 changes: 5 additions & 5 deletions library/alloc/tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,19 @@ fn test_format_macro_interface() {

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

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

// test that trailing commas are acceptable
format!("{}", "test",);
format!("{foo}", foo = "test",);
let _ = format!("{}", "test",);
let _ = format!("{foo}", foo = "test",);
}

// Basic test to make sure that we can invoke the `write!` macro with an
Expand Down
6 changes: 3 additions & 3 deletions library/core/tests/fmt/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ mod debug_map {
}
}

format!("{Foo:?}");
let _ = format!("{Foo:?}");
}

#[test]
Expand All @@ -455,7 +455,7 @@ mod debug_map {
}
}

format!("{Foo:?}");
let _ = format!("{Foo:?}");
}

#[test]
Expand All @@ -469,7 +469,7 @@ mod debug_map {
}
}

format!("{Foo:?}");
let _ = format!("{Foo:?}");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ fn configure_cmake(
}

if builder.config.llvm_clang_cl.is_some() {
cflags.push(&format!(" --target={target}"));
cflags.push(format!(" --target={target}"));
}
cfg.define("CMAKE_C_FLAGS", cflags);
let mut cxxflags: OsString = builder
Expand All @@ -774,7 +774,7 @@ fn configure_cmake(
cxxflags.push(s);
}
if builder.config.llvm_clang_cl.is_some() {
cxxflags.push(&format!(" --target={target}"));
cxxflags.push(format!(" --target={target}"));
}
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
if let Some(ar) = builder.ar(target) {
Expand Down Expand Up @@ -915,7 +915,7 @@ impl Step for Lld {
// Find clang's runtime library directory and push that as a search path to the
// cmake linker flags.
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
ldflags.push_all(&format!("/libpath:{}", clang_rt_dir.display()));
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,7 @@ impl Cargo {

if let Some(target_linker) = builder.linker(target) {
let target = crate::envify(&target.triple);
self.command.env(&format!("CARGO_TARGET_{target}_LINKER"), target_linker);
self.command.env(format!("CARGO_TARGET_{target}_LINKER"), target_linker);
}
// We want to set -Clinker using Cargo, therefore we only call `linker_flags` and not
// `linker_args` here.
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/or_fun_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn or_fun_call() {

let opt = Some(1);
let hello = "Hello";
let _ = opt.ok_or(format!("{} world.", hello));
let _ = opt.ok_or_else(|| format!("{} world.", hello));

// index
let map = HashMap::<u64, u64>::new();
Expand Down
21 changes: 11 additions & 10 deletions tests/pretty/issue-4264.pp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@



({
let res =
((::alloc::fmt::format as
for<'a> fn(Arguments<'a>) -> String {format})(((format_arguments::new_const
as
fn(&[&'static str; 1]) -> Arguments<'_> {Arguments::<'_>::new_const::<1>})((&([("test"
as &str)] as [&str; 1]) as &[&str; 1])) as Arguments<'_>))
as String);
(res as String)
} as String);
((::alloc::__export::must_use as
fn(String) -> String {must_use::<String>})(({
let res =
((::alloc::fmt::format as
for<'a> fn(Arguments<'a>) -> String {format})(((format_arguments::new_const
as
fn(&[&'static str; 1]) -> Arguments<'_> {Arguments::<'_>::new_const::<1>})((&([("test"
as &str)] as [&str; 1]) as &[&str; 1])) as Arguments<'_>))
as String);
(res as String)
} as String)) as String);
} as ())
type Foo = [i32; (3 as usize)];
struct Bar {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/deriving/deriving-in-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ pub fn main() {
}

let f = Foo { foo: 10 };
format!("{:?}", f);
let _ = format!("{:?}", f);
}
14 changes: 7 additions & 7 deletions tests/ui/fmt/struct-field-as-captured-argument.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ struct Foo {
fn main() {
let foo = Foo { field: 0 };
let bar = 3;
format!("{0}", foo.field); //~ ERROR invalid format string: field access isn't supported
format!("{1} {} {bar}", "aa", foo.field); //~ ERROR invalid format string: field access isn't supported
format!("{2} {} {1} {bar}", "aa", "bb", foo.field); //~ ERROR invalid format string: field access isn't supported
format!("{1} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
format!("{1:?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{0}", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1} {} {bar}", "aa", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{2} {} {1} {bar}", "aa", "bb", foo.field); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1:?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1:#?} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{1:.3} {} {baz}", "aa", foo.field, baz = 3); //~ ERROR invalid format string: field access isn't supported
}
14 changes: 7 additions & 7 deletions tests/ui/fmt/struct-field-as-captured-argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ struct Foo {
fn main() {
let foo = Foo { field: 0 };
let bar = 3;
format!("{foo.field}"); //~ ERROR invalid format string: field access isn't supported
format!("{foo.field} {} {bar}", "aa"); //~ ERROR invalid format string: field access isn't supported
format!("{foo.field} {} {1} {bar}", "aa", "bb"); //~ ERROR invalid format string: field access isn't supported
format!("{foo.field} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
format!("{foo.field:?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
format!("{foo.field:#?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
format!("{foo.field:.3} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field}"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {bar}", "aa"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {1} {bar}", "aa", "bb"); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field:?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field:#?} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
let _ = format!("{foo.field:.3} {} {baz}", "aa", baz = 3); //~ ERROR invalid format string: field access isn't supported
}
Loading

0 comments on commit 21650b1

Please sign in to comment.