summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/starts_ends_with.rs
blob: e3335dd2e2ef7727f6e92500fbeebb4450a47164 (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
// run-rustfix
#![allow(dead_code, unused_must_use)]

fn main() {}

#[allow(clippy::unnecessary_operation)]
fn starts_with() {
    "".chars().next() == Some(' ');
    Some(' ') != "".chars().next();

    // Ensure that suggestion is escaped correctly
    "".chars().next() == Some('\n');
    Some('\n') != "".chars().next();
}

fn chars_cmp_with_unwrap() {
    let s = String::from("foo");
    if s.chars().next().unwrap() == 'f' {
        // s.starts_with('f')
        // Nothing here
    }
    if s.chars().next_back().unwrap() == 'o' {
        // s.ends_with('o')
        // Nothing here
    }
    if s.chars().last().unwrap() == 'o' {
        // s.ends_with('o')
        // Nothing here
    }
    if s.chars().next().unwrap() != 'f' {
        // !s.starts_with('f')
        // Nothing here
    }
    if s.chars().next_back().unwrap() != 'o' {
        // !s.ends_with('o')
        // Nothing here
    }
    if s.chars().last().unwrap() != '\n' {
        // !s.ends_with('o')
        // Nothing here
    }
}

#[allow(clippy::unnecessary_operation)]
fn ends_with() {
    "".chars().last() == Some(' ');
    Some(' ') != "".chars().last();
    "".chars().next_back() == Some(' ');
    Some(' ') != "".chars().next_back();

    // Ensure that suggestion is escaped correctly
    "".chars().last() == Some('\n');
    Some('\n') != "".chars().last();
}