Skip to content

Commit b59db9b

Browse files
authored
rust: exclude tests from panic matches (#74)
* rust: exclude tests from panic matches * add #[test] annotation on dummy test functions * add patterns for different return type defs * run prettier
1 parent 796f564 commit b59db9b

File tree

2 files changed

+101
-16
lines changed

2 files changed

+101
-16
lines changed
Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,126 @@
1-
use std::io;
1+
use std::convert::TryInto;
22
use std::fs::File;
3+
use std::io;
34
use std::io::Read;
4-
use std::convert::TryInto;
55

66
fn main() {
77
println!("Enter a file name: ");
88
let mut input = String::new();
99

10-
io::stdin().read_line(&mut input)
10+
io::stdin()
11+
.read_line(&mut input)
1112
.expect("error: unable to read user input");
1213

1314
let _ = match get_file_content(&input.trim()) {
14-
Ok(c) => println!("{}", c),
15+
Ok(c) => println!("{}", c),
1516
Err(e) => panic!("Unable to parse file. {}", e),
1617
};
1718

18-
match get_file_content_2(&input.trim()){
19+
match get_file_content_2(&input.trim()) {
1920
Ok(_) => println!("Success"),
20-
Err(e) => panic!("{}", e)
21+
Err(e) => panic!("{}", e),
2122
}
2223

2324
get_file_content_3(&input).unwrap();
2425
get_file_content_4(&input).unwrap();
2526
}
2627

27-
fn get_file_content(path:&str) -> Result<String, std::io::Error> {
28+
fn get_file_content(path: &str) -> Result<String, std::io::Error> {
2829
// ruleid: panic-in-function-returning-result
2930
let mut f = File::open(path).unwrap();
3031
let mut s = String::new();
3132
// ruleid: panic-in-function-returning-result
3233
f.read_to_string(&mut s).expect("oh");
33-
return Ok(s)
34+
return Ok(s);
3435
}
3536

36-
fn get_file_content_2(path:&str) -> io::Result<()> {
37+
fn get_file_content_2(path: &str) -> io::Result<()> {
3738
// ruleid: panic-in-function-returning-result
3839
let mut f = File::open(path).expect("uh");
3940
let mut s = String::new();
4041
// ruleid: panic-in-function-returning-result
4142
f.read_to_string(&mut s).unwrap();
4243
println!("{}", s);
43-
return Ok(())
44+
return Ok(());
4445
}
4546

46-
4747
type CustomResult = Result<f64, String>;
4848

4949
thread_local!(static GLOB: [i32; 10] = {
5050
// ok: panic-in-function-returning-result
5151
(0..10).collect::<Vec<i32>>().try_into().unwrap()
5252
});
5353

54-
fn get_file_content_3(path:&str) -> CustomResult {
54+
fn get_file_content_3(path: &str) -> CustomResult {
5555
// ruleid: panic-in-function-returning-result
5656
let mut f = File::open(path).unwrap();
5757
let mut s = String::new();
5858
// ruleid: panic-in-function-returning-result
5959
f.read_to_string(&mut s).unwrap();
6060
println!("{}", s);
61-
return Ok(1.)
61+
return Ok(1.);
6262
}
6363

64-
65-
fn get_file_content_4(path:&str) -> io::Result<()> {
64+
fn get_file_content_4(path: &str) -> io::Result<()> {
6665
// ok: panic-in-function-returning-result
6766
let mut f = File::open(path)?;
6867
let mut s = String::new();
6968
// ok: panic-in-function-returning-result
7069
f.read_to_string(&mut s)?;
7170
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+
}
73126
}

rs/panic-in-function-returning-result.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,35 @@ rules:
5151
fn $FUNC(...) -> $RETTYPE {
5252
...
5353
}
54+
- pattern-not-inside: |
55+
#[cfg(test)]
56+
mod tests {
57+
...
58+
#[test]
59+
fn $TESTFUNC(...) -> Result<$T1, $T2> {
60+
...
61+
}
62+
...
63+
}
64+
- pattern-not-inside: |
65+
#[cfg(test)]
66+
mod tests {
67+
...
68+
#[test]
69+
fn $TESTFUNC(...) -> Result<$T> {
70+
...
71+
}
72+
...
73+
}
74+
- pattern-not-inside: |
75+
#[cfg(test)]
76+
mod tests {
77+
...
78+
type $CUSTOMRESULT = Result<$T1, $T2>;
79+
...
80+
#[test]
81+
fn $TESTFUNC(...) -> $CUSTOMRESULT {
82+
...
83+
}
84+
...
85+
}

0 commit comments

Comments
 (0)