Skip to content

Commit 85f535b

Browse files
committed
add test cases for u32, f32, bool, String
1 parent 8fad54e commit 85f535b

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

tests/ui/read_line_without_trim.fixed

+17
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,21 @@ fn main() {
1616
let mut input = String::new();
1717
std::io::stdin().read_line(&mut input).unwrap();
1818
let _x = input.trim_end().parse::<i32>().unwrap();
19+
20+
let mut input = String::new();
21+
std::io::stdin().read_line(&mut input).unwrap();
22+
let _x = input.trim_end().parse::<u32>().unwrap();
23+
24+
let mut input = String::new();
25+
std::io::stdin().read_line(&mut input).unwrap();
26+
let _x = input.trim_end().parse::<f32>().unwrap();
27+
28+
let mut input = String::new();
29+
std::io::stdin().read_line(&mut input).unwrap();
30+
let _x = input.trim_end().parse::<bool>().unwrap();
31+
32+
let mut input = String::new();
33+
std::io::stdin().read_line(&mut input).unwrap();
34+
// this is actually ok, so don't lint here
35+
let _x = input.parse::<String>().unwrap();
1936
}

tests/ui/read_line_without_trim.rs

+17
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,21 @@ fn main() {
1616
let mut input = String::new();
1717
std::io::stdin().read_line(&mut input).unwrap();
1818
let _x = input.parse::<i32>().unwrap();
19+
20+
let mut input = String::new();
21+
std::io::stdin().read_line(&mut input).unwrap();
22+
let _x = input.parse::<u32>().unwrap();
23+
24+
let mut input = String::new();
25+
std::io::stdin().read_line(&mut input).unwrap();
26+
let _x = input.parse::<f32>().unwrap();
27+
28+
let mut input = String::new();
29+
std::io::stdin().read_line(&mut input).unwrap();
30+
let _x = input.parse::<bool>().unwrap();
31+
32+
let mut input = String::new();
33+
std::io::stdin().read_line(&mut input).unwrap();
34+
// this is actually ok, so don't lint here
35+
let _x = input.parse::<String>().unwrap();
1936
}

tests/ui/read_line_without_trim.stderr

+43-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,47 @@ note: call to `.read_line()` here, which leaves a trailing newline character in
2727
LL | std::io::stdin().read_line(&mut input).unwrap();
2828
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2929

30-
error: aborting due to 2 previous errors
30+
error: calling `.parse()` without trimming the trailing newline character
31+
--> $DIR/read_line_without_trim.rs:22:20
32+
|
33+
LL | let _x = input.parse::<u32>().unwrap();
34+
| ----- ^^^^^^^^^^^^^^
35+
| |
36+
| help: try: `input.trim_end()`
37+
|
38+
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail
39+
--> $DIR/read_line_without_trim.rs:21:5
40+
|
41+
LL | std::io::stdin().read_line(&mut input).unwrap();
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
44+
error: calling `.parse()` without trimming the trailing newline character
45+
--> $DIR/read_line_without_trim.rs:26:20
46+
|
47+
LL | let _x = input.parse::<f32>().unwrap();
48+
| ----- ^^^^^^^^^^^^^^
49+
| |
50+
| help: try: `input.trim_end()`
51+
|
52+
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail
53+
--> $DIR/read_line_without_trim.rs:25:5
54+
|
55+
LL | std::io::stdin().read_line(&mut input).unwrap();
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57+
58+
error: calling `.parse()` without trimming the trailing newline character
59+
--> $DIR/read_line_without_trim.rs:30:20
60+
|
61+
LL | let _x = input.parse::<bool>().unwrap();
62+
| ----- ^^^^^^^^^^^^^^^
63+
| |
64+
| help: try: `input.trim_end()`
65+
|
66+
note: call to `.read_line()` here, which leaves a trailing newline character in the buffer, which in turn will cause `.parse()` to fail
67+
--> $DIR/read_line_without_trim.rs:29:5
68+
|
69+
LL | std::io::stdin().read_line(&mut input).unwrap();
70+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
72+
error: aborting due to 5 previous errors
3173

0 commit comments

Comments
 (0)