Skip to content

Commit 1c277d1

Browse files
committed
Unit tests highlighting unsafe match issue
These unit tests generate non-compilable code. I did NOT `bless` them on purpose because the stderr output is not good. I'm surprised we don't auto-compile the suggestions here - is this something that can be easily enabled? See #10808
1 parent 435a8ad commit 1c277d1

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

tests/ui/single_match.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![warn(clippy::single_match)]
2-
#![allow(clippy::uninlined_format_args)]
2+
#![allow(unused, clippy::uninlined_format_args)]
33

44
fn dummy() {}
55

@@ -244,3 +244,24 @@ fn main() {
244244
_ => 0,
245245
};
246246
}
247+
248+
fn issue_10808(bar: Option<i32>) {
249+
match bar {
250+
Some(v) => unsafe {
251+
let r = &v as *const i32;
252+
println!("{}", *r);
253+
},
254+
_ => {},
255+
}
256+
257+
match bar {
258+
Some(v) => {
259+
// this comment prevents rustfmt from collapsing the block
260+
unsafe {
261+
let r = &v as *const i32;
262+
println!("{}", *r);
263+
}
264+
},
265+
_ => {},
266+
}
267+
}

tests/ui/single_match_else.rs

+85-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@aux-build: proc_macros.rs
22
#![warn(clippy::single_match_else)]
3-
#![allow(clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
3+
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
44

55
extern crate proc_macros;
66
use proc_macros::with_span;
@@ -115,3 +115,87 @@ fn main() {
115115
}
116116
}
117117
}
118+
119+
fn issue_10808(bar: Option<i32>) {
120+
match bar {
121+
Some(v) => unsafe {
122+
let r = &v as *const i32;
123+
println!("{}", *r);
124+
},
125+
None => {
126+
println!("None1");
127+
println!("None2");
128+
},
129+
}
130+
131+
match bar {
132+
Some(v) => {
133+
println!("Some");
134+
println!("{v}");
135+
},
136+
None => unsafe {
137+
let v = 0;
138+
let r = &v as *const i32;
139+
println!("{}", *r);
140+
},
141+
}
142+
143+
match bar {
144+
Some(v) => unsafe {
145+
let r = &v as *const i32;
146+
println!("{}", *r);
147+
},
148+
None => unsafe {
149+
let v = 0;
150+
let r = &v as *const i32;
151+
println!("{}", *r);
152+
},
153+
}
154+
155+
match bar {
156+
Some(v) => {
157+
// this comment prevents rustfmt from collapsing the block
158+
unsafe {
159+
let r = &v as *const i32;
160+
println!("{}", *r);
161+
}
162+
},
163+
None => {
164+
println!("None");
165+
println!("None");
166+
},
167+
}
168+
169+
match bar {
170+
Some(v) => {
171+
println!("Some");
172+
println!("{v}");
173+
},
174+
None => {
175+
// this comment prevents rustfmt from collapsing the block
176+
unsafe {
177+
let v = 0;
178+
let r = &v as *const i32;
179+
println!("{}", *r);
180+
}
181+
},
182+
}
183+
184+
match bar {
185+
Some(v) => {
186+
// this comment prevents rustfmt from collapsing the block
187+
unsafe {
188+
let r = &v as *const i32;
189+
println!("{}", *r);
190+
}
191+
},
192+
None => {
193+
// this comment prevents rustfmt from collapsing the block
194+
unsafe {
195+
let v = 0;
196+
let r = &v as *const i32;
197+
println!("{}", *r);
198+
}
199+
},
200+
}
201+
}

0 commit comments

Comments
 (0)