Skip to content
37 changes: 19 additions & 18 deletions slicec/src/diagnostics/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ pub struct Diagnostic {
}

impl Diagnostic {
pub fn new(kind: impl Into<DiagnosticKind>) -> Self {
let kind = kind.into();
/// Creates a new `Diagnostic` directly from a [`DiagnosticKind`].
/// The newly created `Diagnostic` has no `span`, `scope`, or `notes` set.
pub fn new(kind: DiagnosticKind) -> Self {
let level = match &kind {
DiagnosticKind::Error(_) => DiagnosticLevel::Error,
DiagnosticKind::Lint(lint) => lint.get_default_level(),
DiagnosticKind::Lint(lint) => lint.default_diagnostic_level(),
};
Comment thread
InsertCreativityHere marked this conversation as resolved.

Diagnostic {
Expand All @@ -34,6 +35,18 @@ impl Diagnostic {
}
}

/// Creates a new error `Diagnostic` from the provided [`Error`].
/// The newly created `Diagnostic` has no `span`, `scope`, or `notes` set.
pub fn from_error(error: Error) -> Self {
Self::new(DiagnosticKind::Error(error))
}

/// Creates a new lint `Diagnostic` from the provided [`Lint`].
/// The newly created `Diagnostic` has no `span`, `scope`, or `notes` set.
pub fn from_lint(lint: Lint) -> Self {
Self::new(DiagnosticKind::Lint(lint))
}

/// Returns the message of this diagnostic.
pub fn message(&self) -> String {
match &self.kind {
Expand All @@ -45,8 +58,8 @@ impl Diagnostic {
/// Returns this diagnostic's code. This is either the name of a lint or of the form `E###`.
pub fn code(&self) -> &str {
match &self.kind {
DiagnosticKind::Error(error) => error.code(),
DiagnosticKind::Lint(lint) => lint.code(),
DiagnosticKind::Error(error) => error.error_code(),
DiagnosticKind::Lint(lint) => lint.lint_name(),
}
}

Expand Down Expand Up @@ -105,18 +118,6 @@ pub enum DiagnosticKind {
Lint(Lint),
}

impl From<Error> for DiagnosticKind {
fn from(error: Error) -> Self {
DiagnosticKind::Error(error)
}
}

impl From<Lint> for DiagnosticKind {
fn from(lint: Lint) -> Self {
DiagnosticKind::Lint(lint)
}
}
Comment on lines -108 to -118
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are no longer needed now that we have dedicated constructors for error and lint, instead of using From/Into casts.


/// Diagnostic levels describe the severity of a diagnostic, and how the compiler should react to their emission.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum DiagnosticLevel {
Expand Down Expand Up @@ -160,7 +161,7 @@ impl Diagnostics {
pub fn into_updated(mut self, ast: &Ast, files: &[SliceFile], options: &SliceOptions) -> Vec<Diagnostic> {
// Helper function that checks whether a lint should be allowed according to the provided identifiers.
fn is_lint_allowed_by<'b>(mut identifiers: impl Iterator<Item = &'b String>, lint: &Lint) -> bool {
identifiers.any(|identifier| identifier == "All" || identifier == lint.code())
identifiers.any(|identifier| identifier == "All" || identifier == lint.lint_name())
}

// Helper function that checks whether a lint is allowed by attributes on the provided entity.
Expand Down
Loading