]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/unwrap_in_result.rs
Auto merge of #84620 - Dylan-DPC:rollup-wkv97im, r=Dylan-DPC
[rust.git] / src / tools / clippy / tests / ui / unwrap_in_result.rs
1 #![warn(clippy::unwrap_in_result)]
2
3 struct A;
4
5 impl A {
6     // should not be detected
7     fn good_divisible_by_3(i_str: String) -> Result<bool, String> {
8         // checks whether a string represents a number divisible by 3
9         let i_result = i_str.parse::<i32>();
10         match i_result {
11             Err(_e) => Err("Not a number".to_string()),
12             Ok(i) => {
13                 if i % 3 == 0 {
14                     return Ok(true);
15                 }
16                 Err("Number is not divisible by 3".to_string())
17             },
18         }
19     }
20
21     // should be detected
22     fn bad_divisible_by_3(i_str: String) -> Result<bool, String> {
23         // checks whether a string represents a number divisible by 3
24         let i = i_str.parse::<i32>().unwrap();
25         if i % 3 == 0 {
26             Ok(true)
27         } else {
28             Err("Number is not divisible by 3".to_string())
29         }
30     }
31
32     fn example_option_expect(i_str: String) -> Option<bool> {
33         let i = i_str.parse::<i32>().expect("not a number");
34         if i % 3 == 0 {
35             return Some(true);
36         }
37         None
38     }
39 }
40
41 fn main() {
42     A::bad_divisible_by_3("3".to_string());
43     A::good_divisible_by_3("3".to_string());
44 }