From 0f193d1a62c128ae94e1f21d7c1212d7c9e95b7d Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Mon, 19 Oct 2020 21:14:05 +0200 Subject: [PATCH] Small cleanups in assert!() and panic_fmt lint. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (From the PR feedback.) Co-authored-by: Esteban Küber --- compiler/rustc_builtin_macros/src/assert.rs | 60 ++++++++++----------- compiler/rustc_lint/src/panic_fmt.rs | 8 +-- src/test/ui/panic-brace.rs | 10 ++-- src/test/ui/panic-brace.stderr | 20 +++---- 4 files changed, 48 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index bc3276538da..bb6d3f6a007 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -27,37 +27,35 @@ pub fn expand_assert<'cx>( // context to pick up whichever is currently in scope. let sp = cx.with_call_site_ctxt(sp); - let panic_call = { - if let Some(tokens) = custom_message { - // Pass the custom message to panic!(). - cx.expr( - sp, - ExprKind::MacCall(MacCall { - path: Path::from_ident(Ident::new(sym::panic, sp)), - args: P(MacArgs::Delimited( - DelimSpan::from_single(sp), - MacDelimiter::Parenthesis, - tokens, - )), - prior_type_ascription: None, - }), - ) - } else { - // Pass our own message directly to $crate::panicking::panic(), - // because it might contain `{` and `}` that should always be - // passed literally. - cx.expr_call_global( - sp, - cx.std_path(&[sym::panicking, sym::panic]), - vec![cx.expr_str( - DUMMY_SP, - Symbol::intern(&format!( - "assertion failed: {}", - pprust::expr_to_string(&cond_expr).escape_debug() - )), - )], - ) - } + let panic_call = if let Some(tokens) = custom_message { + // Pass the custom message to panic!(). + cx.expr( + sp, + ExprKind::MacCall(MacCall { + path: Path::from_ident(Ident::new(sym::panic, sp)), + args: P(MacArgs::Delimited( + DelimSpan::from_single(sp), + MacDelimiter::Parenthesis, + tokens, + )), + prior_type_ascription: None, + }), + ) + } else { + // Pass our own message directly to $crate::panicking::panic(), + // because it might contain `{` and `}` that should always be + // passed literally. + cx.expr_call_global( + sp, + cx.std_path(&[sym::panicking, sym::panic]), + vec![cx.expr_str( + DUMMY_SP, + Symbol::intern(&format!( + "assertion failed: {}", + pprust::expr_to_string(&cond_expr).escape_debug() + )), + )], + ) }; let if_expr = cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None); diff --git a/compiler/rustc_lint/src/panic_fmt.rs b/compiler/rustc_lint/src/panic_fmt.rs index 05bc272d0e0..288e1d61bbf 100644 --- a/compiler/rustc_lint/src/panic_fmt.rs +++ b/compiler/rustc_lint/src/panic_fmt.rs @@ -72,8 +72,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc } if looks_like_placeholder { cx.struct_span_lint(PANIC_FMT, arg.span.source_callsite(), |lint| { - let mut l = lint.build("Panic message contains an unused formatting placeholder"); - l.note("This message is not used as a format string when given without arguments, but will be in a future Rust version"); + let mut l = lint.build("panic message contains an unused formatting placeholder"); + l.note("this message is not used as a format string when given without arguments, but will be in a future Rust version"); if expn.call_site.contains(arg.span) { l.span_suggestion( arg.span.shrink_to_hi(), @@ -92,8 +92,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc }); } else { cx.struct_span_lint(PANIC_FMT, expn.call_site, |lint| { - let mut l = lint.build("Panic message contains a brace"); - l.note("This message is not used as a format string, but will be in a future Rust version"); + let mut l = lint.build("panic message contains a brace"); + l.note("this message is not used as a format string, but will be in a future Rust version"); if expn.call_site.contains(arg.span) { l.span_suggestion( arg.span.shrink_to_lo(), diff --git a/src/test/ui/panic-brace.rs b/src/test/ui/panic-brace.rs index d6bb3222ac7..6ab5fafee88 100644 --- a/src/test/ui/panic-brace.rs +++ b/src/test/ui/panic-brace.rs @@ -2,9 +2,9 @@ #[allow(unreachable_code)] fn main() { - panic!("here's a brace: {"); //~ WARN Panic message contains a brace - std::panic!("another one: }"); //~ WARN Panic message contains a brace - core::panic!("Hello {}"); //~ WARN Panic message contains an unused formatting placeholder - assert!(false, "{:03x} bla"); //~ WARN Panic message contains an unused formatting placeholder - debug_assert!(false, "{{}} bla"); //~ WARN Panic message contains a brace + panic!("here's a brace: {"); //~ WARN panic message contains a brace + std::panic!("another one: }"); //~ WARN panic message contains a brace + core::panic!("Hello {}"); //~ WARN panic message contains an unused formatting placeholder + assert!(false, "{:03x} bla"); //~ WARN panic message contains an unused formatting placeholder + debug_assert!(false, "{{}} bla"); //~ WARN panic message contains a brace } diff --git a/src/test/ui/panic-brace.stderr b/src/test/ui/panic-brace.stderr index 578731896ba..00b005a59d8 100644 --- a/src/test/ui/panic-brace.stderr +++ b/src/test/ui/panic-brace.stderr @@ -1,35 +1,35 @@ -warning: Panic message contains a brace +warning: panic message contains a brace --> $DIR/panic-brace.rs:5:5 | LL | panic!("here's a brace: {"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(panic_fmt)]` on by default - = note: This message is not used as a format string, but will be in a future Rust version + = note: this message is not used as a format string, but will be in a future Rust version help: add a "{}" format string to use the message literally | LL | panic!("{}", "here's a brace: {"); | ^^^^^ -warning: Panic message contains a brace +warning: panic message contains a brace --> $DIR/panic-brace.rs:6:5 | LL | std::panic!("another one: }"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: This message is not used as a format string, but will be in a future Rust version + = note: this message is not used as a format string, but will be in a future Rust version help: add a "{}" format string to use the message literally | LL | std::panic!("{}", "another one: }"); | ^^^^^ -warning: Panic message contains an unused formatting placeholder +warning: panic message contains an unused formatting placeholder --> $DIR/panic-brace.rs:7:18 | LL | core::panic!("Hello {}"); | ^^^^^^^^^^ | - = note: This message is not used as a format string when given without arguments, but will be in a future Rust version + = note: this message is not used as a format string when given without arguments, but will be in a future Rust version help: add the missing argument(s) | LL | core::panic!("Hello {}", argument); @@ -39,13 +39,13 @@ help: or add a "{}" format string to use the message literally LL | core::panic!("{}", "Hello {}"); | ^^^^^ -warning: Panic message contains an unused formatting placeholder +warning: panic message contains an unused formatting placeholder --> $DIR/panic-brace.rs:8:20 | LL | assert!(false, "{:03x} bla"); | ^^^^^^^^^^^^ | - = note: This message is not used as a format string when given without arguments, but will be in a future Rust version + = note: this message is not used as a format string when given without arguments, but will be in a future Rust version help: add the missing argument(s) | LL | assert!(false, "{:03x} bla", argument); @@ -55,13 +55,13 @@ help: or add a "{}" format string to use the message literally LL | assert!(false, "{}", "{:03x} bla"); | ^^^^^ -warning: Panic message contains a brace +warning: panic message contains a brace --> $DIR/panic-brace.rs:9:5 | LL | debug_assert!(false, "{{}} bla"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: This message is not used as a format string, but will be in a future Rust version + = note: this message is not used as a format string, but will be in a future Rust version help: add a "{}" format string to use the message literally | LL | debug_assert!(false, "{}", "{{}} bla");