-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathsuspicious_to_owned.rs
62 lines (55 loc) · 1.84 KB
/
suspicious_to_owned.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![warn(clippy::suspicious_to_owned)]
#![warn(clippy::implicit_clone)]
#![allow(clippy::redundant_clone)]
use std::borrow::Cow;
use std::ffi::{c_char, CStr};
fn main() {
let moo = "Moooo";
let c_moo = b"Moooo\0";
let c_moo_ptr = c_moo.as_ptr() as *const c_char;
let moos = ['M', 'o', 'o'];
let moos_vec = moos.to_vec();
// we expect this to be linted
let cow = Cow::Borrowed(moo);
let _ = cow.to_owned();
// we expect no lints for this
let cow = Cow::Borrowed(moo);
let _ = cow.into_owned();
// we expect no lints for this
let cow = Cow::Borrowed(moo);
let _ = cow.clone();
// we expect this to be linted
let cow = Cow::Borrowed(&moos);
let _ = cow.to_owned();
// we expect no lints for this
let cow = Cow::Borrowed(&moos);
let _ = cow.into_owned();
// we expect no lints for this
let cow = Cow::Borrowed(&moos);
let _ = cow.clone();
// we expect this to be linted
let cow = Cow::Borrowed(&moos_vec);
let _ = cow.to_owned();
// we expect no lints for this
let cow = Cow::Borrowed(&moos_vec);
let _ = cow.into_owned();
// we expect no lints for this
let cow = Cow::Borrowed(&moos_vec);
let _ = cow.clone();
// we expect this to be linted
let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
let _ = cow.to_owned();
// we expect no lints for this
let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
let _ = cow.into_owned();
// we expect no lints for this
let cow = unsafe { CStr::from_ptr(c_moo_ptr) }.to_string_lossy();
let _ = cow.clone();
// we expect no lints for these
let _ = moo.to_owned();
let _ = c_moo.to_owned();
let _ = moos.to_owned();
// we expect implicit_clone lints for these
let _ = String::from(moo).to_owned();
let _ = moos_vec.to_owned();
}