Skip to content

Commit c97f11a

Browse files
committedJan 10, 2021
Auto merge of #79414 - sasurau4:feature/add-suggestion-for-pattern-in-fns-without-body, r=matthewjasper
Add suggestion for PATTERNS_IN_FNS_WITHOUT_BODY ## Overview Fix #78927
2 parents 080ee6f + e97b97e commit c97f11a

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
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
}
@@ -834,7 +834,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
834834
match ty.kind {
835835
TyKind::BareFn(ref bfty) => {
836836
self.check_fn_decl(&bfty.decl, SelfSemantic::No);
837-
Self::check_decl_no_pat(&bfty.decl, |span, _| {
837+
Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
838838
struct_span_err!(
839839
self.session,
840840
span,
@@ -1289,7 +1289,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12891289

12901290
// Functions without bodies cannot have patterns.
12911291
if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
1292-
Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
1292+
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
12931293
let (code, msg, label) = match ctxt {
12941294
FnCtxt::Foreign => (
12951295
error_code!(E0130),
@@ -1303,7 +1303,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13031303
),
13041304
};
13051305
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
1306-
self.lint_buffer.buffer_lint(PATTERNS_IN_FNS_WITHOUT_BODY, id, span, msg);
1306+
if let Some(ident) = ident {
1307+
let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident);
1308+
self.lint_buffer.buffer_lint_with_diagnostic(
1309+
PATTERNS_IN_FNS_WITHOUT_BODY,
1310+
id,
1311+
span,
1312+
msg,
1313+
diag,
1314+
)
1315+
}
13071316
} else {
13081317
self.err_handler()
13091318
.struct_span_err(span, msg)

‎compiler/rustc_lint/src/context.rs

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

‎src/test/ui/no-patterns-in-args-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ error: patterns aren't allowed in functions without bodies
88
--> $DIR/no-patterns-in-args-2.rs:4:11
99
|
1010
LL | fn f1(mut arg: u8);
11-
| ^^^^^^^
11+
| ^^^^^^^ help: remove `mut` from the parameter: `arg`
1212
|
1313
note: the lint level is defined here
1414
--> $DIR/no-patterns-in-args-2.rs:1:9

0 commit comments

Comments
 (0)
Please sign in to comment.