X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=tests%2Fui%2Fborrow_box.rs;h=2a4f8b53e8b0534a0e2b37beca673cee2f86b3c2;hb=2f48257cfb1732f6824fed7da10c559b01e9554e;hp=3b53aab7e23f234650c6acafbf0c90903e3a8a19;hpb=f11d993c0f0a8282e6721f770c04960ae1d2b2bc;p=rust.git diff --git a/tests/ui/borrow_box.rs b/tests/ui/borrow_box.rs index 3b53aab7e23..2a4f8b53e8b 100644 --- a/tests/ui/borrow_box.rs +++ b/tests/ui/borrow_box.rs @@ -1,9 +1,19 @@ #![deny(clippy::borrowed_box)] -#![allow(clippy::blacklisted_name)] +#![allow(clippy::disallowed_name)] #![allow(unused_variables)] #![allow(dead_code)] +use std::fmt::Display; + pub fn test1(foo: &mut Box) { + // Although this function could be changed to "&mut bool", + // avoiding the Box, mutable references to boxes are not + // flagged by this lint. + // + // This omission is intentional: By passing a mutable Box, + // the memory location of the pointed-to object could be + // modified. By passing a mutable reference, the contents + // could change, but not the location. println!("{:?}", foo) } @@ -27,55 +37,79 @@ fn test4(a: &Box) { use std::any::Any; -pub fn test5(foo: &mut Box) { +pub fn test5(foo: &mut Box) { println!("{:?}", foo) } pub fn test6() { - let foo: &Box; + let foo: &Box; } struct Test7<'a> { - foo: &'a Box, + foo: &'a Box, } trait Test8 { - fn test8(a: &Box); + fn test8(a: &Box); } impl<'a> Test8 for Test7<'a> { - fn test8(a: &Box) { + fn test8(a: &Box) { unimplemented!(); } } -pub fn test9(foo: &mut Box) { +pub fn test9(foo: &mut Box) { let _ = foo; } pub fn test10() { - let foo: &Box; + let foo: &Box; } struct Test11<'a> { - foo: &'a Box, + foo: &'a Box, } trait Test12 { - fn test4(a: &Box); + fn test4(a: &Box); } impl<'a> Test12 for Test11<'a> { - fn test4(a: &Box) { + fn test4(a: &Box) { unimplemented!(); } } +pub fn test13(boxed_slice: &mut Box<[i32]>) { + // Unconditionally replaces the box pointer. + // + // This cannot be accomplished if "&mut [i32]" is passed, + // and provides a test case where passing a reference to + // a Box is valid. + let mut data = vec![12]; + *boxed_slice = data.into_boxed_slice(); +} + +// The suggestion should include proper parentheses to avoid a syntax error. +pub fn test14(_display: &Box) {} +pub fn test15(_display: &Box) {} +pub fn test16<'a>(_display: &'a Box) {} + +pub fn test17(_display: &Box) {} +pub fn test18(_display: &Box) {} +pub fn test19<'a>(_display: &'a Box) {} + +// This exists only to check what happens when parentheses are already present. +// Even though the current implementation doesn't put extra parentheses, +// it's fine that unnecessary parentheses appear in the future for some reason. +pub fn test20(_display: &Box<(dyn Display + Send)>) {} + fn main() { test1(&mut Box::new(false)); test2(); - test5(&mut (Box::new(false) as Box)); + test5(&mut (Box::new(false) as Box)); test6(); - test9(&mut (Box::new(false) as Box)); + test9(&mut (Box::new(false) as Box)); test10(); }