]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/suspicious_to_owned.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / suspicious_to_owned.rs
CommitLineData
781aab86 1//@no-rustfix: overlapping suggestions
f2b60f7d
FG
2#![warn(clippy::suspicious_to_owned)]
3#![warn(clippy::implicit_clone)]
4#![allow(clippy::redundant_clone)]
5use std::borrow::Cow;
6use std::ffi::{c_char, CStr};
7
8fn main() {
9 let moo = "Moooo";
10 let c_moo = b"Moooo\0";
11 let c_moo_ptr = c_moo.as_ptr() as *const c_char;
12 let moos = ['M', 'o', 'o'];
13 let moos_vec = moos.to_vec();
14
15 // we expect this to be linted
16 let cow = Cow::Borrowed(moo);
17 let _ = cow.to_owned();
781aab86
FG
18 //~^ ERROR: this `to_owned` call clones the Cow<'_, str> itself and does not cause the
19 //~| NOTE: `-D clippy::suspicious-to-owned` implied by `-D warnings`
f2b60f7d
FG
20 // we expect no lints for this
21 let cow = Cow::Borrowed(moo);
22 let _ = cow.into_owned();
23 // we expect no lints for this
24 let cow = Cow::Borrowed(moo);
25 let _ = cow.clone();
26
27 // we expect this to be linted
28 let cow = Cow::Borrowed(&moos);
29 let _ = cow.to_owned();
781aab86 30 //~^ ERROR: this `to_owned` call clones the Cow<'_, [char; 3]> itself and does not cau
f2b60f7d
FG
31 // we expect no lints for this
32 let cow = Cow::Borrowed(&moos);
33 let _ = cow.into_owned();
34 // we expect no lints for this
35 let cow = Cow::Borrowed(&moos);
36 let _ = cow.clone();
37
38 // we expect this to be linted
39 let cow = Cow::Borrowed(&moos_vec);
40 let _ = cow.to_owned();
781aab86 41 //~^ ERROR: this `to_owned` call clones the Cow<'_, Vec<char>> itself and does not cau
f2b60f7d
FG
42 // we expect no lints for this
43 let cow = Cow::Borrowed(&moos_vec);
44 let _ = cow.into_owned();
45 // we expect no lints for this
46 let cow = Cow::Borrowed(&moos_vec);
47 let _ = cow.clone();
48
49 // we expect this to be linted
50 let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
51 let _ = cow.to_owned();
781aab86 52 //~^ ERROR: this `to_owned` call clones the Cow<'_, str> itself and does not cause the
f2b60f7d
FG
53 // we expect no lints for this
54 let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
55 let _ = cow.into_owned();
56 // we expect no lints for this
57 let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
58 let _ = cow.clone();
59
60 // we expect no lints for these
61 let _ = moo.to_owned();
62 let _ = c_moo.to_owned();
63 let _ = moos.to_owned();
64
65 // we expect implicit_clone lints for these
66 let _ = String::from(moo).to_owned();
781aab86
FG
67 //~^ ERROR: implicitly cloning a `String` by calling `to_owned` on its dereferenced ty
68 //~| NOTE: `-D clippy::implicit-clone` implied by `-D warnings`
f2b60f7d 69 let _ = moos_vec.to_owned();
781aab86 70 //~^ ERROR: implicitly cloning a `Vec` by calling `to_owned` on its dereferenced type
f2b60f7d 71}