Skip to content

Commit 2d27976

Browse files
authored
Rollup merge of #87965 - m-ou-se:non-fmt-panic-external, r=estebank
Silence non_fmt_panic from external macros. This stops the non_fmt_panic lint from triggering if a macro from another crate is entirely responsible. In those cases there's nothing that the current crate can/should do. See also #87621 (comment)
2 parents 0c4e37a + a6da55c commit 2d27976

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

compiler/rustc_lint/src/non_fmt_panic.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{LateContext, LateLintPass, LintContext};
22
use rustc_ast as ast;
33
use rustc_errors::{pluralize, Applicability};
44
use rustc_hir as hir;
5+
use rustc_middle::lint::in_external_macro;
56
use rustc_middle::ty;
67
use rustc_parse_format::{ParseMode, Parser, Piece};
78
use rustc_session::lint::FutureIncompatibilityReason;
@@ -75,6 +76,11 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
7576

7677
let (span, panic, symbol_str) = panic_call(cx, f);
7778

79+
if in_external_macro(cx.sess(), span) {
80+
// Nothing that can be done about it in the current crate.
81+
return;
82+
}
83+
7884
// Find the span of the argument to `panic!()`, before expansion in the
7985
// case of `panic!(some_macro!())`.
8086
// We don't use source_callsite(), because this `panic!(..)` might itself
@@ -152,6 +158,13 @@ fn check_panic_str<'tcx>(
152158
return;
153159
}
154160

161+
let (span, _, _) = panic_call(cx, f);
162+
163+
if in_external_macro(cx.sess(), span) && in_external_macro(cx.sess(), arg.span) {
164+
// Nothing that can be done about it in the current crate.
165+
return;
166+
}
167+
155168
let fmt_span = arg.span.source_callsite();
156169

157170
let (snippet, style) = match cx.sess().parse_sess.source_map().span_to_snippet(fmt_span) {
@@ -167,8 +180,6 @@ fn check_panic_str<'tcx>(
167180
Parser::new(fmt.as_ref(), style, snippet.clone(), false, ParseMode::Format);
168181
let n_arguments = (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();
169182

170-
let (span, _, _) = panic_call(cx, f);
171-
172183
if n_arguments > 0 && fmt_parser.errors.is_empty() {
173184
let arg_spans: Vec<_> = match &fmt_parser.arg_places[..] {
174185
[] => vec![fmt_span],

src/test/ui/auxiliary/fancy-panic.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#[macro_export]
22
macro_rules! fancy_panic {
3+
() => {
4+
panic!("{}");
5+
};
36
($msg:expr) => {
47
panic!($msg)
58
};

src/test/ui/non-fmt-panic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ fn main() {
2626
fancy_panic::fancy_panic!("test {} 123");
2727
//~^ WARN panic message contains an unused formatting placeholder
2828

29-
fancy_panic::fancy_panic!(S);
30-
//~^ WARN panic message is not a string literal
29+
fancy_panic::fancy_panic!(); // OK
30+
fancy_panic::fancy_panic!(S); // OK
3131

3232
macro_rules! a {
3333
() => { 123 };

src/test/ui/non-fmt-panic.stderr

+1-10
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,6 @@ LL | fancy_panic::fancy_panic!("test {} 123");
180180
|
181181
= note: this message is not used as a format string when given without arguments, but will be in Rust 2021
182182

183-
warning: panic message is not a string literal
184-
--> $DIR/non-fmt-panic.rs:29:31
185-
|
186-
LL | fancy_panic::fancy_panic!(S);
187-
| ^
188-
|
189-
= note: this usage of panic!() is deprecated; it will be a hard error in Rust 2021
190-
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/panic-macro-consistency.html>
191-
192183
warning: panic message is not a string literal
193184
--> $DIR/non-fmt-panic.rs:36:12
194185
|
@@ -285,5 +276,5 @@ help: or use std::panic::panic_any instead
285276
LL | std::panic::panic_any(123);
286277
| ~~~~~~~~~~~~~~~~~~~~~~ ~
287278

288-
warning: 20 warnings emitted
279+
warning: 19 warnings emitted
289280

0 commit comments

Comments
 (0)