Skip to content

feat: improve the lifetime of as_ast_kind methods #5506

@shulaoda

Description

@shulaoda

I tried to introduce new methods in PR #5491 , but encountered:

pub fn get_function_like_declaration<'b>(
    node: &AstNode<'b>,
    ctx: &LintContext<'b>,
) -> Option<&'b BindingIdentifier<'b>> {
    let parent = outermost_paren_parent(node, ctx)?;

    if let AstKind::VariableDeclarator(decl) = parent.kind() {
        let ident = decl.id.get_binding_identifier()?;
        Some(ident)
    } else {
        None
    }
}

to

pub fn get_function_like_declaration<'b>(
    node: &AstNode<'b>,
    ctx: &LintContext<'b>,
) -> Option<&'b BindingIdentifier<'b>> {
    let parent = outermost_paren_parent(node, ctx)?;
    let decl = parent.kind().as_variable_declarator()?;

    let ident = decl.id.get_binding_identifier()?;
    Some(ident)
}

error:

temporary value created hererustc[E0515](https://doc.rust-lang.org/error-index.html#E0515)
ast_util.rs(436, 5): original diagnostic
// size = 8, align = 0x8
let parent: &AstNode<'_>

When I changed the method to the following, there was no longer this problem.

pub fn as_variable_declarator(self) -> Option<&'a VariableDeclarator<'a>> {
    if let Self::VariableDeclarator(v) = self {
        Some(v)
    } else {
        None
    }
}

@IWANABETHATGUY cc

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory - New feature or request

    Type

    No type

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions