]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/suspicious_to_owned.rs
Rollup merge of #102454 - chenyukang:fix-102396-missing-parentheses, r=lcnr
[rust.git] / src / tools / clippy / tests / ui / suspicious_to_owned.rs
1 #![warn(clippy::suspicious_to_owned)]
2 #![warn(clippy::implicit_clone)]
3 #![allow(clippy::redundant_clone)]
4 use std::borrow::Cow;
5 use std::ffi::{c_char, CStr};
6
7 fn main() {
8     let moo = "Moooo";
9     let c_moo = b"Moooo\0";
10     let c_moo_ptr = c_moo.as_ptr() as *const c_char;
11     let moos = ['M', 'o', 'o'];
12     let moos_vec = moos.to_vec();
13
14     // we expect this to be linted
15     let cow = Cow::Borrowed(moo);
16     let _ = cow.to_owned();
17     // we expect no lints for this
18     let cow = Cow::Borrowed(moo);
19     let _ = cow.into_owned();
20     // we expect no lints for this
21     let cow = Cow::Borrowed(moo);
22     let _ = cow.clone();
23
24     // we expect this to be linted
25     let cow = Cow::Borrowed(&moos);
26     let _ = cow.to_owned();
27     // we expect no lints for this
28     let cow = Cow::Borrowed(&moos);
29     let _ = cow.into_owned();
30     // we expect no lints for this
31     let cow = Cow::Borrowed(&moos);
32     let _ = cow.clone();
33
34     // we expect this to be linted
35     let cow = Cow::Borrowed(&moos_vec);
36     let _ = cow.to_owned();
37     // we expect no lints for this
38     let cow = Cow::Borrowed(&moos_vec);
39     let _ = cow.into_owned();
40     // we expect no lints for this
41     let cow = Cow::Borrowed(&moos_vec);
42     let _ = cow.clone();
43
44     // we expect this to be linted
45     let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
46     let _ = cow.to_owned();
47     // we expect no lints for this
48     let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
49     let _ = cow.into_owned();
50     // we expect no lints for this
51     let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
52     let _ = cow.clone();
53
54     // we expect no lints for these
55     let _ = moo.to_owned();
56     let _ = c_moo.to_owned();
57     let _ = moos.to_owned();
58
59     // we expect implicit_clone lints for these
60     let _ = String::from(moo).to_owned();
61     let _ = moos_vec.to_owned();
62 }