From a61b9638bbbb48f9c2fde0ccbbcf03e64494ea0f Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Fri, 25 Sep 2020 23:10:24 +0200 Subject: review: fix nits and move panic safety tests to the correct place --- library/alloc/tests/boxed.rs | 3 +-- library/alloc/tests/fmt.rs | 21 +++++++-------- library/core/src/fmt/mod.rs | 3 ++- library/core/tests/lib.rs | 1 - library/core/tests/option.rs | 3 +-- library/core/tests/panic_safe.rs | 56 ---------------------------------------- library/core/tests/slice.rs | 3 +-- library/std/src/panic.rs | 3 +++ library/std/src/panic/tests.rs | 56 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 74 insertions(+), 75 deletions(-) delete mode 100644 library/core/tests/panic_safe.rs create mode 100644 library/std/src/panic/tests.rs diff --git a/library/alloc/tests/boxed.rs b/library/alloc/tests/boxed.rs index df13b36e778..6a83f5da87c 100644 --- a/library/alloc/tests/boxed.rs +++ b/library/alloc/tests/boxed.rs @@ -1,3 +1,4 @@ +use std::cell::Cell; use std::mem::MaybeUninit; use std::ptr::NonNull; @@ -52,8 +53,6 @@ fn box_clone_from_ptr_stability() { #[test] fn box_deref_lval() { - use std::cell::Cell; - let x = Box::new(Cell::new(5)); x.set(1000); assert_eq!(x.get(), 1000); diff --git a/library/alloc/tests/fmt.rs b/library/alloc/tests/fmt.rs index 6fd6468c3ce..757fddd2418 100644 --- a/library/alloc/tests/fmt.rs +++ b/library/alloc/tests/fmt.rs @@ -1,5 +1,4 @@ #![deny(warnings)] -#![allow(unused_must_use)] use std::cell::RefCell; use std::fmt::{self, Write}; @@ -241,15 +240,15 @@ fn test_format_macro_interface() { #[test] fn test_write() { let mut buf = String::new(); - write!(&mut buf, "{}", 3); + let _ = write!(&mut buf, "{}", 3); { let w = &mut buf; - write!(w, "{foo}", foo = 4); - write!(w, "{}", "hello"); - writeln!(w, "{}", "line"); - writeln!(w, "{foo}", foo = "bar"); - w.write_char('☃'); - w.write_str("str"); + let _ = write!(w, "{foo}", foo = 4); + let _ = write!(w, "{}", "hello"); + let _ = writeln!(w, "{}", "line"); + let _ = writeln!(w, "{foo}", foo = "bar"); + let _ = w.write_char('☃'); + let _ = w.write_str("str"); } t!(buf, "34helloline\nbar\n☃str"); @@ -273,9 +272,9 @@ fn test_format_args() { let mut buf = String::new(); { let w = &mut buf; - write!(w, "{}", format_args!("{}", 1)); - write!(w, "{}", format_args!("test")); - write!(w, "{}", format_args!("{test}", test = 3)); + let _ = write!(w, "{}", format_args!("{}", 1)); + let _ = write!(w, "{}", format_args!("test")); + let _ = write!(w, "{}", format_args!("{test}", test = 3)); } let s = buf; t!(s, "1test3"); diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 8558cb5a5e8..c1038ce4260 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -2238,5 +2238,6 @@ impl Debug for UnsafeCell { } } -// If you expected tests to be here, look instead at the ui/ifmt.rs test, +// If you expected tests to be here, look instead at the core/tests/fmt.rs file, // it's a lot easier than creating all of the rt::Piece structures here. +// There are also tests in the alloc crate, for those that need allocations. diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 4211d0e95a9..4db391f3e56 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -78,7 +78,6 @@ mod nonzero; mod num; mod ops; mod option; -mod panic_safe; mod pattern; mod ptr; mod result; diff --git a/library/core/tests/option.rs b/library/core/tests/option.rs index bd9a5a6a2cd..ae814efec20 100644 --- a/library/core/tests/option.rs +++ b/library/core/tests/option.rs @@ -1,3 +1,4 @@ +use core::cell::Cell; use core::clone::Clone; use core::mem; use core::ops::DerefMut; @@ -375,8 +376,6 @@ fn option_const() { #[test] fn test_unwrap_drop() { - use std::cell::Cell; - struct Dtor<'a> { x: &'a Cell, } diff --git a/library/core/tests/panic_safe.rs b/library/core/tests/panic_safe.rs deleted file mode 100644 index 3104ba539c3..00000000000 --- a/library/core/tests/panic_safe.rs +++ /dev/null @@ -1,56 +0,0 @@ -#![allow(dead_code)] - -use std::cell::RefCell; -use std::panic::{AssertUnwindSafe, UnwindSafe}; -use std::rc::Rc; -use std::sync::{Arc, Mutex, RwLock}; - -struct Foo { - a: i32, -} - -fn assert() {} - -#[test] -fn test_panic_safety_traits() { - assert::(); - assert::<&i32>(); - assert::<*mut i32>(); - assert::<*const i32>(); - assert::(); - assert::(); - assert::<&str>(); - assert::(); - assert::<&Foo>(); - assert::>(); - assert::(); - assert::>(); - assert::>(); - assert::>(); - assert::>(); - assert::<&Mutex>(); - assert::<&RwLock>(); - assert::>(); - assert::>(); - assert::>(); - - { - trait Trait: UnwindSafe {} - assert::>(); - } - - fn bar() { - assert::>(); - assert::>(); - } - - fn baz() { - assert::>(); - assert::>(); - assert::>(); - assert::>(); - assert::<&AssertUnwindSafe>(); - assert::>>(); - assert::>>(); - } -} diff --git a/library/core/tests/slice.rs b/library/core/tests/slice.rs index d66d17c1c85..5ef30b1a889 100644 --- a/library/core/tests/slice.rs +++ b/library/core/tests/slice.rs @@ -1,3 +1,4 @@ +use core::cell::Cell; use core::result::Result::{Err, Ok}; #[test] @@ -1983,8 +1984,6 @@ fn test_is_sorted() { #[test] fn test_slice_run_destructors() { - use core::cell::Cell; - // Make sure that destructors get run on slice literals struct Foo<'a> { x: &'a Cell, diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 18d9c2f11b5..4281867314c 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -411,3 +411,6 @@ pub fn catch_unwind R + UnwindSafe, R>(f: F) -> Result { pub fn resume_unwind(payload: Box) -> ! { panicking::rust_panic_without_hook(payload) } + +#[cfg(test)] +mod tests; diff --git a/library/std/src/panic/tests.rs b/library/std/src/panic/tests.rs new file mode 100644 index 00000000000..b37d74011cc --- /dev/null +++ b/library/std/src/panic/tests.rs @@ -0,0 +1,56 @@ +#![allow(dead_code)] + +use crate::cell::RefCell; +use crate::panic::{AssertUnwindSafe, UnwindSafe}; +use crate::rc::Rc; +use crate::sync::{Arc, Mutex, RwLock}; + +struct Foo { + a: i32, +} + +fn assert() {} + +#[test] +fn panic_safety_traits() { + assert::(); + assert::<&i32>(); + assert::<*mut i32>(); + assert::<*const i32>(); + assert::(); + assert::(); + assert::<&str>(); + assert::(); + assert::<&Foo>(); + assert::>(); + assert::(); + assert::>(); + assert::>(); + assert::>(); + assert::>(); + assert::<&Mutex>(); + assert::<&RwLock>(); + assert::>(); + assert::>(); + assert::>(); + + { + trait Trait: UnwindSafe {} + assert::>(); + } + + fn bar() { + assert::>(); + assert::>(); + } + + fn baz() { + assert::>(); + assert::>(); + assert::>(); + assert::>(); + assert::<&AssertUnwindSafe>(); + assert::>>(); + assert::>>(); + } +} -- cgit 1.4.1-3-g733a5