diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index a886e17f5a9c3..f7b8006978363 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -12,6 +12,7 @@ //! Some examples of the [`format!`] extension are: //! //! ``` +//! # #![allow(unused_must_use)] //! format!("Hello"); // => "Hello" //! format!("Hello, {}!", "world"); // => "Hello, world!" //! format!("The number is {}", 1); // => "The number is 1" @@ -44,7 +45,7 @@ //! the iterator advances. This leads to behavior like this: //! //! ``` -//! format!("{1} {} {0} {}", 1, 2); // => "2 1 1 2" +//! println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2" //! ``` //! //! The internal iterator over the argument has not been advanced by the time @@ -71,6 +72,7 @@ //! For example, the following [`format!`] expressions all use named argument: //! //! ``` +//! # #![allow(unused_must_use)] //! 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" diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index 2f744618d6936..1079e3dcaa762 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -95,6 +95,7 @@ macro_rules! vec { /// # Examples /// /// ``` +/// # #![allow(unused_must_use)] /// format!("test"); /// format!("hello {}", "world!"); /// format!("x = {}, y = {y}", 10, y = 30); diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 339592728ac24..5ebffec6525c8 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -487,6 +487,7 @@ impl str { /// A panic upon overflow: /// /// ```should_panic + /// # #![allow(unused_must_use)] /// // this will panic at runtime /// "0123456789abcdef".repeat(usize::MAX); /// ``` diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 05690e19d23e4..18da3e195c324 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -275,6 +275,7 @@ use crate::vec::Vec; #[derive(PartialOrd, Eq, Ord)] #[cfg_attr(not(test), rustc_diagnostic_item = "string_type")] #[stable(feature = "rust1", since = "1.0.0")] +#[must_use = "unused allocation might not be optimized out by compiler"] pub struct String { vec: Vec, } @@ -949,6 +950,7 @@ impl String { /// # Examples /// /// ``` + /// # #![allow(unused_must_use)] /// #![feature(try_reserve)] /// use std::collections::TryReserveError; /// @@ -987,6 +989,7 @@ impl String { /// # Examples /// /// ``` + /// # #![allow(unused_must_use)] /// #![feature(try_reserve)] /// use std::collections::TryReserveError; /// diff --git a/library/core/tests/fmt/builders.rs b/library/core/tests/fmt/builders.rs index 129c121e8ceac..cd1ccd3550927 100644 --- a/library/core/tests/fmt/builders.rs +++ b/library/core/tests/fmt/builders.rs @@ -446,7 +446,7 @@ mod debug_map { } } - format!("{:?}", Foo); + println!("{:?}", Foo); } #[test] @@ -460,7 +460,7 @@ mod debug_map { } } - format!("{:?}", Foo); + println!("{:?}", Foo); } #[test] @@ -474,7 +474,7 @@ mod debug_map { } } - format!("{:?}", Foo); + println!("{:?}", Foo); } } diff --git a/library/core/tests/option.rs b/library/core/tests/option.rs index fa308160fc228..14da135848737 100644 --- a/library/core/tests/option.rs +++ b/library/core/tests/option.rs @@ -139,7 +139,7 @@ fn test_unwrap_panic1() { #[should_panic] fn test_unwrap_panic2() { let x: Option = None; - x.unwrap(); + drop(x.unwrap()); } #[test] diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 99b31473f87a3..32833f837f10c 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -472,7 +472,7 @@ impl DocFolder for Cache { }); if pushed { - self.stack.pop().expect("stack already empty"); + let _ = self.stack.pop().expect("stack already empty"); } if parent_pushed { self.parent_stack.pop().expect("parent stack already empty"); diff --git a/src/test/ui/block-result/consider-removing-last-semi.fixed b/src/test/ui/block-result/consider-removing-last-semi.fixed index a2ecb73ac5b28..29c9e97e6a4eb 100644 --- a/src/test/ui/block-result/consider-removing-last-semi.fixed +++ b/src/test/ui/block-result/consider-removing-last-semi.fixed @@ -5,6 +5,7 @@ pub fn f() -> String { //~ ERROR mismatched types "bla".to_string() } +#[allow(unused_must_use)] pub fn g() -> String { //~ ERROR mismatched types "this won't work".to_string(); "removeme".to_string() diff --git a/src/test/ui/block-result/consider-removing-last-semi.rs b/src/test/ui/block-result/consider-removing-last-semi.rs index 4991d24b26cce..bf7097b3e685b 100644 --- a/src/test/ui/block-result/consider-removing-last-semi.rs +++ b/src/test/ui/block-result/consider-removing-last-semi.rs @@ -5,6 +5,7 @@ pub fn f() -> String { //~ ERROR mismatched types "bla".to_string(); } +#[allow(unused_must_use)] pub fn g() -> String { //~ ERROR mismatched types "this won't work".to_string(); "removeme".to_string(); diff --git a/src/test/ui/block-result/consider-removing-last-semi.stderr b/src/test/ui/block-result/consider-removing-last-semi.stderr index 15ca8316708a2..953880ca24371 100644 --- a/src/test/ui/block-result/consider-removing-last-semi.stderr +++ b/src/test/ui/block-result/consider-removing-last-semi.stderr @@ -10,7 +10,7 @@ LL | "bla".to_string(); | - help: consider removing this semicolon error[E0308]: mismatched types - --> $DIR/consider-removing-last-semi.rs:8:15 + --> $DIR/consider-removing-last-semi.rs:9:15 | LL | pub fn g() -> String { | - ^^^^^^ expected struct `std::string::String`, found `()` diff --git a/src/test/ui/deriving/deriving-in-fn.rs b/src/test/ui/deriving/deriving-in-fn.rs index 8931e94a4f8d2..d2077dc95b7ed 100644 --- a/src/test/ui/deriving/deriving-in-fn.rs +++ b/src/test/ui/deriving/deriving-in-fn.rs @@ -6,5 +6,5 @@ pub fn main() { } let f = Foo { foo: 10 }; - format!("{:?}", f); + println!("{:?}", f); } diff --git a/src/test/ui/issues/issue-2063.rs b/src/test/ui/issues/issue-2063.rs index 9dbac6ccee1d1..6d37525baa8ca 100644 --- a/src/test/ui/issues/issue-2063.rs +++ b/src/test/ui/issues/issue-2063.rs @@ -14,8 +14,8 @@ impl ToStr2 for T { } #[allow(dead_code)] -fn new_t(x: T) { - x.my_to_string(); +fn new_t(x: T) -> String { + x.my_to_string() } fn main() { diff --git a/src/test/ui/issues/issue-20676.rs b/src/test/ui/issues/issue-20676.rs index 2bc5034960a1b..f6a14ab21af47 100644 --- a/src/test/ui/issues/issue-20676.rs +++ b/src/test/ui/issues/issue-20676.rs @@ -8,5 +8,5 @@ use std::fmt; fn main() { let a: &dyn fmt::Debug = &1; - format!("{:?}", a); + println!("{:?}", a); } diff --git a/src/test/ui/istr.rs b/src/test/ui/istr.rs index dca6d40d59adf..d45ba3dc6a505 100644 --- a/src/test/ui/istr.rs +++ b/src/test/ui/istr.rs @@ -11,6 +11,7 @@ fn test_stack_assign() { assert!((s != u)); } +#[allow(unused_must_use)] fn test_heap_lit() { "a big string".to_string(); } fn test_heap_assign() { diff --git a/src/test/ui/nll/issue-61424.fixed b/src/test/ui/nll/issue-61424.fixed index 63e00c1722e45..1d76932ca1985 100644 --- a/src/test/ui/nll/issue-61424.fixed +++ b/src/test/ui/nll/issue-61424.fixed @@ -5,5 +5,5 @@ fn main() { let x; //~ ERROR: variable does not need to be mutable x = String::new(); - dbg!(x); + let _ = dbg!(x); } diff --git a/src/test/ui/nll/issue-61424.rs b/src/test/ui/nll/issue-61424.rs index 3b64996c27b07..7f7b0d2c78f07 100644 --- a/src/test/ui/nll/issue-61424.rs +++ b/src/test/ui/nll/issue-61424.rs @@ -5,5 +5,5 @@ fn main() { let mut x; //~ ERROR: variable does not need to be mutable x = String::new(); - dbg!(x); + let _ = dbg!(x); } diff --git a/src/test/ui/overloaded/overloaded-deref-count.rs b/src/test/ui/overloaded/overloaded-deref-count.rs index e2f1e10b5c8ae..d531baddd7d22 100644 --- a/src/test/ui/overloaded/overloaded-deref-count.rs +++ b/src/test/ui/overloaded/overloaded-deref-count.rs @@ -64,7 +64,7 @@ pub fn main() { // Immutable deref used for calling a method taking &self. (The // typechecker is smarter now about doing this.) - (*n).to_string(); + let _ = (*n).to_string(); assert_eq!(n.counts(), (3, 3)); // Mutable deref used for calling a method taking &mut self. diff --git a/src/tools/clippy/src/driver.rs b/src/tools/clippy/src/driver.rs index 47315fa64cd80..40a6d94dbe4d2 100644 --- a/src/tools/clippy/src/driver.rs +++ b/src/tools/clippy/src/driver.rs @@ -348,7 +348,7 @@ pub fn main() { // for example `clippy-driver --rustc --version` will print the rustc version that clippy-driver // uses if let Some(pos) = orig_args.iter().position(|arg| arg == "--rustc") { - orig_args.remove(pos); + drop(orig_args.remove(pos)); orig_args[0] = "rustc".to_string(); // if we call "rustc", we need to pass --sysroot here as well @@ -372,7 +372,7 @@ pub fn main() { if wrapper_mode { // we still want to be able to invoke it normally though - orig_args.remove(1); + drop(orig_args.remove(1)); } if !wrapper_mode && (orig_args.iter().any(|a| a == "--help" || a == "-h") || orig_args.len() == 1) { diff --git a/src/tools/clippy/tests/ui/format.fixed b/src/tools/clippy/tests/ui/format.fixed index 306514769990d..933bd14401c0a 100644 --- a/src/tools/clippy/tests/ui/format.fixed +++ b/src/tools/clippy/tests/ui/format.fixed @@ -1,6 +1,6 @@ // run-rustfix -#![allow(clippy::print_literal, clippy::redundant_clone)] +#![allow(unused_must_use, clippy::print_literal, clippy::redundant_clone)] #![warn(clippy::useless_format)] struct Foo(pub String); diff --git a/src/tools/clippy/tests/ui/format.rs b/src/tools/clippy/tests/ui/format.rs index b604d79cca373..95685c3bd1986 100644 --- a/src/tools/clippy/tests/ui/format.rs +++ b/src/tools/clippy/tests/ui/format.rs @@ -1,6 +1,6 @@ // run-rustfix -#![allow(clippy::print_literal, clippy::redundant_clone)] +#![allow(unused_must_use, clippy::print_literal, clippy::redundant_clone)] #![warn(clippy::useless_format)] struct Foo(pub String); diff --git a/src/tools/clippy/tests/ui/question_mark.fixed b/src/tools/clippy/tests/ui/question_mark.fixed index 11dff94a28865..1b7eb69767a6b 100644 --- a/src/tools/clippy/tests/ui/question_mark.fixed +++ b/src/tools/clippy/tests/ui/question_mark.fixed @@ -94,8 +94,8 @@ impl MoveStruct { } fn func() -> Option { - fn f() -> Option { - Some(String::new()) + fn f() -> Option { + Some(42) } f()?; diff --git a/src/tools/clippy/tests/ui/question_mark.rs b/src/tools/clippy/tests/ui/question_mark.rs index 1d0ee82b4f778..488efd2d0eb56 100644 --- a/src/tools/clippy/tests/ui/question_mark.rs +++ b/src/tools/clippy/tests/ui/question_mark.rs @@ -122,8 +122,8 @@ impl MoveStruct { } fn func() -> Option { - fn f() -> Option { - Some(String::new()) + fn f() -> Option { + Some(42) } if f().is_none() { diff --git a/src/tools/clippy/tests/ui/redundant_clone.fixed b/src/tools/clippy/tests/ui/redundant_clone.fixed index cdeefda4c234c..5ff97be54abb6 100644 --- a/src/tools/clippy/tests/ui/redundant_clone.fixed +++ b/src/tools/clippy/tests/ui/redundant_clone.fixed @@ -48,7 +48,7 @@ fn main() { with_branch(Alpha, true); cannot_double_move(Alpha); - cannot_move_from_type_with_drop(); + drop(cannot_move_from_type_with_drop()); borrower_propagation(); not_consumed(); issue_5405(); diff --git a/src/tools/clippy/tests/ui/redundant_clone.rs b/src/tools/clippy/tests/ui/redundant_clone.rs index acb7ffb305f2a..b78bfd4b362b4 100644 --- a/src/tools/clippy/tests/ui/redundant_clone.rs +++ b/src/tools/clippy/tests/ui/redundant_clone.rs @@ -48,7 +48,7 @@ fn main() { with_branch(Alpha, true); cannot_double_move(Alpha); - cannot_move_from_type_with_drop(); + drop(cannot_move_from_type_with_drop()); borrower_propagation(); not_consumed(); issue_5405();