about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/suspicious_to_owned.rs
blob: 2eec05ccaf4cb51cdbe485438c1ec299f843005e (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
//@no-rustfix: overlapping suggestions
#![warn(clippy::suspicious_to_owned)]
#![warn(clippy::implicit_clone)]
#![allow(clippy::redundant_clone)]
use std::borrow::Cow;
use std::ffi::{CStr, c_char};

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();
    //~^ suspicious_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();
    //~^ suspicious_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();
    //~^ suspicious_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();
    //~^ suspicious_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();
    //~^ implicit_clone

    let _ = moos_vec.to_owned();
    //~^ implicit_clone
}