From acbebd81d4968f76f81ff6f4cb61901fa88d6478 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Mon, 30 Nov 2020 23:24:08 +0900 Subject: [PATCH 1/2] add suggest for PatternsInWithoutBody --- .../rustc_ast_passes/src/ast_validation.rs | 25 +++++++++++++------ compiler/rustc_lint/src/context.rs | 3 +++ compiler/rustc_lint_defs/src/lib.rs | 1 + 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index bf6d332217613..8718bba1fd6d4 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_errors::{error_code, pluralize, struct_span_err, Applicability}; use rustc_parse::validate_attr; 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_span::symbol::{kw, sym, Ident}; use rustc_span::Span; @@ -213,14 +213,14 @@ impl<'a> AstValidator<'a> { 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, bool)) { for Param { pat, .. } in &decl.inputs { match pat.kind { PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {} - PatKind::Ident(BindingMode::ByValue(Mutability::Mut), _, None) => { - report_err(pat.span, true) + PatKind::Ident(BindingMode::ByValue(Mutability::Mut), ident, None) => { + 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 { TyKind::BareFn(ref bfty) => { 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!( self.session, span, @@ -1285,7 +1285,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { // Functions without bodies cannot have patterns. 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 { FnCtxt::Foreign => ( error_code!(E0130), @@ -1299,7 +1299,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> { ), }; 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 { self.err_handler() .struct_span_err(span, msg) diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index bfeef4904893a..d0e46426ff531 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -596,6 +596,9 @@ pub trait LintContext: Sized { db.help("to document an item produced by a macro, \ 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. decorate(LintDiagnosticBuilder::new(db)); diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index aec0fc253ca5e..2bfc6a8557691 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -253,6 +253,7 @@ pub enum BuiltinLintDiagnostics { RedundantImport(Vec<(Span, bool)>, Ident), DeprecatedMacro(Option, Span), UnusedDocComment(Span), + PatternsInFnsWithoutBody(Span, Ident), } /// Lints that are buffered up early on in the `Session` before the From e97b97ebcd778337e7ae6afb6868ca4c2f6ebbb3 Mon Sep 17 00:00:00 2001 From: Daiki Ihara Date: Mon, 30 Nov 2020 23:25:07 +0900 Subject: [PATCH 2/2] bless no-patterns-in-args-2 ui test suit --- src/test/ui/no-patterns-in-args-2.stderr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/no-patterns-in-args-2.stderr b/src/test/ui/no-patterns-in-args-2.stderr index 21f4439d89009..98932349a7902 100644 --- a/src/test/ui/no-patterns-in-args-2.stderr +++ b/src/test/ui/no-patterns-in-args-2.stderr @@ -8,7 +8,7 @@ error: patterns aren't allowed in functions without bodies --> $DIR/no-patterns-in-args-2.rs:4:11 | LL | fn f1(mut arg: u8); - | ^^^^^^^ + | ^^^^^^^ help: remove `mut` from the parameter: `arg` | note: the lint level is defined here --> $DIR/no-patterns-in-args-2.rs:1:9