Skip to content

Commit acbebd8

Browse files
committed
add suggest for PatternsInWithoutBody
1 parent 463ce40 commit acbebd8

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashMap;
1616
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
1717
use rustc_parse::validate_attr;
1818
use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
19-
use rustc_session::lint::LintBuffer;
19+
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
2020
use rustc_session::Session;
2121
use rustc_span::symbol::{kw, sym, Ident};
2222
use rustc_span::Span;
@@ -213,14 +213,14 @@ impl<'a> AstValidator<'a> {
213213
err.emit();
214214
}
215215

216-
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, bool)) {
216+
fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
217217
for Param { pat, .. } in &decl.inputs {
218218
match pat.kind {
219219
PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {}
220-
PatKind::Ident(BindingMode::ByValue(Mutability::Mut), _, None) => {
221-
report_err(pat.span, true)
220+
PatKind::Ident(BindingMode::ByValue(Mutability::Mut), ident, None) => {
221+
report_err(pat.span, Some(ident), true)
222222
}
223-
_ => report_err(pat.span, false),
223+
_ => report_err(pat.span, None, false),
224224
}
225225
}
226226
}
@@ -815,7 +815,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
815815
match ty.kind {
816816
TyKind::BareFn(ref bfty) => {
817817
self.check_fn_decl(&bfty.decl, SelfSemantic::No);
818-
Self::check_decl_no_pat(&bfty.decl, |span, _| {
818+
Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
819819
struct_span_err!(
820820
self.session,
821821
span,
@@ -1285,7 +1285,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12851285

12861286
// Functions without bodies cannot have patterns.
12871287
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
1288-
Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
1288+
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
12891289
let (code, msg, label) = match ctxt {
12901290
FnCtxt::Foreign => (
12911291
error_code!(E0130),
@@ -1299,7 +1299,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12991299
),
13001300
};
13011301
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
1302-
self.lint_buffer.buffer_lint(PATTERNS_IN_FNS_WITHOUT_BODY, id, span, msg);
1302+
if let Some(ident) = ident {
1303+
let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident);
1304+
self.lint_buffer.buffer_lint_with_diagnostic(
1305+
PATTERNS_IN_FNS_WITHOUT_BODY,
1306+
id,
1307+
span,
1308+
msg,
1309+
diag,
1310+
)
1311+
}
13031312
} else {
13041313
self.err_handler()
13051314
.struct_span_err(span, msg)

compiler/rustc_lint/src/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ pub trait LintContext: Sized {
596596
db.help("to document an item produced by a macro, \
597597
the macro must produce the documentation as part of its expansion");
598598
}
599+
BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident) => {
600+
db.span_suggestion(span, "remove `mut` from the parameter", ident.to_string(), Applicability::MachineApplicable);
601+
}
599602
}
600603
// Rewrap `db`, and pass control to the user.
601604
decorate(LintDiagnosticBuilder::new(db));

compiler/rustc_lint_defs/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ pub enum BuiltinLintDiagnostics {
253253
RedundantImport(Vec<(Span, bool)>, Ident),
254254
DeprecatedMacro(Option<Symbol>, Span),
255255
UnusedDocComment(Span),
256+
PatternsInFnsWithoutBody(Span, Ident),
256257
}
257258

258259
/// Lints that are buffered up early on in the `Session` before the

0 commit comments

Comments
 (0)