Skip to content

Commit c868f02

Browse files
committed
Fill the diagnostic code field in publish_diagnostics
1 parent 789d9ca commit c868f02

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

crates/hir/src/diagnostics.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! FIXME: write short doc here
22
pub use hir_def::diagnostics::{InactiveCode, UnresolvedModule};
3-
pub use hir_expand::diagnostics::{Diagnostic, DiagnosticSink, DiagnosticSinkBuilder};
3+
pub use hir_expand::diagnostics::{
4+
Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder,
5+
};
46
pub use hir_ty::diagnostics::{
57
IncorrectCase, MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr,
68
NoSuchField,

crates/hir_expand/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax::SyntaxNodePtr;
2020

2121
use crate::InFile;
2222

23-
#[derive(Copy, Clone, PartialEq)]
23+
#[derive(Copy, Clone, Debug, PartialEq)]
2424
pub struct DiagnosticCode(pub &'static str);
2525

2626
impl DiagnosticCode {

crates/ide/src/diagnostics.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod field_shorthand;
1010
use std::cell::RefCell;
1111

1212
use hir::{
13-
diagnostics::{Diagnostic as _, DiagnosticSinkBuilder},
13+
diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder},
1414
Semantics,
1515
};
1616
use 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

4041
impl 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

151166
fn 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

155172
fn 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

159178
fn 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
"#]],

crates/rust-analyzer/src/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use lsp_types::{
1818
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
1919
CodeActionKind, CodeLens, Command, CompletionItem, Diagnostic, DiagnosticTag,
2020
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeParams,
21-
HoverContents, Location, Position, PrepareRenameResponse, Range, RenameParams,
21+
HoverContents, Location, NumberOrString, Position, PrepareRenameResponse, Range, RenameParams,
2222
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
2323
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
2424
SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
@@ -1127,7 +1127,7 @@ pub(crate) fn publish_diagnostics(
11271127
.map(|d| Diagnostic {
11281128
range: to_proto::range(&line_index, d.range),
11291129
severity: Some(to_proto::diagnostic_severity(d.severity)),
1130-
code: None,
1130+
code: d.code.map(|d| d.as_str().to_owned()).map(NumberOrString::String),
11311131
code_description: None,
11321132
source: Some("rust-analyzer".to_string()),
11331133
message: d.message,

0 commit comments

Comments
 (0)