Repository: astral-sh/ruff
File: crates/ty_ide/src/goto.rs (Rust)

Code before:
) -> Option<crate::NavigationTargets> {
    // When our target is a class constructor, we want to exclude
    // navigation targets to its `__init__` or `__new__` methods.
    //
    // ... unless the cursor is on the parenthesis of the call,
    // in which case we want to prefer the constructor methods
    // if available.
    //
    // See: https://github.com/astral-sh/ty/issues/2218
    if let GotoTarget::Call { on_parenthesis, .. } = *goto_target
        && goto_target
            .inferred_type(model)
            .is_some_and(|ty| ty.is_class_literal())
    {
        let is_constructor_method = |resolved_def: &ty_python_semantic::ResolvedDefinition<'_>| {
            let Some(def) = resolved_def.definition() else {
                return false;
            };
            if !matches!(*def.kind(model.db()), DefinitionKind::Function(_)) {
                return false;
            }
            def.name(model.db())
                .is_some_and(|name| name == "__init__" || name == "__new__")
        };

        if !on_parenthesis {
            definitions.retain(|resolved_def| !is_constructor_method(resolved_def));
        } else {
            // Only limit to constructor methods if we have at least one.
            // Otherwise we could end up removing all navigation targets.
            // e.g., See `goto_definition_dynamic_namedtuple_literal_parenthesis`
            // test.
            if definitions.iter().any(is_constructor_method) {
                definitions.retain(|resolved_def| is_constructor_method(resolved_def));
            }
        }
    }
    if let Some(mapper) = stub_mapper {
        definitions = mapper.map_definitions(definitions);
    }
    if definitions.is_empty() {
        None
    } else {
        let targets = convert_resolved_definitions_to_targets(model.db(), definitions);
        Some(crate::NavigationTargets::unique(targets))
    }
}

pub(crate) fn find_goto_target<'a>(
    model: &'a SemanticModel,
    parsed: &'a ParsedModuleRef,

Review comment (suggestion):
nit: slight preference towards not using negation and instead framing it as `if on_parenthesis { ... } else { ... }` 😅

Code after:
) -> Option<crate::NavigationTargets> {
    // When our target is a class constructor, we want to exclude
    // navigation targets to its `__init__` or `__new__` methods.
    //
    // ... unless the cursor is on the parenthesis of the call,
    // in which case we want to prefer the constructor methods
    // if available.
    //
    // See: https://github.com/astral-sh/ty/issues/2218
    if let GotoTarget::Call { on_parenthesis, .. } = *goto_target
        && goto_target
            .inferred_type(model)
            .is_some_and(|ty| ty.is_class_literal())
    {
        let is_constructor_method = |resolved_def: &ty_python_semantic::ResolvedDefinition<'_>| {
            let Some(def) = resolved_def.definition() else {
                return false;
            };
            if !matches!(*def.kind(model.db()), DefinitionKind::Function(_)) {
                return false;
            }
            def.name(model.db())
                .is_some_and(|name| name == "__init__" || name == "__new__")
        };

        if on_parenthesis {
            // Only limit to constructor methods if we have at least one.
            // Otherwise we could end up removing all navigation targets.
            // e.g., See `goto_definition_dynamic_namedtuple_literal_parenthesis`
            // test.
            if definitions.iter().any(is_constructor_method) {
                definitions.retain(|resolved_def| is_constructor_method(resolved_def));
            }
        } else {
            definitions.retain(|resolved_def| !is_constructor_method(resolved_def));
        }
    }
    if let Some(mapper) = stub_mapper {
        definitions = mapper.map_definitions(definitions);
    }
    if definitions.is_empty() {
        None
    } else {
        let targets = convert_resolved_definitions_to_targets(model.db(), definitions);
        Some(crate::NavigationTargets::unique(targets))
    }
}

pub(crate) fn find_goto_target<'a>(
    model: &'a SemanticModel,
    parsed: &'a ParsedModuleRef,