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