]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/while_let_loop.rs
Auto merge of #102536 - scottmcm:lookup_line-tweak, r=jackh726
[rust.git] / src / tools / clippy / tests / ui / while_let_loop.rs
1 #![warn(clippy::while_let_loop)]
2 #![allow(clippy::uninlined_format_args)]
3
4 fn main() {
5     let y = Some(true);
6     loop {
7         if let Some(_x) = y {
8             let _v = 1;
9         } else {
10             break;
11         }
12     }
13
14     #[allow(clippy::never_loop)]
15     loop {
16         // no error, break is not in else clause
17         if let Some(_x) = y {
18             let _v = 1;
19         }
20         break;
21     }
22
23     loop {
24         match y {
25             Some(_x) => true,
26             None => break,
27         };
28     }
29
30     loop {
31         let x = match y {
32             Some(x) => x,
33             None => break,
34         };
35         let _x = x;
36         let _str = "foo";
37     }
38
39     loop {
40         let x = match y {
41             Some(x) => x,
42             None => break,
43         };
44         {
45             let _a = "bar";
46         };
47         {
48             let _b = "foobar";
49         }
50     }
51
52     loop {
53         // no error, else branch does something other than break
54         match y {
55             Some(_x) => true,
56             _ => {
57                 let _z = 1;
58                 break;
59             },
60         };
61     }
62
63     while let Some(x) = y {
64         // no error, obviously
65         println!("{}", x);
66     }
67
68     // #675, this used to have a wrong suggestion
69     loop {
70         let (e, l) = match "".split_whitespace().next() {
71             Some(word) => (word.is_empty(), word.len()),
72             None => break,
73         };
74
75         let _ = (e, l);
76     }
77 }
78
79 fn issue771() {
80     let mut a = 100;
81     let b = Some(true);
82     loop {
83         if a > 10 {
84             break;
85         }
86
87         match b {
88             Some(_) => a = 0,
89             None => break,
90         }
91     }
92 }
93
94 fn issue1017() {
95     let r: Result<u32, u32> = Ok(42);
96     let mut len = 1337;
97
98     loop {
99         match r {
100             Err(_) => len = 0,
101             Ok(length) => {
102                 len = length;
103                 break;
104             },
105         }
106     }
107 }
108
109 #[allow(clippy::never_loop)]
110 fn issue1948() {
111     // should not trigger clippy::while_let_loop lint because break passes an expression
112     let a = Some(10);
113     let b = loop {
114         if let Some(c) = a {
115             break Some(c);
116         } else {
117             break None;
118         }
119     };
120 }
121
122 fn issue_7913(m: &std::sync::Mutex<Vec<u32>>) {
123     // Don't lint. The lock shouldn't be held while printing.
124     loop {
125         let x = if let Some(x) = m.lock().unwrap().pop() {
126             x
127         } else {
128             break;
129         };
130
131         println!("{}", x);
132     }
133 }
134
135 fn issue_5715(mut m: core::cell::RefCell<Option<u32>>) {
136     // Don't lint. The temporary from `borrow_mut` must be dropped before overwriting the `RefCell`.
137     loop {
138         let x = if let &mut Some(x) = &mut *m.borrow_mut() {
139             x
140         } else {
141             break;
142         };
143
144         m = core::cell::RefCell::new(Some(x + 1));
145     }
146 }