]> git.lizzy.rs Git - rust.git/blob - tests/ui/let_if_seq.rs
Auto merge of #3529 - matthiaskrgr:rustfmt_tests, r=phansch
[rust.git] / tests / ui / let_if_seq.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![allow(
11     unused_variables,
12     unused_assignments,
13     clippy::similar_names,
14     clippy::blacklisted_name
15 )]
16 #![warn(clippy::useless_let_if_seq)]
17
18 fn f() -> bool {
19     true
20 }
21 fn g(x: i32) -> i32 {
22     x + 1
23 }
24
25 fn issue985() -> i32 {
26     let mut x = 42;
27     if f() {
28         x = g(x);
29     }
30
31     x
32 }
33
34 fn issue985_alt() -> i32 {
35     let mut x = 42;
36     if f() {
37         f();
38     } else {
39         x = g(x);
40     }
41
42     x
43 }
44
45 fn issue975() -> String {
46     let mut udn = "dummy".to_string();
47     if udn.starts_with("uuid:") {
48         udn = String::from(&udn[5..]);
49     }
50     udn
51 }
52
53 fn early_return() -> u8 {
54     // FIXME: we could extend the lint to include such cases:
55     let foo;
56
57     if f() {
58         return 42;
59     } else {
60         foo = 0;
61     }
62
63     foo
64 }
65
66 fn main() {
67     early_return();
68     issue975();
69     issue985();
70     issue985_alt();
71
72     let mut foo = 0;
73     if f() {
74         foo = 42;
75     }
76
77     let mut bar = 0;
78     if f() {
79         f();
80         bar = 42;
81     } else {
82         f();
83     }
84
85     let quz;
86     if f() {
87         quz = 42;
88     } else {
89         quz = 0;
90     }
91
92     // `toto` is used several times
93     let mut toto;
94     if f() {
95         toto = 42;
96     } else {
97         for i in &[1, 2] {
98             toto = *i;
99         }
100
101         toto = 2;
102     }
103
104     // found in libcore, the inner if is not a statement but the block's expr
105     let mut ch = b'x';
106     if f() {
107         ch = b'*';
108         if f() {
109             ch = b'?';
110         }
111     }
112
113     // baz needs to be mut
114     let mut baz = 0;
115     if f() {
116         baz = 42;
117     }
118
119     baz = 1337;
120 }