Skip to content

code cleanup: if visit::walk_foo(a, b, c) discards b, then b should not be in its API. #14134

Closed
@pnkfelix

Description

@pnkfelix

The pattern for our syntax::visit module is that each visit method has a default implementation that just walks the substructure of that node in the AST.

What I often do when implementing a visitor is cut-and-paste the trait definition (with its default methods) into my new visitor and then consider each method to determine whether it should (1.) do extra-work, and/or (2.) stop the traversal, or (3.) just use the default impl.

When I see something like:

fn visit_struct_def(&mut self, s: &StructDef, i: Ident, g: &Generics, n: NodeId, e: E) {
    walk_struct_def(self, s, i, g, n, e)
}

I might assume: "Ah, the walk routine is already going to look at i and g, so if I need to look at those in my own code, then I can just inherit the visitor's default method."

The problem with that assumption is this:

pub fn walk_struct_def<E: Clone, V: Visitor<E>>(visitor: &mut V,
                                                struct_definition: &StructDef,
                                                _: Ident,
                                                _: &Generics,
                                                _: NodeId,
                                                env: E) {
   ...
}

My proposal solution: Any fn walk_foo in visit.rs should not have any parameters that are ignored.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions