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