]> git.lizzy.rs Git - rust.git/blob - tests/ui/let_if_seq.rs
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[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 )]
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 fn issue975() -> String {
37     let mut udn = "dummy".to_string();
38     if udn.starts_with("uuid:") {
39         udn = String::from(&udn[5..]);
40     }
41     udn
42 }
43
44 fn early_return() -> u8 {
45     // FIXME: we could extend the lint to include such cases:
46     let foo;
47
48     if f() {
49         return 42;
50     } else {
51         foo = 0;
52     }
53
54     foo
55 }
56
57 fn main() {
58     early_return();
59     issue975();
60     issue985();
61     issue985_alt();
62
63     let mut foo = 0;
64     if f() {
65         foo = 42;
66     }
67
68     let mut bar = 0;
69     if f() {
70         f();
71         bar = 42;
72     } else {
73         f();
74     }
75
76     let quz;
77     if f() {
78         quz = 42;
79     } else {
80         quz = 0;
81     }
82
83     // `toto` is used several times
84     let mut toto;
85     if f() {
86         toto = 42;
87     } else {
88         for i in &[1, 2] {
89             toto = *i;
90         }
91
92         toto = 2;
93     }
94
95     // found in libcore, the inner if is not a statement but the block's expr
96     let mut ch = b'x';
97     if f() {
98         ch = b'*';
99         if f() {
100             ch = b'?';
101         }
102     }
103
104     // baz needs to be mut
105     let mut baz = 0;
106     if f() {
107         baz = 42;
108     }
109
110     baz = 1337;
111
112     // issue 3043 - types with interior mutability should not trigger this lint
113     use std::cell::Cell;
114     let mut val = Cell::new(1);
115     if true {
116         val = Cell::new(2);
117     }
118     println!("{}", val.get());
119 }