@@ -202,6 +202,77 @@ impl<'a> LintDiagnosticBuilder<'a> {
202202 }
203203}
204204
205+ pub fn explain_lint_level_source < ' s > (
206+ sess : & ' s Session ,
207+ lint : & ' static Lint ,
208+ level : Level ,
209+ src : LintLevelSource ,
210+ err : & mut DiagnosticBuilder < ' s > ,
211+ ) {
212+ let name = lint. name_lower ( ) ;
213+ match src {
214+ LintLevelSource :: Default => {
215+ sess. diag_note_once (
216+ err,
217+ DiagnosticMessageId :: from ( lint) ,
218+ & format ! ( "`#[{}({})]` on by default" , level. as_str( ) , name) ,
219+ ) ;
220+ }
221+ LintLevelSource :: CommandLine ( lint_flag_val, orig_level) => {
222+ let flag = match orig_level {
223+ Level :: Warn => "-W" ,
224+ Level :: Deny => "-D" ,
225+ Level :: Forbid => "-F" ,
226+ Level :: Allow => "-A" ,
227+ Level :: ForceWarn => "--force-warn" ,
228+ } ;
229+ let hyphen_case_lint_name = name. replace ( '_' , "-" ) ;
230+ if lint_flag_val. as_str ( ) == name {
231+ sess. diag_note_once (
232+ err,
233+ DiagnosticMessageId :: from ( lint) ,
234+ & format ! (
235+ "requested on the command line with `{} {}`" ,
236+ flag, hyphen_case_lint_name
237+ ) ,
238+ ) ;
239+ } else {
240+ let hyphen_case_flag_val = lint_flag_val. as_str ( ) . replace ( '_' , "-" ) ;
241+ sess. diag_note_once (
242+ err,
243+ DiagnosticMessageId :: from ( lint) ,
244+ & format ! (
245+ "`{} {}` implied by `{} {}`" ,
246+ flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
247+ ) ,
248+ ) ;
249+ }
250+ }
251+ LintLevelSource :: Node ( lint_attr_name, src, reason) => {
252+ if let Some ( rationale) = reason {
253+ err. note ( rationale. as_str ( ) ) ;
254+ }
255+ sess. diag_span_note_once (
256+ err,
257+ DiagnosticMessageId :: from ( lint) ,
258+ src,
259+ "the lint level is defined here" ,
260+ ) ;
261+ if lint_attr_name. as_str ( ) != name {
262+ let level_str = level. as_str ( ) ;
263+ sess. diag_note_once (
264+ err,
265+ DiagnosticMessageId :: from ( lint) ,
266+ & format ! (
267+ "`#[{}({})]` implied by `#[{}({})]`" ,
268+ level_str, name, level_str, lint_attr_name
269+ ) ,
270+ ) ;
271+ }
272+ }
273+ }
274+ }
275+
205276pub fn struct_lint_level < ' s , ' d > (
206277 sess : & ' s Session ,
207278 lint : & ' static Lint ,
@@ -277,69 +348,9 @@ pub fn struct_lint_level<'s, 'd>(
277348 }
278349 }
279350
280- let name = lint. name_lower ( ) ;
281- match src {
282- LintLevelSource :: Default => {
283- sess. diag_note_once (
284- & mut err,
285- DiagnosticMessageId :: from ( lint) ,
286- & format ! ( "`#[{}({})]` on by default" , level. as_str( ) , name) ,
287- ) ;
288- }
289- LintLevelSource :: CommandLine ( lint_flag_val, orig_level) => {
290- let flag = match orig_level {
291- Level :: Warn => "-W" ,
292- Level :: Deny => "-D" ,
293- Level :: Forbid => "-F" ,
294- Level :: Allow => "-A" ,
295- Level :: ForceWarn => "--force-warn" ,
296- } ;
297- let hyphen_case_lint_name = name. replace ( '_' , "-" ) ;
298- if lint_flag_val. as_str ( ) == name {
299- sess. diag_note_once (
300- & mut err,
301- DiagnosticMessageId :: from ( lint) ,
302- & format ! (
303- "requested on the command line with `{} {}`" ,
304- flag, hyphen_case_lint_name
305- ) ,
306- ) ;
307- } else {
308- let hyphen_case_flag_val = lint_flag_val. as_str ( ) . replace ( '_' , "-" ) ;
309- sess. diag_note_once (
310- & mut err,
311- DiagnosticMessageId :: from ( lint) ,
312- & format ! (
313- "`{} {}` implied by `{} {}`" ,
314- flag, hyphen_case_lint_name, flag, hyphen_case_flag_val
315- ) ,
316- ) ;
317- }
318- }
319- LintLevelSource :: Node ( lint_attr_name, src, reason) => {
320- if let Some ( rationale) = reason {
321- err. note ( rationale. as_str ( ) ) ;
322- }
323- sess. diag_span_note_once (
324- & mut err,
325- DiagnosticMessageId :: from ( lint) ,
326- src,
327- "the lint level is defined here" ,
328- ) ;
329- if lint_attr_name. as_str ( ) != name {
330- let level_str = level. as_str ( ) ;
331- sess. diag_note_once (
332- & mut err,
333- DiagnosticMessageId :: from ( lint) ,
334- & format ! (
335- "`#[{}({})]` implied by `#[{}({})]`" ,
336- level_str, name, level_str, lint_attr_name
337- ) ,
338- ) ;
339- }
340- }
341- }
351+ explain_lint_level_source ( sess, lint, level, src, & mut err) ;
342352
353+ let name = lint. name_lower ( ) ;
343354 let is_force_warn = matches ! ( level, Level :: ForceWarn ) ;
344355 err. code ( DiagnosticId :: Lint { name, has_future_breakage, is_force_warn } ) ;
345356
0 commit comments