@@ -10,7 +10,7 @@ mod field_shorthand;
1010use std:: cell:: RefCell ;
1111
1212use hir:: {
13- diagnostics:: { Diagnostic as _, DiagnosticSinkBuilder } ,
13+ diagnostics:: { Diagnostic as _, DiagnosticCode , DiagnosticSinkBuilder } ,
1414 Semantics ,
1515} ;
1616use ide_db:: base_db:: SourceDatabase ;
@@ -35,15 +35,23 @@ pub struct Diagnostic {
3535 pub severity : Severity ,
3636 pub fix : Option < Fix > ,
3737 pub unused : bool ,
38+ pub code : Option < DiagnosticCode > ,
3839}
3940
4041impl Diagnostic {
4142 fn error ( range : TextRange , message : String ) -> Self {
42- Self { message, range, severity : Severity :: Error , fix : None , unused : false }
43+ Self { message, range, severity : Severity :: Error , fix : None , unused : false , code : None }
4344 }
4445
4546 fn hint ( range : TextRange , message : String ) -> Self {
46- Self { message, range, severity : Severity :: WeakWarning , fix : None , unused : false }
47+ Self {
48+ message,
49+ range,
50+ severity : Severity :: WeakWarning ,
51+ fix : None ,
52+ unused : false ,
53+ code : None ,
54+ }
4755 }
4856
4957 fn with_fix ( self , fix : Option < Fix > ) -> Self {
@@ -53,6 +61,10 @@ impl Diagnostic {
5361 fn with_unused ( self , unused : bool ) -> Self {
5462 Self { unused, ..self }
5563 }
64+
65+ fn with_code ( self , code : Option < DiagnosticCode > ) -> Self {
66+ Self { code, ..self }
67+ }
5668}
5769
5870#[ derive( Debug ) ]
@@ -126,7 +138,8 @@ pub(crate) fn diagnostics(
126138 // Override severity and mark as unused.
127139 res. borrow_mut ( ) . push (
128140 Diagnostic :: hint ( sema. diagnostics_display_range ( d) . range , d. message ( ) )
129- . with_unused ( true ) ,
141+ . with_unused ( true )
142+ . with_code ( Some ( d. code ( ) ) ) ,
130143 ) ;
131144 } )
132145 // Only collect experimental diagnostics when they're enabled.
@@ -137,8 +150,10 @@ pub(crate) fn diagnostics(
137150 let mut sink = sink_builder
138151 // Diagnostics not handled above get no fix and default treatment.
139152 . build ( |d| {
140- res. borrow_mut ( )
141- . push ( Diagnostic :: error ( sema. diagnostics_display_range ( d) . range , d. message ( ) ) ) ;
153+ res. borrow_mut ( ) . push (
154+ Diagnostic :: error ( sema. diagnostics_display_range ( d) . range , d. message ( ) )
155+ . with_code ( Some ( d. code ( ) ) ) ,
156+ ) ;
142157 } ) ;
143158
144159 if let Some ( m) = sema. to_module_def ( file_id) {
@@ -149,11 +164,15 @@ pub(crate) fn diagnostics(
149164}
150165
151166fn diagnostic_with_fix < D : DiagnosticWithFix > ( d : & D , sema : & Semantics < RootDatabase > ) -> Diagnostic {
152- Diagnostic :: error ( sema. diagnostics_display_range ( d) . range , d. message ( ) ) . with_fix ( d. fix ( & sema) )
167+ Diagnostic :: error ( sema. diagnostics_display_range ( d) . range , d. message ( ) )
168+ . with_fix ( d. fix ( & sema) )
169+ . with_code ( Some ( d. code ( ) ) )
153170}
154171
155172fn warning_with_fix < D : DiagnosticWithFix > ( d : & D , sema : & Semantics < RootDatabase > ) -> Diagnostic {
156- Diagnostic :: hint ( sema. diagnostics_display_range ( d) . range , d. message ( ) ) . with_fix ( d. fix ( & sema) )
173+ Diagnostic :: hint ( sema. diagnostics_display_range ( d) . range , d. message ( ) )
174+ . with_fix ( d. fix ( & sema) )
175+ . with_code ( Some ( d. code ( ) ) )
157176}
158177
159178fn check_unnecessary_braces_in_use_statement (
@@ -589,6 +608,11 @@ fn test_fn() {
589608 },
590609 ),
591610 unused: false,
611+ code: Some(
612+ DiagnosticCode(
613+ "unresolved-module",
614+ ),
615+ ),
592616 },
593617 ]
594618 "# ] ] ,
0 commit comments