Skip to content

Commit 266f452

Browse files
committed
Auto merge of rust-lang#85150 - Dylan-DPC:rollup-q26gbx3, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - rust-lang#85050 (Fix suggestions for missing return type lifetime specifiers) - rust-lang#85075 (Improve "panic message is not a string literal" warning) - rust-lang#85096 (Make unchecked_{add,sub,mul} inherent methods unstably const) - rust-lang#85112 (ensure failing promoteds in const/static bodies are handled correctly) - rust-lang#85146 (Provide io::Seek::rewind) - rust-lang#85147 (:arrow_up: rust-analyzer) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 544d124 + e763401 commit 266f452

26 files changed

+707
-193
lines changed

compiler/rustc_errors/src/diagnostic.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,22 @@ impl Diagnostic {
282282
msg: &str,
283283
suggestion: Vec<(Span, String)>,
284284
applicability: Applicability,
285+
) -> &mut Self {
286+
self.multipart_suggestion_with_style(
287+
msg,
288+
suggestion,
289+
applicability,
290+
SuggestionStyle::ShowCode,
291+
)
292+
}
293+
294+
/// [`Diagnostic::multipart_suggestion()`] but you can set the [`SuggestionStyle`].
295+
pub fn multipart_suggestion_with_style(
296+
&mut self,
297+
msg: &str,
298+
suggestion: Vec<(Span, String)>,
299+
applicability: Applicability,
300+
style: SuggestionStyle,
285301
) -> &mut Self {
286302
assert!(!suggestion.is_empty());
287303
self.suggestions.push(CodeSuggestion {
@@ -292,7 +308,7 @@ impl Diagnostic {
292308
.collect(),
293309
}],
294310
msg: msg.to_owned(),
295-
style: SuggestionStyle::ShowCode,
311+
style,
296312
applicability,
297313
tool_metadata: Default::default(),
298314
});

compiler/rustc_lint/src/non_fmt_panic.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::{pluralize, Applicability};
44
use rustc_hir as hir;
55
use rustc_middle::ty;
66
use rustc_parse_format::{ParseMode, Parser, Piece};
7-
use rustc_span::{sym, symbol::kw, InnerSpan, Span, Symbol};
7+
use rustc_span::{hygiene, sym, symbol::kw, symbol::SymbolStr, InnerSpan, Span, Symbol};
88

99
declare_lint! {
1010
/// The `non_fmt_panic` lint detects `panic!(..)` invocations where the first
@@ -67,7 +67,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
6767

6868
// The argument is *not* a string literal.
6969

70-
let (span, panic) = panic_call(cx, f);
70+
let (span, panic, symbol_str) = panic_call(cx, f);
7171

7272
// Find the span of the argument to `panic!()`, before expansion in the
7373
// case of `panic!(some_macro!())`.
@@ -95,7 +95,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
9595
}
9696
if arg_macro.map_or(false, |id| cx.tcx.is_diagnostic_item(sym::format_macro, id)) {
9797
// A case of `panic!(format!(..))`.
98-
l.note("the panic!() macro supports formatting, so there's no need for the format!() macro here");
98+
l.note(format!("the {}!() macro supports formatting, so there's no need for the format!() macro here", symbol_str).as_str());
9999
if let Some((open, close, _)) = find_delimiters(cx, arg_span) {
100100
l.multipart_suggestion(
101101
"remove the `format!(..)` macro call",
@@ -160,7 +160,7 @@ fn check_panic_str<'tcx>(
160160
Parser::new(fmt.as_ref(), style, snippet.clone(), false, ParseMode::Format);
161161
let n_arguments = (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();
162162

163-
let (span, _) = panic_call(cx, f);
163+
let (span, _, _) = panic_call(cx, f);
164164

165165
if n_arguments > 0 && fmt_parser.errors.is_empty() {
166166
let arg_spans: Vec<_> = match &fmt_parser.arg_places[..] {
@@ -230,7 +230,7 @@ fn find_delimiters<'tcx>(cx: &LateContext<'tcx>, span: Span) -> Option<(Span, Sp
230230
))
231231
}
232232

233-
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol) {
233+
fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, Symbol, SymbolStr) {
234234
let mut expn = f.span.ctxt().outer_expn_data();
235235

236236
let mut panic_macro = kw::Empty;
@@ -248,5 +248,10 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
248248
}
249249
}
250250

251-
(expn.call_site, panic_macro)
251+
let macro_symbol = if let hygiene::ExpnKind::Macro(_, symbol) = expn.kind {
252+
symbol
253+
} else {
254+
Symbol::intern("panic")
255+
};
256+
(expn.call_site, panic_macro, macro_symbol.as_str())
252257
}

0 commit comments

Comments
 (0)