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

Code before:
/// Context for non-import completions.
#[derive(Debug)]
struct ContextNonImport<'m> {
    /// The AST of the completion target.
    target: CompletionTargetAst<'m>,
}

impl<'m> Context<'m> {
    /// Create a new context for finding completions.
    fn new(
        db: &'_ dyn Db,
        file: File,
        parsed: &'m ParsedModuleRef,
        source: &'m SourceText,
        offset: TextSize,
    ) -> Option<Context<'m>> {
        let cursor = ContextCursor::new(parsed, source, offset);
        if cursor.is_in_no_completions_place() {
            return None;
        }

        let kind = if let Some(import) = ImportStatement::detect(db, file, &cursor) {
            ContextKind::Import(import)
        } else {
            let target_token = CompletionTargetTokens::find(&cursor)?;
            let target = dbg!(target_token.ast(&cursor)?);
            ContextKind::NonImport(ContextNonImport { target })
        };

        Some(Context { kind, cursor })
    }

    /// Returns a filtering context for use with a completion collector.
    fn collection_context<'db>(&self, db: &'db dyn Db) -> CollectionContext<'db> {
        match self.kind {
            ContextKind::Import(_) => CollectionContext::none(),
            ContextKind::NonImport(_) => {
                let is_raising_exception = self.cursor.is_raising_exception();
                CollectionContext {
                    raisable_ty: is_raising_exception.then(|| {
                        UnionType::from_elements(
                            db,
                            [
                                KnownClass::BaseException.to_subclass_of(db),
                                KnownClass::BaseException.to_instance(db),
                            ],
                        )
                    }),
                    is_raising_exception,
                    valid_keywords: self.cursor.valid_keywords(),
                }

Review comment (suggestion):
```suggestion
            let target = target_token.ast(&cursor)?;
```

Code after:
#[derive(Debug)]
struct ContextNonImport<'m> {
    /// The AST of the completion target.
    target: CompletionTargetAst<'m>,
}

impl<'m> Context<'m> {
    /// Create a new context for finding completions.
    fn new(
        db: &'_ dyn Db,
        file: File,
        parsed: &'m ParsedModuleRef,
        source: &'m SourceText,
        offset: TextSize,
    ) -> Option<Context<'m>> {
        let cursor = ContextCursor::new(parsed, source, offset);
        if cursor.is_in_no_completions_place() {
            return None;
        }

        let kind = if let Some(import) = ImportStatement::detect(db, file, &cursor) {
            ContextKind::Import(import)
        } else {
            let target_token = CompletionTargetTokens::find(&cursor)?;
            let target = target_token.ast(&cursor)?;
            ContextKind::NonImport(ContextNonImport { target })
        };

        Some(Context { kind, cursor })
    }

    /// Returns a filtering context for use with a completion collector.
    fn collection_context<'db>(&self, db: &'db dyn Db) -> CollectionContext<'db> {
        match self.kind {
            ContextKind::Import(_) => CollectionContext::none(),
            ContextKind::NonImport(_) => {
                let is_raising_exception = self.cursor.is_raising_exception();
                CollectionContext {
                    raisable_ty: is_raising_exception.then(|| {
                        UnionType::from_elements(
                            db,
                            [
                                KnownClass::BaseException.to_subclass_of(db),
                                KnownClass::BaseException.to_instance(db),
                            ],
                        )
                    }),
                    is_raising_exception,
                    valid_keywords: self.cursor.valid_keywords(),
                }
            }