From 1c277d1be896ad5499a464d92a3d427488df470e Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sun, 21 May 2023 23:33:42 -0400 Subject: [PATCH] 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 --- tests/ui/single_match.rs | 23 +++++++++- tests/ui/single_match_else.rs | 86 ++++++++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/tests/ui/single_match.rs b/tests/ui/single_match.rs index d0c9b7b56..d474088aa 100644 --- a/tests/ui/single_match.rs +++ b/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() {} @@ -244,3 +244,24 @@ fn main() { _ => 0, }; } + +fn issue_10808(bar: Option) { + 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); + } + }, + _ => {}, + } +} diff --git a/tests/ui/single_match_else.rs b/tests/ui/single_match_else.rs index c8ac768b6..768316eda 100644 --- a/tests/ui/single_match_else.rs +++ b/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; @@ -115,3 +115,87 @@ fn main() { } } } + +fn issue_10808(bar: Option) { + 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); + } + }, + } +}