add suggest for PatternsInWithoutBody
This commit is contained in:
parent
463ce40428
commit
acbebd81d4
3 changed files with 21 additions and 8 deletions
|
@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
|
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
|
||||||
use rustc_parse::validate_attr;
|
use rustc_parse::validate_attr;
|
||||||
use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
|
use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
|
||||||
use rustc_session::lint::LintBuffer;
|
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
use rustc_span::symbol::{kw, sym, Ident};
|
use rustc_span::symbol::{kw, sym, Ident};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
@ -213,14 +213,14 @@ impl<'a> AstValidator<'a> {
|
||||||
err.emit();
|
err.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, bool)) {
|
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
|
||||||
for Param { pat, .. } in &decl.inputs {
|
for Param { pat, .. } in &decl.inputs {
|
||||||
match pat.kind {
|
match pat.kind {
|
||||||
PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {}
|
PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {}
|
||||||
PatKind::Ident(BindingMode::ByValue(Mutability::Mut), _, None) => {
|
PatKind::Ident(BindingMode::ByValue(Mutability::Mut), ident, None) => {
|
||||||
report_err(pat.span, true)
|
report_err(pat.span, Some(ident), true)
|
||||||
}
|
}
|
||||||
_ => report_err(pat.span, false),
|
_ => report_err(pat.span, None, false),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -815,7 +815,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
||||||
match ty.kind {
|
match ty.kind {
|
||||||
TyKind::BareFn(ref bfty) => {
|
TyKind::BareFn(ref bfty) => {
|
||||||
self.check_fn_decl(&bfty.decl, SelfSemantic::No);
|
self.check_fn_decl(&bfty.decl, SelfSemantic::No);
|
||||||
Self::check_decl_no_pat(&bfty.decl, |span, _| {
|
Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
|
||||||
struct_span_err!(
|
struct_span_err!(
|
||||||
self.session,
|
self.session,
|
||||||
span,
|
span,
|
||||||
|
@ -1285,7 +1285,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
||||||
|
|
||||||
// Functions without bodies cannot have patterns.
|
// Functions without bodies cannot have patterns.
|
||||||
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
|
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
|
||||||
Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
|
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
|
||||||
let (code, msg, label) = match ctxt {
|
let (code, msg, label) = match ctxt {
|
||||||
FnCtxt::Foreign => (
|
FnCtxt::Foreign => (
|
||||||
error_code!(E0130),
|
error_code!(E0130),
|
||||||
|
@ -1299,7 +1299,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
|
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
|
||||||
self.lint_buffer.buffer_lint(PATTERNS_IN_FNS_WITHOUT_BODY, id, span, msg);
|
if let Some(ident) = ident {
|
||||||
|
let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident);
|
||||||
|
self.lint_buffer.buffer_lint_with_diagnostic(
|
||||||
|
PATTERNS_IN_FNS_WITHOUT_BODY,
|
||||||
|
id,
|
||||||
|
span,
|
||||||
|
msg,
|
||||||
|
diag,
|
||||||
|
)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.err_handler()
|
self.err_handler()
|
||||||
.struct_span_err(span, msg)
|
.struct_span_err(span, msg)
|
||||||
|
|
|
@ -596,6 +596,9 @@ pub trait LintContext: Sized {
|
||||||
db.help("to document an item produced by a macro, \
|
db.help("to document an item produced by a macro, \
|
||||||
the macro must produce the documentation as part of its expansion");
|
the macro must produce the documentation as part of its expansion");
|
||||||
}
|
}
|
||||||
|
BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident) => {
|
||||||
|
db.span_suggestion(span, "remove `mut` from the parameter", ident.to_string(), Applicability::MachineApplicable);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Rewrap `db`, and pass control to the user.
|
// Rewrap `db`, and pass control to the user.
|
||||||
decorate(LintDiagnosticBuilder::new(db));
|
decorate(LintDiagnosticBuilder::new(db));
|
||||||
|
|
|
@ -253,6 +253,7 @@ pub enum BuiltinLintDiagnostics {
|
||||||
RedundantImport(Vec<(Span, bool)>, Ident),
|
RedundantImport(Vec<(Span, bool)>, Ident),
|
||||||
DeprecatedMacro(Option<Symbol>, Span),
|
DeprecatedMacro(Option<Symbol>, Span),
|
||||||
UnusedDocComment(Span),
|
UnusedDocComment(Span),
|
||||||
|
PatternsInFnsWithoutBody(Span, Ident),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lints that are buffered up early on in the `Session` before the
|
/// Lints that are buffered up early on in the `Session` before the
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue