@@ -61,7 +61,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
6161 } ;
6262
6363 let hir_id = self . cx . tcx . hir ( ) . local_def_id_to_hir_id ( local_id) ;
64- let suggest_using_text = code_block. syntax . is_none ( ) && code_block. is_fenced ;
64+ let empty_block = code_block. syntax . is_none ( ) && code_block. is_fenced ;
6565 let is_ignore = code_block. is_ignore ;
6666
6767 // The span and whether it is precise or not.
@@ -78,40 +78,38 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
7878 // lambda that will use the lint to start a new diagnostic and add
7979 // a suggestion to it when needed.
8080 let diag_builder = |lint : LintDiagnosticBuilder < ' _ > | {
81- let mut diag = if precise_span {
82- let msg = if buffer. has_errors {
83- "could not parse code block as Rust code"
84- } else {
85- "Rust code block is empty"
86- } ;
87-
88- let mut diag = lint. build ( msg) ;
89-
90- if suggest_using_text {
91- let extended_msg = if is_ignore {
92- "`ignore` code blocks require valid Rust code for syntax highlighting. \
93- Mark blocks that do not contain Rust code as text"
94- } else {
95- "mark blocks that do not contain Rust code as text"
96- } ;
81+ let explanation = if is_ignore {
82+ "`ignore` code blocks require valid Rust code for syntax highlighting; \
83+ mark blocks that do not contain Rust code as text"
84+ } else {
85+ "mark blocks that do not contain Rust code as text"
86+ } ;
87+ let msg = if buffer. has_errors {
88+ "could not parse code block as Rust code"
89+ } else {
90+ "Rust code block is empty"
91+ } ;
92+ let mut diag = lint. build ( msg) ;
9793
94+ if precise_span {
95+ if is_ignore {
96+ // giving an accurate suggestion is hard because `ignore` might not have come first in the list.
97+ // just give a `help` instead.
98+ diag. span_help (
99+ sp. from_inner ( InnerSpan :: new ( 0 , 3 ) ) ,
100+ & format ! ( "{}: ```text" , explanation) ,
101+ ) ;
102+ } else if empty_block {
98103 diag. span_suggestion (
99104 sp. from_inner ( InnerSpan :: new ( 0 , 3 ) ) ,
100- extended_msg ,
105+ explanation ,
101106 String :: from ( "```text" ) ,
102107 Applicability :: MachineApplicable ,
103108 ) ;
104109 }
105-
106- diag
107- } else {
108- let mut diag = lint. build ( "doc comment contains an invalid Rust code block" ) ;
109- if suggest_using_text {
110- diag. help ( "mark blocks that do not contain Rust code as text: ```text" ) ;
111- }
112-
113- diag
114- } ;
110+ } else if empty_block || is_ignore {
111+ diag. help ( & format ! ( "{}: ```text" , explanation) ) ;
112+ }
115113
116114 // FIXME(#67563): Provide more context for these errors by displaying the spans inline.
117115 for message in buffer. messages . iter ( ) {
0 commit comments