Repository: astral-sh/ruff
File: crates/ruff_server/src/lint.rs (Rust)

Code before:
        .flatten();

    let range: lsp_types::Range;
    let cell: usize;

    if let Some(notebook_index) = source_kind.as_ipy_notebook().map(Notebook::index) {
        NotebookRange { cell, range } = diagnostic_range.to_notebook_range(
            source_kind.source_code(),
            index,
            notebook_index,
            encoding,
        );
    } else {
        cell = usize::default();
        range = diagnostic_range.to_range(source_kind.source_code(), index, encoding);
    }

    let (severity, tags, code) = if let Some(code) = code {
        let code = code.to_string();
        (
            severity(&code),
            tags(diagnostic),
            Some(lsp_types::NumberOrString::String(code)),
        )
    } else {
        let severity = match diagnostic.severity() {
            ruff_db::diagnostic::Severity::Info => lsp_types::DiagnosticSeverity::INFORMATION,
            ruff_db::diagnostic::Severity::Warning => lsp_types::DiagnosticSeverity::WARNING,
            ruff_db::diagnostic::Severity::Error => lsp_types::DiagnosticSeverity::ERROR,
            ruff_db::diagnostic::Severity::Fatal => lsp_types::DiagnosticSeverity::ERROR,
        };
        (severity, tags(diagnostic), None)
    };

    (
        cell,
        lsp_types::Diagnostic {
            range,
            severity: Some(severity),
            tags,
            code,
            code_description: diagnostic.documentation_url().and_then(|url| {
                Some(lsp_types::CodeDescription {
                    href: lsp_types::Url::parse(url).ok()?,
                })
            }),
            source: Some(DIAGNOSTIC_NAME.into()),
            message: body,
            related_information: None,
            data,
        },

Review comment (refactor):
Thank you. Hmm, this seems like an oversight to me from our diagnostic refactor. @ntBre could we always use `diagnostic.severity` here?

Code after:
        .flatten();

    let range: lsp_types::Range;
    let cell: usize;

    if let Some(notebook_index) = source_kind.as_ipy_notebook().map(Notebook::index) {
        NotebookRange { cell, range } = diagnostic_range.to_notebook_range(
            source_kind.source_code(),
            index,
            notebook_index,
            encoding,
        );
    } else {
        cell = usize::default();
        range = diagnostic_range.to_range(source_kind.source_code(), index, encoding);
    }

    let (severity, code) = if let Some(code) = code {
        (severity(code), code.to_string())
    } else {
        (
            match diagnostic.severity() {
                ruff_db::diagnostic::Severity::Info => lsp_types::DiagnosticSeverity::INFORMATION,
                ruff_db::diagnostic::Severity::Warning => lsp_types::DiagnosticSeverity::WARNING,
                ruff_db::diagnostic::Severity::Error => lsp_types::DiagnosticSeverity::ERROR,
                ruff_db::diagnostic::Severity::Fatal => lsp_types::DiagnosticSeverity::ERROR,
            },
            diagnostic.id().to_string(),
        )
    };

    (
        cell,
        lsp_types::Diagnostic {
            range,
            severity: Some(severity),
            tags: tags(diagnostic),
            code: Some(lsp_types::NumberOrString::String(code)),
            code_description: diagnostic.documentation_url().and_then(|url| {
                Some(lsp_types::CodeDescription {
                    href: lsp_types::Url::parse(url).ok()?,
                })
            }),
            source: Some(DIAGNOSTIC_NAME.into()),
            message: body,
            related_information: None,
            data,
        },
    )
}