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