Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 384010b

Browse files
committed
migrate: non_fmt_panic.rs
1 parent a9bbe31 commit 384010b

File tree

2 files changed

+53
-33
lines changed

2 files changed

+53
-33
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,43 @@ use rustc_span::{symbol::Ident, Span, Symbol};
66

77
use crate::LateContext;
88

9+
pub struct NonFmtPanicUnused {
10+
pub count: usize,
11+
pub suggestion: Option<Span>,
12+
}
13+
14+
impl<G: EmissionGuarantee> DecorateLint<'_, G> for NonFmtPanicUnused {
15+
fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
16+
let mut diag = diag.build(fluent::lint_non_fmt_panic_unused);
17+
diag.set_arg("count", self.count);
18+
diag.note(fluent::note);
19+
if let Some(span) = self.suggestion {
20+
diag.span_suggestion(
21+
span.shrink_to_hi(),
22+
fluent::add_args_suggestion,
23+
", ...",
24+
Applicability::HasPlaceholders,
25+
);
26+
diag.span_suggestion(
27+
span.shrink_to_lo(),
28+
fluent::add_fmt_suggestion,
29+
"\"{}\", ",
30+
Applicability::MachineApplicable,
31+
);
32+
}
33+
diag.emit();
34+
}
35+
}
36+
37+
#[derive(LintDiagnostic)]
38+
#[diag(lint_non_fmt_panic_braces)]
39+
#[note]
40+
pub struct NonFmtPanicBraces {
41+
pub count: usize,
42+
#[suggestion(code = "\"{{}}\", ", applicability = "machine-applicable")]
43+
pub suggestion: Option<Span>,
44+
}
45+
946
#[derive(LintDiagnostic)]
1047
#[diag(lint_non_camel_case_type)]
1148
pub struct NonCamelCaseType<'a> {

compiler/rustc_lint/src/non_fmt_panic.rs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![deny(rustc::untranslatable_diagnostic)]
2+
#![deny(rustc::diagnostic_outside_of_impl)]
3+
use crate::lints::{NonFmtPanicBraces, NonFmtPanicUnused};
14
use crate::{LateContext, LateLintPass, LintContext};
25
use rustc_ast as ast;
36
use rustc_errors::{fluent, Applicability};
@@ -118,6 +121,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
118121
arg_span = expn.call_site;
119122
}
120123

124+
#[allow(rustc::diagnostic_outside_of_impl)]
121125
cx.struct_span_lint(NON_FMT_PANICS, arg_span, fluent::lint_non_fmt_panic, |lint| {
122126
lint.set_arg("name", symbol);
123127
lint.note(fluent::note);
@@ -253,25 +257,14 @@ fn check_panic_str<'tcx>(
253257
.map(|span| fmt_span.from_inner(InnerSpan::new(span.start, span.end)))
254258
.collect(),
255259
};
256-
cx.struct_span_lint(NON_FMT_PANICS, arg_spans, fluent::lint_non_fmt_panic_unused, |lint| {
257-
lint.set_arg("count", n_arguments);
258-
lint.note(fluent::note);
259-
if is_arg_inside_call(arg.span, span) {
260-
lint.span_suggestion(
261-
arg.span.shrink_to_hi(),
262-
fluent::add_args_suggestion,
263-
", ...",
264-
Applicability::HasPlaceholders,
265-
);
266-
lint.span_suggestion(
267-
arg.span.shrink_to_lo(),
268-
fluent::add_fmt_suggestion,
269-
"\"{}\", ",
270-
Applicability::MachineApplicable,
271-
);
272-
}
273-
lint
274-
});
260+
cx.emit_spanned_lint(
261+
NON_FMT_PANICS,
262+
arg_spans,
263+
NonFmtPanicUnused {
264+
count: n_arguments,
265+
suggestion: is_arg_inside_call(arg.span, span).then_some(arg.span),
266+
},
267+
);
275268
} else {
276269
let brace_spans: Option<Vec<_>> =
277270
snippet.filter(|s| s.starts_with('"') || s.starts_with("r#")).map(|s| {
@@ -281,22 +274,12 @@ fn check_panic_str<'tcx>(
281274
.collect()
282275
});
283276
let count = brace_spans.as_ref().map(|v| v.len()).unwrap_or(/* any number >1 */ 2);
284-
cx.struct_span_lint(
277+
cx.emit_spanned_lint(
285278
NON_FMT_PANICS,
286279
brace_spans.unwrap_or_else(|| vec![span]),
287-
fluent::lint_non_fmt_panic_braces,
288-
|lint| {
289-
lint.set_arg("count", count);
290-
lint.note(fluent::note);
291-
if is_arg_inside_call(arg.span, span) {
292-
lint.span_suggestion(
293-
arg.span.shrink_to_lo(),
294-
fluent::suggestion,
295-
"\"{}\", ",
296-
Applicability::MachineApplicable,
297-
);
298-
}
299-
lint
280+
NonFmtPanicBraces {
281+
count,
282+
suggestion: is_arg_inside_call(arg.span, span).then_some(arg.span.shrink_to_lo()),
300283
},
301284
);
302285
}

0 commit comments

Comments
 (0)