about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unnecessary_literal_bound.fixed
blob: ec0d2db154cea88f7387a88097e163a7228da547 (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
#![warn(clippy::unnecessary_literal_bound)]

struct Struct<'a> {
    not_literal: &'a str,
}

impl Struct<'_> {
    // Should warn
    fn returns_lit(&self) -> &'static str {
        //~^ unnecessary_literal_bound
        "Hello"
    }

    // Should NOT warn
    fn returns_non_lit(&self) -> &str {
        self.not_literal
    }

    // Should warn, does not currently
    fn conditionally_returns_lit(&self, cond: bool) -> &str {
        if cond { "Literal" } else { "also a literal" }
    }

    // Should NOT warn
    fn conditionally_returns_non_lit(&self, cond: bool) -> &str {
        if cond { "Literal" } else { self.not_literal }
    }

    // Should warn
    fn contionally_returns_literals_explicit(&self, cond: bool) -> &'static str {
        //~^ unnecessary_literal_bound
        if cond {
            return "Literal";
        }

        "also a literal"
    }

    // Should NOT warn
    fn conditionally_returns_non_lit_explicit(&self, cond: bool) -> &str {
        if cond {
            return self.not_literal;
        }

        "Literal"
    }
}

trait ReturnsStr {
    fn trait_method(&self) -> &str;
}

impl ReturnsStr for u8 {
    // Should warn, even though not useful without trait refinement
    fn trait_method(&self) -> &'static str {
        //~^ unnecessary_literal_bound
        "Literal"
    }
}

impl ReturnsStr for Struct<'_> {
    // Should NOT warn
    fn trait_method(&self) -> &str {
        self.not_literal
    }
}

fn main() {}