22use rustc_data_structures:: sync:: { Lock , Lrc } ;
33use rustc_errors:: {
44 emitter:: Emitter , translation:: Translate , Applicability , Diagnostic , Handler ,
5- LazyFallbackBundle , LintDiagnosticBuilder ,
5+ LazyFallbackBundle ,
66} ;
77use rustc_parse:: parse_stream_from_source_str;
88use rustc_session:: parse:: ParseSess ;
@@ -97,48 +97,10 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
9797 None => ( item. attr_span ( self . cx . tcx ) , false ) ,
9898 } ;
9999
100- // lambda that will use the lint to start a new diagnostic and add
101- // a suggestion to it when needed.
102- let diag_builder = |lint : LintDiagnosticBuilder < ' _ , ( ) > | {
103- let explanation = if is_ignore {
104- "`ignore` code blocks require valid Rust code for syntax highlighting; \
105- mark blocks that do not contain Rust code as text"
106- } else {
107- "mark blocks that do not contain Rust code as text"
108- } ;
109- let msg = if buffer. has_errors {
110- "could not parse code block as Rust code"
111- } else {
112- "Rust code block is empty"
113- } ;
114- let mut diag = lint. build ( msg) ;
115-
116- if precise_span {
117- if is_ignore {
118- // giving an accurate suggestion is hard because `ignore` might not have come first in the list.
119- // just give a `help` instead.
120- diag. span_help (
121- sp. from_inner ( InnerSpan :: new ( 0 , 3 ) ) ,
122- & format ! ( "{}: ```text" , explanation) ,
123- ) ;
124- } else if empty_block {
125- diag. span_suggestion (
126- sp. from_inner ( InnerSpan :: new ( 0 , 3 ) ) . shrink_to_hi ( ) ,
127- explanation,
128- "text" ,
129- Applicability :: MachineApplicable ,
130- ) ;
131- }
132- } else if empty_block || is_ignore {
133- diag. help ( & format ! ( "{}: ```text" , explanation) ) ;
134- }
135-
136- // FIXME(#67563): Provide more context for these errors by displaying the spans inline.
137- for message in buffer. messages . iter ( ) {
138- diag. note ( message) ;
139- }
140-
141- diag. emit ( ) ;
100+ let msg = if buffer. has_errors {
101+ "could not parse code block as Rust code"
102+ } else {
103+ "Rust code block is empty"
142104 } ;
143105
144106 // Finally build and emit the completed diagnostic.
@@ -148,7 +110,42 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
148110 crate :: lint:: INVALID_RUST_CODEBLOCKS ,
149111 hir_id,
150112 sp,
151- diag_builder,
113+ msg,
114+ |lint| {
115+ let explanation = if is_ignore {
116+ "`ignore` code blocks require valid Rust code for syntax highlighting; \
117+ mark blocks that do not contain Rust code as text"
118+ } else {
119+ "mark blocks that do not contain Rust code as text"
120+ } ;
121+
122+ if precise_span {
123+ if is_ignore {
124+ // giving an accurate suggestion is hard because `ignore` might not have come first in the list.
125+ // just give a `help` instead.
126+ lint. span_help (
127+ sp. from_inner ( InnerSpan :: new ( 0 , 3 ) ) ,
128+ & format ! ( "{}: ```text" , explanation) ,
129+ ) ;
130+ } else if empty_block {
131+ lint. span_suggestion (
132+ sp. from_inner ( InnerSpan :: new ( 0 , 3 ) ) . shrink_to_hi ( ) ,
133+ explanation,
134+ "text" ,
135+ Applicability :: MachineApplicable ,
136+ ) ;
137+ }
138+ } else if empty_block || is_ignore {
139+ lint. help ( & format ! ( "{}: ```text" , explanation) ) ;
140+ }
141+
142+ // FIXME(#67563): Provide more context for these errors by displaying the spans inline.
143+ for message in buffer. messages . iter ( ) {
144+ lint. note ( message) ;
145+ }
146+
147+ lint
148+ } ,
152149 ) ;
153150 }
154151}
0 commit comments