diff --git a/clippy_lints/src/utils/diagnostics.rs b/clippy_lints/src/utils/diagnostics.rs index 9739b118f7494..cc519d5255212 100644 --- a/clippy_lints/src/utils/diagnostics.rs +++ b/clippy_lints/src/utils/diagnostics.rs @@ -6,27 +6,16 @@ use rustc_lint::{LateContext, Lint, LintContext}; use rustc_span::source_map::{MultiSpan, Span}; use std::env; -/// Wrapper around `DiagnosticBuilder` that adds a link to Clippy documentation for the emitted lint -struct DiagnosticWrapper<'a>(DiagnosticBuilder<'a>); - -impl<'a> Drop for DiagnosticWrapper<'a> { - fn drop(&mut self) { - self.0.emit(); - } -} - -impl<'a> DiagnosticWrapper<'a> { - fn docs_link(&mut self, lint: &'static Lint) { - if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() { - self.0.help(&format!( - "for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{}", - &option_env!("RUST_RELEASE_NUM").map_or("master".to_string(), |n| { - // extract just major + minor version and ignore patch versions - format!("rust-{}", n.rsplitn(2, '.').nth(1).unwrap()) - }), - lint.name_lower().replacen("clippy::", "", 1) - )); - } +fn docs_link(db: &mut DiagnosticBuilder<'_>, lint: &'static Lint) { + if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() { + db.help(&format!( + "for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{}", + &option_env!("RUST_RELEASE_NUM").map_or("master".to_string(), |n| { + // extract just major + minor version and ignore patch versions + format!("rust-{}", n.rsplitn(2, '.').nth(1).unwrap()) + }), + lint.name_lower().replacen("clippy::", "", 1) + )); } } @@ -48,7 +37,11 @@ impl<'a> DiagnosticWrapper<'a> { /// | ^^^^^^^^^^^^^^^^^^^^^^^ /// ``` pub fn span_lint(cx: &T, lint: &'static Lint, sp: impl Into, msg: &str) { - DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)).docs_link(lint); + cx.struct_span_lint(lint, sp, |ldb| { + let mut db = ldb.build(msg); + docs_link(&mut db, lint); + db.emit(); + }); } /// Same as `span_lint` but with an extra `help` message. @@ -70,9 +63,12 @@ pub fn span_lint(cx: &T, lint: &'static Lint, sp: impl Into(cx: &'a T, lint: &'static Lint, span: Span, msg: &str, help: &str) { - let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg)); - db.0.help(help); - db.docs_link(lint); + cx.struct_span_lint(lint, span, |ldb| { + let mut db = ldb.build(msg); + db.help(help); + docs_link(&mut db, lint); + db.emit(); + }); } /// Like `span_lint` but with a `note` section instead of a `help` message. @@ -104,26 +100,36 @@ pub fn span_lint_and_note<'a, T: LintContext>( note_span: Span, note: &str, ) { - let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, span, msg)); - if note_span == span { - db.0.note(note); - } else { - db.0.span_note(note_span, note); - } - db.docs_link(lint); + cx.struct_span_lint(lint, span, |ldb| { + let mut db = ldb.build(msg); + if note_span == span { + db.note(note); + } else { + db.span_note(note_span, note); + } + docs_link(&mut db, lint); + db.emit(); + }); } pub fn span_lint_and_then<'a, T: LintContext, F>(cx: &'a T, lint: &'static Lint, sp: Span, msg: &str, f: F) where F: for<'b> FnOnce(&mut DiagnosticBuilder<'b>), { - let mut db = DiagnosticWrapper(cx.struct_span_lint(lint, sp, msg)); - f(&mut db.0); - db.docs_link(lint); + cx.struct_span_lint(lint, sp, |ldb| { + let mut db = ldb.build(msg); + f(&mut db); + docs_link(&mut db, lint); + db.emit(); + }); } pub fn span_lint_hir(cx: &LateContext<'_, '_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) { - DiagnosticWrapper(cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg)).docs_link(lint); + cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |ldb| { + let mut db = ldb.build(msg); + docs_link(&mut db, lint); + db.emit(); + }); } pub fn span_lint_hir_and_then( @@ -134,9 +140,12 @@ pub fn span_lint_hir_and_then( msg: &str, f: impl FnOnce(&mut DiagnosticBuilder<'_>), ) { - let mut db = DiagnosticWrapper(cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg)); - f(&mut db.0); - db.docs_link(lint); + cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |ldb| { + let mut db = ldb.build(msg); + f(&mut db); + docs_link(&mut db, lint); + db.emit(); + }); } /// Add a span lint with a suggestion on how to fix it.