Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! FIXME: write short doc here
pub use hir_def::diagnostics::{InactiveCode, UnresolvedModule};
pub use hir_expand::diagnostics::{Diagnostic, DiagnosticSink, DiagnosticSinkBuilder};
pub use hir_expand::diagnostics::{
Diagnostic, DiagnosticCode, DiagnosticSink, DiagnosticSinkBuilder,
};
pub use hir_ty::diagnostics::{
IncorrectCase, MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr,
NoSuchField,
Expand Down
2 changes: 1 addition & 1 deletion crates/hir_expand/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use syntax::SyntaxNodePtr;

use crate::InFile;

#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct DiagnosticCode(pub &'static str);

impl DiagnosticCode {
Expand Down
40 changes: 32 additions & 8 deletions crates/ide/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod field_shorthand;
use std::cell::RefCell;

use hir::{
diagnostics::{Diagnostic as _, DiagnosticSinkBuilder},
diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder},
Semantics,
};
use ide_db::base_db::SourceDatabase;
Expand All @@ -35,15 +35,23 @@ pub struct Diagnostic {
pub severity: Severity,
pub fix: Option<Fix>,
pub unused: bool,
pub code: Option<DiagnosticCode>,
}

impl Diagnostic {
fn error(range: TextRange, message: String) -> Self {
Self { message, range, severity: Severity::Error, fix: None, unused: false }
Self { message, range, severity: Severity::Error, fix: None, unused: false, code: None }
}

fn hint(range: TextRange, message: String) -> Self {
Self { message, range, severity: Severity::WeakWarning, fix: None, unused: false }
Self {
message,
range,
severity: Severity::WeakWarning,
fix: None,
unused: false,
code: None,
}
}

fn with_fix(self, fix: Option<Fix>) -> Self {
Expand All @@ -53,6 +61,10 @@ impl Diagnostic {
fn with_unused(self, unused: bool) -> Self {
Self { unused, ..self }
}

fn with_code(self, code: Option<DiagnosticCode>) -> Self {
Self { code, ..self }
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -126,7 +138,8 @@ pub(crate) fn diagnostics(
// Override severity and mark as unused.
res.borrow_mut().push(
Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message())
.with_unused(true),
.with_unused(true)
.with_code(Some(d.code())),
);
})
// Only collect experimental diagnostics when they're enabled.
Expand All @@ -137,8 +150,10 @@ pub(crate) fn diagnostics(
let mut sink = sink_builder
// Diagnostics not handled above get no fix and default treatment.
.build(|d| {
res.borrow_mut()
.push(Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()));
res.borrow_mut().push(
Diagnostic::error(sema.diagnostics_display_range(d).range, d.message())
.with_code(Some(d.code())),
);
});

if let Some(m) = sema.to_module_def(file_id) {
Expand All @@ -149,11 +164,15 @@ pub(crate) fn diagnostics(
}

fn diagnostic_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic {
Diagnostic::error(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema))
Diagnostic::error(sema.diagnostics_display_range(d).range, d.message())
.with_fix(d.fix(&sema))
.with_code(Some(d.code()))
}

fn warning_with_fix<D: DiagnosticWithFix>(d: &D, sema: &Semantics<RootDatabase>) -> Diagnostic {
Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message()).with_fix(d.fix(&sema))
Diagnostic::hint(sema.diagnostics_display_range(d).range, d.message())
.with_fix(d.fix(&sema))
.with_code(Some(d.code()))
}

fn check_unnecessary_braces_in_use_statement(
Expand Down Expand Up @@ -589,6 +608,11 @@ fn test_fn() {
},
),
unused: false,
code: Some(
DiagnosticCode(
"unresolved-module",
),
),
},
]
"#]],
Expand Down
4 changes: 2 additions & 2 deletions crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use lsp_types::{
CallHierarchyOutgoingCall, CallHierarchyOutgoingCallsParams, CallHierarchyPrepareParams,
CodeActionKind, CodeLens, Command, CompletionItem, Diagnostic, DiagnosticTag,
DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeParams,
HoverContents, Location, Position, PrepareRenameResponse, Range, RenameParams,
HoverContents, Location, NumberOrString, Position, PrepareRenameResponse, Range, RenameParams,
SemanticTokensDeltaParams, SemanticTokensFullDeltaResult, SemanticTokensParams,
SemanticTokensRangeParams, SemanticTokensRangeResult, SemanticTokensResult, SymbolInformation,
SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
Expand Down Expand Up @@ -1127,7 +1127,7 @@ pub(crate) fn publish_diagnostics(
.map(|d| Diagnostic {
range: to_proto::range(&line_index, d.range),
severity: Some(to_proto::diagnostic_severity(d.severity)),
code: None,
code: d.code.map(|d| d.as_str().to_owned()).map(NumberOrString::String),
code_description: None,
source: Some("rust-analyzer".to_string()),
message: d.message,
Expand Down