From c868f0255f677a4cedb62c27af96758fffdd6f8f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 17 Nov 2020 16:14:45 +0100 Subject: Fill the diagnostic code field in publish_diagnostics --- crates/ide/src/diagnostics.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 1c7f02763..3df73ed4f 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -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; @@ -35,15 +35,23 @@ pub struct Diagnostic { pub severity: Severity, pub fix: Option, pub unused: bool, + pub code: Option, } 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) -> Self { @@ -53,6 +61,10 @@ impl Diagnostic { fn with_unused(self, unused: bool) -> Self { Self { unused, ..self } } + + fn with_code(self, code: Option) -> Self { + Self { code, ..self } + } } #[derive(Debug)] @@ -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. @@ -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) { @@ -149,11 +164,15 @@ pub(crate) fn diagnostics( } fn diagnostic_with_fix(d: &D, sema: &Semantics) -> 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: &D, sema: &Semantics) -> 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( @@ -589,6 +608,11 @@ fn test_fn() { }, ), unused: false, + code: Some( + DiagnosticCode( + "unresolved-module", + ), + ), }, ] "#]], -- cgit v1.2.3