@@ -6,27 +6,16 @@ use rustc_lint::{LateContext, Lint, LintContext};
6
6
use rustc_span:: source_map:: { MultiSpan , Span } ;
7
7
use std:: env;
8
8
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
+ ) ) ;
30
19
}
31
20
}
32
21
@@ -48,7 +37,11 @@ impl<'a> DiagnosticWrapper<'a> {
48
37
/// | ^^^^^^^^^^^^^^^^^^^^^^^
49
38
/// ```
50
39
pub 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
+ } ) ;
52
45
}
53
46
54
47
/// 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
70
63
/// = help: Consider using `std::f64::NAN` if you would like a constant representing NaN
71
64
/// ```
72
65
pub 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
+ } ) ;
76
72
}
77
73
78
74
/// 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>(
104
100
note_span : Span ,
105
101
note : & str ,
106
102
) {
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
+ } ) ;
114
113
}
115
114
116
115
pub fn span_lint_and_then < ' a , T : LintContext , F > ( cx : & ' a T , lint : & ' static Lint , sp : Span , msg : & str , f : F )
117
116
where
118
117
F : for < ' b > FnOnce ( & mut DiagnosticBuilder < ' b > ) ,
119
118
{
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
+ } ) ;
123
125
}
124
126
125
127
pub 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
+ } ) ;
127
133
}
128
134
129
135
pub fn span_lint_hir_and_then (
@@ -134,9 +140,12 @@ pub fn span_lint_hir_and_then(
134
140
msg : & str ,
135
141
f : impl FnOnce ( & mut DiagnosticBuilder < ' _ > ) ,
136
142
) {
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
+ } ) ;
140
149
}
141
150
142
151
/// Add a span lint with a suggestion on how to fix it.
0 commit comments