@@ -6,27 +6,16 @@ use rustc_lint::{LateContext, Lint, LintContext};
66use rustc_span:: source_map:: { MultiSpan , Span } ;
77use std:: env;
88
9- /// Wrapper around `DiagnosticBuilder` that adds a link to Clippy documentation for the emitted lint
10- struct DiagnosticWrapper < ' a > ( DiagnosticBuilder < ' a > ) ;
11-
12- impl < ' a > Drop for DiagnosticWrapper < ' a > {
13- fn drop ( & mut self ) {
14- self . 0 . emit ( ) ;
15- }
16- }
17-
18- impl < ' a > DiagnosticWrapper < ' a > {
19- fn docs_link ( & mut self , lint : & ' static Lint ) {
20- if env:: var ( "CLIPPY_DISABLE_DOCS_LINKS" ) . is_err ( ) {
21- self . 0 . help ( & format ! (
22- "for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{}" ,
23- & option_env!( "RUST_RELEASE_NUM" ) . map_or( "master" . to_string( ) , |n| {
24- // extract just major + minor version and ignore patch versions
25- format!( "rust-{}" , n. rsplitn( 2 , '.' ) . nth( 1 ) . unwrap( ) )
26- } ) ,
27- lint. name_lower( ) . replacen( "clippy::" , "" , 1 )
28- ) ) ;
29- }
9+ fn docs_link ( db : & mut DiagnosticBuilder < ' _ > , lint : & ' static Lint ) {
10+ if env:: var ( "CLIPPY_DISABLE_DOCS_LINKS" ) . is_err ( ) {
11+ db. help ( & format ! (
12+ "for further information visit https://rust-lang.github.io/rust-clippy/{}/index.html#{}" ,
13+ & option_env!( "RUST_RELEASE_NUM" ) . map_or( "master" . to_string( ) , |n| {
14+ // extract just major + minor version and ignore patch versions
15+ format!( "rust-{}" , n. rsplitn( 2 , '.' ) . nth( 1 ) . unwrap( ) )
16+ } ) ,
17+ lint. name_lower( ) . replacen( "clippy::" , "" , 1 )
18+ ) ) ;
3019 }
3120}
3221
@@ -48,7 +37,11 @@ impl<'a> DiagnosticWrapper<'a> {
4837/// | ^^^^^^^^^^^^^^^^^^^^^^^
4938/// ```
5039pub fn span_lint < T : LintContext > ( cx : & T , lint : & ' static Lint , sp : impl Into < MultiSpan > , msg : & str ) {
51- DiagnosticWrapper ( cx. struct_span_lint ( lint, sp, msg) ) . docs_link ( lint) ;
40+ cx. struct_span_lint ( lint, sp, |ldb| {
41+ let mut db = ldb. build ( msg) ;
42+ docs_link ( & mut db, lint) ;
43+ db. emit ( ) ;
44+ } ) ;
5245}
5346
5447/// Same as `span_lint` but with an extra `help` message.
@@ -70,9 +63,12 @@ pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<Mult
7063/// = help: Consider using `std::f64::NAN` if you would like a constant representing NaN
7164/// ```
7265pub fn span_lint_and_help < ' a , T : LintContext > ( cx : & ' a T , lint : & ' static Lint , span : Span , msg : & str , help : & str ) {
73- let mut db = DiagnosticWrapper ( cx. struct_span_lint ( lint, span, msg) ) ;
74- db. 0 . help ( help) ;
75- db. docs_link ( lint) ;
66+ cx. struct_span_lint ( lint, span, |ldb| {
67+ let mut db = ldb. build ( msg) ;
68+ db. help ( help) ;
69+ docs_link ( & mut db, lint) ;
70+ db. emit ( ) ;
71+ } ) ;
7672}
7773
7874/// 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>(
104100 note_span : Span ,
105101 note : & str ,
106102) {
107- let mut db = DiagnosticWrapper ( cx. struct_span_lint ( lint, span, msg) ) ;
108- if note_span == span {
109- db. 0 . note ( note) ;
110- } else {
111- db. 0 . span_note ( note_span, note) ;
112- }
113- db. docs_link ( lint) ;
103+ cx. struct_span_lint ( lint, span, |ldb| {
104+ let mut db = ldb. build ( msg) ;
105+ if note_span == span {
106+ db. note ( note) ;
107+ } else {
108+ db. span_note ( note_span, note) ;
109+ }
110+ docs_link ( & mut db, lint) ;
111+ db. emit ( ) ;
112+ } ) ;
114113}
115114
116115pub fn span_lint_and_then < ' a , T : LintContext , F > ( cx : & ' a T , lint : & ' static Lint , sp : Span , msg : & str , f : F )
117116where
118117 F : for < ' b > FnOnce ( & mut DiagnosticBuilder < ' b > ) ,
119118{
120- let mut db = DiagnosticWrapper ( cx. struct_span_lint ( lint, sp, msg) ) ;
121- f ( & mut db. 0 ) ;
122- db. docs_link ( lint) ;
119+ cx. struct_span_lint ( lint, sp, |ldb| {
120+ let mut db = ldb. build ( msg) ;
121+ f ( & mut db) ;
122+ docs_link ( & mut db, lint) ;
123+ db. emit ( ) ;
124+ } ) ;
123125}
124126
125127pub fn span_lint_hir ( cx : & LateContext < ' _ , ' _ > , lint : & ' static Lint , hir_id : HirId , sp : Span , msg : & str ) {
126- DiagnosticWrapper ( cx. tcx . struct_span_lint_hir ( lint, hir_id, sp, msg) ) . docs_link ( lint) ;
128+ cx. tcx . struct_span_lint_hir ( lint, hir_id, sp, |ldb| {
129+ let mut db = ldb. build ( msg) ;
130+ docs_link ( & mut db, lint) ;
131+ db. emit ( ) ;
132+ } ) ;
127133}
128134
129135pub fn span_lint_hir_and_then (
@@ -134,9 +140,12 @@ pub fn span_lint_hir_and_then(
134140 msg : & str ,
135141 f : impl FnOnce ( & mut DiagnosticBuilder < ' _ > ) ,
136142) {
137- let mut db = DiagnosticWrapper ( cx. tcx . struct_span_lint_hir ( lint, hir_id, sp, msg) ) ;
138- f ( & mut db. 0 ) ;
139- db. docs_link ( lint) ;
143+ cx. tcx . struct_span_lint_hir ( lint, hir_id, sp, |ldb| {
144+ let mut db = ldb. build ( msg) ;
145+ f ( & mut db) ;
146+ docs_link ( & mut db, lint) ;
147+ db. emit ( ) ;
148+ } ) ;
140149}
141150
142151/// Add a span lint with a suggestion on how to fix it.
0 commit comments