Skip to content

Commit

Permalink
Unit tests highlighting unsafe match issue
Browse files Browse the repository at this point in the history
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 rust-lang#10808
  • Loading branch information
nyurik committed May 22, 2023
1 parent 435a8ad commit 1c277d1
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
23 changes: 22 additions & 1 deletion tests/ui/single_match.rs
@@ -1,5 +1,5 @@
#![warn(clippy::single_match)]
#![allow(clippy::uninlined_format_args)]
#![allow(unused, clippy::uninlined_format_args)]

fn dummy() {}

Expand Down Expand Up @@ -244,3 +244,24 @@ fn main() {
_ => 0,
};
}

fn issue_10808(bar: Option<i32>) {
match bar {
Some(v) => unsafe {
let r = &v as *const i32;
println!("{}", *r);
},
_ => {},
}

match bar {
Some(v) => {
// this comment prevents rustfmt from collapsing the block
unsafe {
let r = &v as *const i32;
println!("{}", *r);
}
},
_ => {},
}
}
86 changes: 85 additions & 1 deletion tests/ui/single_match_else.rs
@@ -1,6 +1,6 @@
//@aux-build: proc_macros.rs
#![warn(clippy::single_match_else)]
#![allow(clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]
#![allow(unused, clippy::needless_return, clippy::no_effect, clippy::uninlined_format_args)]

extern crate proc_macros;
use proc_macros::with_span;
Expand Down Expand Up @@ -115,3 +115,87 @@ fn main() {
}
}
}

fn issue_10808(bar: Option<i32>) {
match bar {
Some(v) => unsafe {
let r = &v as *const i32;
println!("{}", *r);
},
None => {
println!("None1");
println!("None2");
},
}

match bar {
Some(v) => {
println!("Some");
println!("{v}");
},
None => unsafe {
let v = 0;
let r = &v as *const i32;
println!("{}", *r);
},
}

match bar {
Some(v) => unsafe {
let r = &v as *const i32;
println!("{}", *r);
},
None => unsafe {
let v = 0;
let r = &v as *const i32;
println!("{}", *r);
},
}

match bar {
Some(v) => {
// this comment prevents rustfmt from collapsing the block
unsafe {
let r = &v as *const i32;
println!("{}", *r);
}
},
None => {
println!("None");
println!("None");
},
}

match bar {
Some(v) => {
println!("Some");
println!("{v}");
},
None => {
// this comment prevents rustfmt from collapsing the block
unsafe {
let v = 0;
let r = &v as *const i32;
println!("{}", *r);
}
},
}

match bar {
Some(v) => {
// this comment prevents rustfmt from collapsing the block
unsafe {
let r = &v as *const i32;
println!("{}", *r);
}
},
None => {
// this comment prevents rustfmt from collapsing the block
unsafe {
let v = 0;
let r = &v as *const i32;
println!("{}", *r);
}
},
}
}

0 comments on commit 1c277d1

Please sign in to comment.