Skip to content

Commit

Permalink
Auto merge of rust-lang#115670 - Zoxc:outline-panic-macro-1, r=Mark-S…
Browse files Browse the repository at this point in the history
…imulacrum

Partially outline code inside the panic! macro

This outlines code inside the panic! macro in some cases. This is split out from rust-lang#115562 to exclude changes to rustc.
  • Loading branch information
bors committed Oct 1, 2023
2 parents ad0b7ed + b5941a2 commit 3169423
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
23 changes: 19 additions & 4 deletions clippy_lints/src/mixed_read_write_in_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,30 @@ impl<'a, 'tcx> DivergenceVisitor<'a, 'tcx> {
}
}

fn stmt_might_diverge(stmt: &Stmt<'_>) -> bool {
match stmt.kind {
StmtKind::Item(..) => false,
_ => true,
}
}

impl<'a, 'tcx> Visitor<'tcx> for DivergenceVisitor<'a, 'tcx> {
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
match e.kind {
// fix #10776
ExprKind::Block(block, ..) => match (block.stmts, block.expr) {
([], Some(e)) => self.visit_expr(e),
([stmt], None) => match stmt.kind {
StmtKind::Expr(e) | StmtKind::Semi(e) => self.visit_expr(e),
_ => {},
(stmts, Some(e)) => {
if stmts.iter().all(|stmt| !stmt_might_diverge(stmt)) {
self.visit_expr(e)
}
},
([first @ .., stmt], None) => {
if first.iter().all(|stmt| !stmt_might_diverge(stmt)) {
match stmt.kind {
StmtKind::Expr(e) | StmtKind::Semi(e) => self.visit_expr(e),
_ => {},
}
}
},
_ => {},
},
Expand Down
16 changes: 13 additions & 3 deletions clippy_utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,26 @@ pub enum PanicExpn<'a> {

impl<'a> PanicExpn<'a> {
pub fn parse(expr: &'a Expr<'a>) -> Option<Self> {
let ExprKind::Call(callee, [arg, rest @ ..]) = &expr.kind else {
let ExprKind::Call(callee, args) = &expr.kind else {
return None;
};
let ExprKind::Path(QPath::Resolved(_, path)) = &callee.kind else {
return None;
};
let result = match path.segments.last().unwrap().ident.as_str() {
let name = path.segments.last().unwrap().ident.as_str();

// This has no argument
if name == "panic_cold_explicit" {
return Some(Self::Empty);
};

let [arg, rest @ ..] = args else {
return None;
};
let result = match name {
"panic" if arg.span.ctxt() == expr.span.ctxt() => Self::Empty,
"panic" | "panic_str" => Self::Str(arg),
"panic_display" => {
"panic_display" | "panic_cold_display" => {
let ExprKind::AddrOf(_, _, e) = &arg.kind else {
return None;
};
Expand Down
20 changes: 12 additions & 8 deletions tests/ui/match_same_arms_non_exhaustive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
//@no-rustfix
use std::sync::atomic::Ordering; // #[non_exhaustive] enum

fn repeat() -> ! {
panic!()
}

pub fn f(x: Ordering) {
match x {
Ordering::Relaxed => println!("relaxed"),
Ordering::Release => println!("release"),
Ordering::Acquire => println!("acquire"),
Ordering::AcqRel | Ordering::SeqCst => panic!(),
Ordering::AcqRel | Ordering::SeqCst => repeat(),
#[deny(non_exhaustive_omitted_patterns)]
_ => panic!(),
_ => repeat(),
}
}

Expand All @@ -25,8 +29,8 @@ mod f {
Ordering::Relaxed => println!("relaxed"),
Ordering::Release => println!("release"),
Ordering::Acquire => println!("acquire"),
Ordering::AcqRel | Ordering::SeqCst => panic!(),
_ => panic!(),
Ordering::AcqRel | Ordering::SeqCst => repeat(),
_ => repeat(),
}
}
}
Expand All @@ -38,9 +42,9 @@ pub fn g(x: Ordering) {
Ordering::Relaxed => println!("relaxed"),
Ordering::Release => println!("release"),
Ordering::Acquire => println!("acquire"),
Ordering::AcqRel | Ordering::SeqCst => panic!(),
Ordering::AcqRel | Ordering::SeqCst => repeat(),
//~^ ERROR: this match arm has an identical body to the `_` wildcard arm
_ => panic!(),
_ => repeat(),
}
}

Expand All @@ -52,9 +56,9 @@ mod g {
Ordering::Relaxed => println!("relaxed"),
Ordering::Release => println!("release"),
Ordering::Acquire => println!("acquire"),
Ordering::AcqRel | Ordering::SeqCst => panic!(),
Ordering::AcqRel | Ordering::SeqCst => repeat(),
//~^ ERROR: this match arm has an identical body to the `_` wildcard arm
_ => panic!(),
_ => repeat(),
}
}
}
16 changes: 8 additions & 8 deletions tests/ui/match_same_arms_non_exhaustive.stderr
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
error: this match arm has an identical body to the `_` wildcard arm
--> $DIR/match_same_arms_non_exhaustive.rs:41:9
--> $DIR/match_same_arms_non_exhaustive.rs:45:9
|
LL | Ordering::AcqRel | Ordering::SeqCst => panic!(),
LL | Ordering::AcqRel | Ordering::SeqCst => repeat(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the arm
|
= help: or try changing either arm body
note: `_` wildcard arm here
--> $DIR/match_same_arms_non_exhaustive.rs:43:9
--> $DIR/match_same_arms_non_exhaustive.rs:47:9
|
LL | _ => panic!(),
LL | _ => repeat(),
| ^^^^^^^^^^^^^
= note: `-D clippy::match-same-arms` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::match_same_arms)]`

error: this match arm has an identical body to the `_` wildcard arm
--> $DIR/match_same_arms_non_exhaustive.rs:55:13
--> $DIR/match_same_arms_non_exhaustive.rs:59:13
|
LL | Ordering::AcqRel | Ordering::SeqCst => panic!(),
LL | Ordering::AcqRel | Ordering::SeqCst => repeat(),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the arm
|
= help: or try changing either arm body
note: `_` wildcard arm here
--> $DIR/match_same_arms_non_exhaustive.rs:57:13
--> $DIR/match_same_arms_non_exhaustive.rs:61:13
|
LL | _ => panic!(),
LL | _ => repeat(),
| ^^^^^^^^^^^^^

error: aborting due to 2 previous errors
Expand Down

0 comments on commit 3169423

Please sign in to comment.