Skip to content

Fix missing block for unsafe code #10809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions clippy_utils/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use rustc_data_structures::sync::Lrc;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_hir::{BlockCheckMode, Expr, ExprKind, UnsafeSource};
use rustc_lint::{LateContext, LintContext};
use rustc_session::Session;
use rustc_span::source_map::{original_sp, SourceMap};
Expand Down Expand Up @@ -71,11 +71,17 @@ pub fn expr_block<T: LintContext>(
app: &mut Applicability,
) -> String {
let (code, from_macro) = snippet_block_with_context(cx, expr.span, outer, default, indent_relative_to, app);
if from_macro {
format!("{{ {code} }}")
} else if let ExprKind::Block(_, _) = expr.kind {
if !from_macro &&
let ExprKind::Block(block, _) = expr.kind &&
// TODO: Is this enough UnsafeSource::UserProvided, or should CompilerGenerated be also included?
block.rules != BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided)
{
format!("{code}")
} else {
// FIXME: add extra indent for the unsafe blocks:
// original code: unsafe { ... }
// result code: { unsafe { ... } }
// desired code: {\n unsafe { ... }\n}
format!("{{ {code} }}")
}
}
Expand Down
23 changes: 22 additions & 1 deletion tests/ui/single_match.rs
Original file line number Diff line number Diff line change
@@ -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);
}
},
_ => {},
}
}
45 changes: 44 additions & 1 deletion tests/ui/single_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,48 @@ LL | | (..) => {},
LL | | }
| |_____^ help: try this: `if let (.., Some(E::V), _) = (Some(42), Some(E::V), Some(42)) {}`

error: aborting due to 16 previous errors
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> $DIR/single_match.rs:249:5
|
LL | / match bar {
LL | | Some(v) => unsafe {
LL | | let r = &v as *const i32;
LL | | println!("{}", *r);
LL | | },
LL | | _ => {},
LL | | }
| |_____^
|
help: try this
|
LL ~ if let Some(v) = bar { unsafe {
LL + let r = &v as *const i32;
LL + println!("{}", *r);
LL + } }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> $DIR/single_match.rs:257:5
|
LL | / match bar {
LL | | Some(v) => {
LL | | // this comment prevents rustfmt from collapsing the block
LL | | unsafe {
... |
LL | | _ => {},
LL | | }
| |_____^
|
help: try this
|
LL ~ if let Some(v) = bar {
LL + // this comment prevents rustfmt from collapsing the block
LL + unsafe {
LL + let r = &v as *const i32;
LL + println!("{}", *r);
LL + }
LL + }
|

error: aborting due to 18 previous errors

86 changes: 85 additions & 1 deletion tests/ui/single_match_else.rs
Original file line number Diff line number Diff line change
@@ -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);
}
},
}
}
99 changes: 98 additions & 1 deletion tests/ui/single_match_else.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,102 @@ LL + return;
LL + }
|

error: aborting due to 5 previous errors
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> $DIR/single_match_else.rs:120:5
|
LL | / match bar {
LL | | Some(v) => unsafe {
LL | | let r = &v as *const i32;
LL | | println!("{}", *r);
... |
LL | | },
LL | | }
| |_____^
|
help: try this
|
LL ~ if let Some(v) = bar { unsafe {
LL + let r = &v as *const i32;
LL + println!("{}", *r);
LL + } } else {
LL + println!("None1");
LL + println!("None2");
LL + }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> $DIR/single_match_else.rs:131:5
|
LL | / match bar {
LL | | Some(v) => {
LL | | println!("Some");
LL | | println!("{v}");
... |
LL | | },
LL | | }
| |_____^
|
help: try this
|
LL ~ if let Some(v) = bar {
LL + println!("Some");
LL + println!("{v}");
LL + } else { unsafe {
LL + let v = 0;
LL + let r = &v as *const i32;
LL + println!("{}", *r);
LL + } }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> $DIR/single_match_else.rs:143:5
|
LL | / match bar {
LL | | Some(v) => unsafe {
LL | | let r = &v as *const i32;
LL | | println!("{}", *r);
... |
LL | | },
LL | | }
| |_____^
|
help: try this
|
LL ~ if let Some(v) = bar { unsafe {
LL + let r = &v as *const i32;
LL + println!("{}", *r);
LL + } } else { unsafe {
LL + let v = 0;
LL + let r = &v as *const i32;
LL + println!("{}", *r);
LL + } }
|

error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
--> $DIR/single_match_else.rs:155:5
|
LL | / match bar {
LL | | Some(v) => {
LL | | // this comment prevents rustfmt from collapsing the block
LL | | unsafe {
... |
LL | | },
LL | | }
| |_____^
|
help: try this
|
LL ~ if let Some(v) = bar {
LL + // this comment prevents rustfmt from collapsing the block
LL + unsafe {
LL + let r = &v as *const i32;
LL + println!("{}", *r);
LL + }
LL + } else {
LL + println!("None");
LL + println!("None");
LL + }
|

error: aborting due to 9 previous errors