]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/let_if_seq.rs
Auto merge of #76931 - oli-obk:const_prop_inline_lint_madness, r=wesleywiser
[rust.git] / src / tools / clippy / tests / ui / let_if_seq.rs
1 #![allow(
2     unused_variables,
3     unused_assignments,
4     clippy::similar_names,
5     clippy::blacklisted_name
6 )]
7 #![warn(clippy::useless_let_if_seq)]
8
9 fn f() -> bool {
10     true
11 }
12 fn g(x: i32) -> i32 {
13     x + 1
14 }
15
16 fn issue985() -> i32 {
17     let mut x = 42;
18     if f() {
19         x = g(x);
20     }
21
22     x
23 }
24
25 fn issue985_alt() -> i32 {
26     let mut x = 42;
27     if f() {
28         f();
29     } else {
30         x = g(x);
31     }
32
33     x
34 }
35
36 #[allow(clippy::manual_strip)]
37 fn issue975() -> String {
38     let mut udn = "dummy".to_string();
39     if udn.starts_with("uuid:") {
40         udn = String::from(&udn[5..]);
41     }
42     udn
43 }
44
45 fn early_return() -> u8 {
46     // FIXME: we could extend the lint to include such cases:
47     let foo;
48
49     if f() {
50         return 42;
51     } else {
52         foo = 0;
53     }
54
55     foo
56 }
57
58 fn main() {
59     early_return();
60     issue975();
61     issue985();
62     issue985_alt();
63
64     let mut foo = 0;
65     if f() {
66         foo = 42;
67     }
68
69     let mut bar = 0;
70     if f() {
71         f();
72         bar = 42;
73     } else {
74         f();
75     }
76
77     let quz;
78     if f() {
79         quz = 42;
80     } else {
81         quz = 0;
82     }
83
84     // `toto` is used several times
85     let mut toto;
86     if f() {
87         toto = 42;
88     } else {
89         for i in &[1, 2] {
90             toto = *i;
91         }
92
93         toto = 2;
94     }
95
96     // found in libcore, the inner if is not a statement but the block's expr
97     let mut ch = b'x';
98     if f() {
99         ch = b'*';
100         if f() {
101             ch = b'?';
102         }
103     }
104
105     // baz needs to be mut
106     let mut baz = 0;
107     if f() {
108         baz = 42;
109     }
110
111     baz = 1337;
112
113     // issue 3043 - types with interior mutability should not trigger this lint
114     use std::cell::Cell;
115     let mut val = Cell::new(1);
116     if true {
117         val = Cell::new(2);
118     }
119     println!("{}", val.get());
120 }