mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-21 11:23:03 +00:00
migrate: non_fmt_panic.rs
This commit is contained in:
parent
a9bbe31519
commit
384010b9f4
@ -6,6 +6,43 @@ use rustc_span::{symbol::Ident, Span, Symbol};
|
|||||||
|
|
||||||
use crate::LateContext;
|
use crate::LateContext;
|
||||||
|
|
||||||
|
pub struct NonFmtPanicUnused {
|
||||||
|
pub count: usize,
|
||||||
|
pub suggestion: Option<Span>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<G: EmissionGuarantee> DecorateLint<'_, G> for NonFmtPanicUnused {
|
||||||
|
fn decorate_lint(self, diag: rustc_errors::LintDiagnosticBuilder<'_, G>) {
|
||||||
|
let mut diag = diag.build(fluent::lint_non_fmt_panic_unused);
|
||||||
|
diag.set_arg("count", self.count);
|
||||||
|
diag.note(fluent::note);
|
||||||
|
if let Some(span) = self.suggestion {
|
||||||
|
diag.span_suggestion(
|
||||||
|
span.shrink_to_hi(),
|
||||||
|
fluent::add_args_suggestion,
|
||||||
|
", ...",
|
||||||
|
Applicability::HasPlaceholders,
|
||||||
|
);
|
||||||
|
diag.span_suggestion(
|
||||||
|
span.shrink_to_lo(),
|
||||||
|
fluent::add_fmt_suggestion,
|
||||||
|
"\"{}\", ",
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
diag.emit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(LintDiagnostic)]
|
||||||
|
#[diag(lint_non_fmt_panic_braces)]
|
||||||
|
#[note]
|
||||||
|
pub struct NonFmtPanicBraces {
|
||||||
|
pub count: usize,
|
||||||
|
#[suggestion(code = "\"{{}}\", ", applicability = "machine-applicable")]
|
||||||
|
pub suggestion: Option<Span>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(LintDiagnostic)]
|
||||||
#[diag(lint_non_camel_case_type)]
|
#[diag(lint_non_camel_case_type)]
|
||||||
pub struct NonCamelCaseType<'a> {
|
pub struct NonCamelCaseType<'a> {
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
#![deny(rustc::untranslatable_diagnostic)]
|
||||||
|
#![deny(rustc::diagnostic_outside_of_impl)]
|
||||||
|
use crate::lints::{NonFmtPanicBraces, NonFmtPanicUnused};
|
||||||
use crate::{LateContext, LateLintPass, LintContext};
|
use crate::{LateContext, LateLintPass, LintContext};
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
use rustc_errors::{fluent, Applicability};
|
use rustc_errors::{fluent, Applicability};
|
||||||
@ -118,6 +121,7 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
|
|||||||
arg_span = expn.call_site;
|
arg_span = expn.call_site;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(rustc::diagnostic_outside_of_impl)]
|
||||||
cx.struct_span_lint(NON_FMT_PANICS, arg_span, fluent::lint_non_fmt_panic, |lint| {
|
cx.struct_span_lint(NON_FMT_PANICS, arg_span, fluent::lint_non_fmt_panic, |lint| {
|
||||||
lint.set_arg("name", symbol);
|
lint.set_arg("name", symbol);
|
||||||
lint.note(fluent::note);
|
lint.note(fluent::note);
|
||||||
@ -253,25 +257,14 @@ fn check_panic_str<'tcx>(
|
|||||||
.map(|span| fmt_span.from_inner(InnerSpan::new(span.start, span.end)))
|
.map(|span| fmt_span.from_inner(InnerSpan::new(span.start, span.end)))
|
||||||
.collect(),
|
.collect(),
|
||||||
};
|
};
|
||||||
cx.struct_span_lint(NON_FMT_PANICS, arg_spans, fluent::lint_non_fmt_panic_unused, |lint| {
|
cx.emit_spanned_lint(
|
||||||
lint.set_arg("count", n_arguments);
|
NON_FMT_PANICS,
|
||||||
lint.note(fluent::note);
|
arg_spans,
|
||||||
if is_arg_inside_call(arg.span, span) {
|
NonFmtPanicUnused {
|
||||||
lint.span_suggestion(
|
count: n_arguments,
|
||||||
arg.span.shrink_to_hi(),
|
suggestion: is_arg_inside_call(arg.span, span).then_some(arg.span),
|
||||||
fluent::add_args_suggestion,
|
},
|
||||||
", ...",
|
);
|
||||||
Applicability::HasPlaceholders,
|
|
||||||
);
|
|
||||||
lint.span_suggestion(
|
|
||||||
arg.span.shrink_to_lo(),
|
|
||||||
fluent::add_fmt_suggestion,
|
|
||||||
"\"{}\", ",
|
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
lint
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
let brace_spans: Option<Vec<_>> =
|
let brace_spans: Option<Vec<_>> =
|
||||||
snippet.filter(|s| s.starts_with('"') || s.starts_with("r#")).map(|s| {
|
snippet.filter(|s| s.starts_with('"') || s.starts_with("r#")).map(|s| {
|
||||||
@ -281,22 +274,12 @@ fn check_panic_str<'tcx>(
|
|||||||
.collect()
|
.collect()
|
||||||
});
|
});
|
||||||
let count = brace_spans.as_ref().map(|v| v.len()).unwrap_or(/* any number >1 */ 2);
|
let count = brace_spans.as_ref().map(|v| v.len()).unwrap_or(/* any number >1 */ 2);
|
||||||
cx.struct_span_lint(
|
cx.emit_spanned_lint(
|
||||||
NON_FMT_PANICS,
|
NON_FMT_PANICS,
|
||||||
brace_spans.unwrap_or_else(|| vec![span]),
|
brace_spans.unwrap_or_else(|| vec![span]),
|
||||||
fluent::lint_non_fmt_panic_braces,
|
NonFmtPanicBraces {
|
||||||
|lint| {
|
count,
|
||||||
lint.set_arg("count", count);
|
suggestion: is_arg_inside_call(arg.span, span).then_some(arg.span.shrink_to_lo()),
|
||||||
lint.note(fluent::note);
|
|
||||||
if is_arg_inside_call(arg.span, span) {
|
|
||||||
lint.span_suggestion(
|
|
||||||
arg.span.shrink_to_lo(),
|
|
||||||
fluent::suggestion,
|
|
||||||
"\"{}\", ",
|
|
||||||
Applicability::MachineApplicable,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
lint
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user