|
1 | | -use std::io; |
| 1 | +use std::convert::TryInto; |
2 | 2 | use std::fs::File; |
| 3 | +use std::io; |
3 | 4 | use std::io::Read; |
4 | | -use std::convert::TryInto; |
5 | 5 |
|
6 | 6 | fn main() { |
7 | 7 | println!("Enter a file name: "); |
8 | 8 | let mut input = String::new(); |
9 | 9 |
|
10 | | - io::stdin().read_line(&mut input) |
| 10 | + io::stdin() |
| 11 | + .read_line(&mut input) |
11 | 12 | .expect("error: unable to read user input"); |
12 | 13 |
|
13 | 14 | let _ = match get_file_content(&input.trim()) { |
14 | | - Ok(c) => println!("{}", c), |
| 15 | + Ok(c) => println!("{}", c), |
15 | 16 | Err(e) => panic!("Unable to parse file. {}", e), |
16 | 17 | }; |
17 | 18 |
|
18 | | - match get_file_content_2(&input.trim()){ |
| 19 | + match get_file_content_2(&input.trim()) { |
19 | 20 | Ok(_) => println!("Success"), |
20 | | - Err(e) => panic!("{}", e) |
| 21 | + Err(e) => panic!("{}", e), |
21 | 22 | } |
22 | 23 |
|
23 | 24 | get_file_content_3(&input).unwrap(); |
24 | 25 | get_file_content_4(&input).unwrap(); |
25 | 26 | } |
26 | 27 |
|
27 | | -fn get_file_content(path:&str) -> Result<String, std::io::Error> { |
| 28 | +fn get_file_content(path: &str) -> Result<String, std::io::Error> { |
28 | 29 | // ruleid: panic-in-function-returning-result |
29 | 30 | let mut f = File::open(path).unwrap(); |
30 | 31 | let mut s = String::new(); |
31 | 32 | // ruleid: panic-in-function-returning-result |
32 | 33 | f.read_to_string(&mut s).expect("oh"); |
33 | | - return Ok(s) |
| 34 | + return Ok(s); |
34 | 35 | } |
35 | 36 |
|
36 | | -fn get_file_content_2(path:&str) -> io::Result<()> { |
| 37 | +fn get_file_content_2(path: &str) -> io::Result<()> { |
37 | 38 | // ruleid: panic-in-function-returning-result |
38 | 39 | let mut f = File::open(path).expect("uh"); |
39 | 40 | let mut s = String::new(); |
40 | 41 | // ruleid: panic-in-function-returning-result |
41 | 42 | f.read_to_string(&mut s).unwrap(); |
42 | 43 | println!("{}", s); |
43 | | - return Ok(()) |
| 44 | + return Ok(()); |
44 | 45 | } |
45 | 46 |
|
46 | | - |
47 | 47 | type CustomResult = Result<f64, String>; |
48 | 48 |
|
49 | 49 | thread_local!(static GLOB: [i32; 10] = { |
50 | 50 | // ok: panic-in-function-returning-result |
51 | 51 | (0..10).collect::<Vec<i32>>().try_into().unwrap() |
52 | 52 | }); |
53 | 53 |
|
54 | | -fn get_file_content_3(path:&str) -> CustomResult { |
| 54 | +fn get_file_content_3(path: &str) -> CustomResult { |
55 | 55 | // ruleid: panic-in-function-returning-result |
56 | 56 | let mut f = File::open(path).unwrap(); |
57 | 57 | let mut s = String::new(); |
58 | 58 | // ruleid: panic-in-function-returning-result |
59 | 59 | f.read_to_string(&mut s).unwrap(); |
60 | 60 | println!("{}", s); |
61 | | - return Ok(1.) |
| 61 | + return Ok(1.); |
62 | 62 | } |
63 | 63 |
|
64 | | - |
65 | | -fn get_file_content_4(path:&str) -> io::Result<()> { |
| 64 | +fn get_file_content_4(path: &str) -> io::Result<()> { |
66 | 65 | // ok: panic-in-function-returning-result |
67 | 66 | let mut f = File::open(path)?; |
68 | 67 | let mut s = String::new(); |
69 | 68 | // ok: panic-in-function-returning-result |
70 | 69 | f.read_to_string(&mut s)?; |
71 | 70 | println!("{}", s); |
72 | | - return Ok(()) |
| 71 | + return Ok(()); |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn test_get_file_content(path: &str) -> Result<String, std::io::Error> { |
| 79 | + // ok: panic-in-function-returning-result |
| 80 | + let mut f = File::open(path).unwrap(); |
| 81 | + let mut s = String::new(); |
| 82 | + // ok: panic-in-function-returning-result |
| 83 | + f.read_to_string(&mut s).expect("oh"); |
| 84 | + return Ok(s); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_get_file_content_2(path: &str) -> io::Result<()> { |
| 89 | + // ok: panic-in-function-returning-result |
| 90 | + let mut f = File::open(path).expect("uh"); |
| 91 | + let mut s = String::new(); |
| 92 | + // ok: panic-in-function-returning-result |
| 93 | + f.read_to_string(&mut s).unwrap(); |
| 94 | + println!("{}", s); |
| 95 | + return Ok(()); |
| 96 | + } |
| 97 | + |
| 98 | + type CustomResult = Result<f64, String>; |
| 99 | + |
| 100 | + thread_local!(static GLOB: [i32; 10] = { |
| 101 | + // ok: panic-in-function-returning-result |
| 102 | + (0..10).collect::<Vec<i32>>().try_into().unwrap() |
| 103 | + }); |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_get_file_content_3(path: &str) -> CustomResult { |
| 107 | + // ok: panic-in-function-returning-result |
| 108 | + let mut f = File::open(path).unwrap(); |
| 109 | + let mut s = String::new(); |
| 110 | + // ok: panic-in-function-returning-result |
| 111 | + f.read_to_string(&mut s).unwrap(); |
| 112 | + println!("{}", s); |
| 113 | + return Ok(1.); |
| 114 | + } |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_get_file_content_4(path: &str) -> io::Result<()> { |
| 118 | + // ok: panic-in-function-returning-result |
| 119 | + let mut f = File::open(path)?; |
| 120 | + let mut s = String::new(); |
| 121 | + // ok: panic-in-function-returning-result |
| 122 | + f.read_to_string(&mut s)?; |
| 123 | + println!("{}", s); |
| 124 | + return Ok(()); |
| 125 | + } |
73 | 126 | } |
0 commit comments