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

Code before:
        self.pattern.is_match_symbol_name(name)
    }

    fn is_match_exact(&self, name: &str) -> bool {
        self.exact
            .as_ref()
            .map(|exact| &**exact == name)
            .unwrap_or(false)
    }
}

/// Context used to help filter completions when collecting them.
#[derive(Clone, Debug, Default)]
struct CollectionContext<'db> {
    /// A pre-computed type corresponding to what is
    /// expected in an exception expression.
    ///
    /// This is only `Some` when `is_raising_exception` is `true`.
    exception_ty: Option<Type<'db>>,
    /// Whether we're in an exception context (`raise` or `except`) or not.
    is_raising_exception: bool,
    /// Whether we're in a class definition context or not.
    is_in_class_def: bool,
    /// Names of base classes that are already specified in the class definition.
    /// Used to filter out duplicate base class suggestions.
    existing_class_bases: FxHashSet<Name>,
    /// When set, the context dictates that only *these* keywords
    /// are acceptable in this context.
    valid_keywords: Option<FxHashSet<&'static str>>,
}

impl<'db> CollectionContext<'db> {
    /// Return a collection context not derived from anything.
    ///
    /// This is useful when one wants to collect completions
    /// outside of any known or specific context. In general,
    /// the "none" context does no additional filtering.
    fn none() -> CollectionContext<'db> {
        CollectionContext::default()
    }

    /// Whether the completion that would be built from the
    /// given builder should be excluded based on this context.
    ///
    /// This only returns `true` when it is definitively known that the
    /// completion would never be valid for this context.
    fn exclude(&self, db: &dyn Db, builder: &CompletionBuilder<'_>) -> bool {
        if self.is_raising_exception && builder.is_notimplemented(db) {
            return true;
        }
        if builder.kind == Some(CompletionKind::Keyword)

Review comment (refactor):
I would probably have a single `existing_class_bases: Option<FxHashSet<Name>>` field, get rid of the `is_in_class_def` field, and add an `is_in_class_def` helper method that just checks whether `existing_class_bases` is `Some()` 

Code after:
        self.pattern.is_match_symbol_name(name)
    }

    fn is_match_exact(&self, name: &str) -> bool {
        self.exact
            .as_ref()
            .map(|exact| &**exact == name)
            .unwrap_or(false)
    }
}

/// Context used to help filter completions when collecting them.
#[derive(Clone, Debug, Default)]
struct CollectionContext<'db> {
    /// A pre-computed type corresponding to what is
    /// expected in an exception expression.
    ///
    /// This is only `Some` when `is_raising_exception` is `true`.
    exception_ty: Option<Type<'db>>,
    /// Whether we're in an exception context (`raise` or `except`) or not.
    is_raising_exception: bool,
    /// Names of base classes that are already specified in the class definition.
    /// Used to filter out duplicate base class suggestions.
    /// This is only `Some` when we're in a class definition context.
    existing_class_bases: Option<FxHashSet<Name>>,
    /// When set, the context dictates that only *these* keywords
    /// are acceptable in this context.
    valid_keywords: Option<FxHashSet<&'static str>>,
}

impl<'db> CollectionContext<'db> {
    /// Return a collection context not derived from anything.
    ///
    /// This is useful when one wants to collect completions
    /// outside of any known or specific context. In general,
    /// the "none" context does no additional filtering.
    fn none() -> CollectionContext<'db> {
        CollectionContext::default()
    }

    /// Whether we're in a class definition context.
    fn is_in_class_def(&self) -> bool {
        self.existing_class_bases.is_some()
    }

    /// Whether the completion that would be built from the
    /// given builder should be excluded based on this context.
    ///
    /// This only returns `true` when it is definitively known that the
    /// completion would never be valid for this context.
    fn exclude(&self, db: &dyn Db, builder: &CompletionBuilder<'_>) -> bool {