Repository: astral-sh/ruff
File: crates/ruff_linter/src/rules/pyupgrade/rules/os_error_alias.rs (Rust)

Code before:
fn atom_diagnostic(checker: &Checker, target: &Expr) {
    let mut diagnostic = checker.report_diagnostic(
        OSErrorAlias {
            name: UnqualifiedName::from_expr(target).map(|name| name.to_string()),
        },
        target.range(),
    );
    diagnostic.try_set_fix(|| {
        let (import_edit, binding) = checker.importer().get_or_import_builtin_symbol(
            "OSError",
            target.start(),
            checker.semantic(),
        )?;
        Ok(Fix::safe_edits(
            Edit::range_replacement(binding, target.range()),
            import_edit,
        ))
    });
}

/// Create a [`Diagnostic`] for a tuple of expressions.
fn tuple_diagnostic(checker: &Checker, tuple: &ast::ExprTuple, aliases: &[&Expr]) {
    let Some(first_alias) = aliases.first() else {
        return;
    };
    let diagnostic_range = if checker.settings().preview.is_enabled() {
        first_alias.range()
    } else {
        tuple.range()
    };
    let mut diagnostic = checker.report_diagnostic(OSErrorAlias { name: None }, diagnostic_range);
    if checker.settings().preview.is_enabled() {
        for alias in aliases.iter().skip(1) {
            diagnostic.secondary_annotation("Also an OSError alias".to_string(), alias.range());
        }
    }
    let semantic = checker.semantic();
    if semantic.has_builtin_binding("OSError") {
        // Filter out any `OSErrors` aliases.
        let mut remaining: Vec<Expr> = tuple
            .iter()
            .filter_map(|element| {
                if aliases.contains(&element) {
                    None
                } else {
                    Some(element.clone())
                }
            })
            .collect();

        // If `OSError` itself isn't already in the tuple, add it.

Review comment (refactor):
We prefer to add a new `is_<feature_name>_enabled()` helper to `ruff_linter/src/preview.rs` rather than simply checking if preview is enabled.

https://github.com/astral-sh/ruff/blob/main/crates/ruff_linter/src/preview.rs

Code after:
/// Create a [`Diagnostic`] for a single target, like an [`Expr::Name`].
fn atom_diagnostic(checker: &Checker, target: &Expr) {
    let mut diagnostic = checker.report_diagnostic(
        OSErrorAlias {
            name: UnqualifiedName::from_expr(target).map(|name| name.to_string()),
        },
        target.range(),
    );
    diagnostic.try_set_fix(|| {
        let (import_edit, binding) = checker.importer().get_or_import_builtin_symbol(
            "OSError",
            target.start(),
            checker.semantic(),
        )?;
        Ok(Fix::safe_edits(
            Edit::range_replacement(binding, target.range()),
            import_edit,
        ))
    });
}

/// Create a [`Diagnostic`] for a tuple of expressions.
fn tuple_diagnostic(checker: &Checker, tuple: &ast::ExprTuple, aliases: &[&Expr]) {
    let Some(first_alias) = aliases.first() else {
        return;
    };
    let diagnostic_range = if is_up024_precise_highlighting_enabled(checker.settings()) {
        first_alias.range()
    } else {
        tuple.range()
    };
    let mut diagnostic = checker.report_diagnostic(OSErrorAlias { name: None }, diagnostic_range);
    if is_up024_precise_highlighting_enabled(checker.settings()) {
        for alias in aliases.iter().skip(1) {
            diagnostic.secondary_annotation("", alias.range());
        }
    }
    let semantic = checker.semantic();
    if semantic.has_builtin_binding("OSError") {
        // Filter out any `OSErrors` aliases.
        let mut remaining: Vec<Expr> = tuple
            .iter()
            .filter_map(|element| {
                if aliases.contains(&element) {
                    None
                } else {
                    Some(element.clone())
                }
            })
            .collect();