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