Skip to content

LintPass trait API #32

Open
Open
@Niki4tap

Description

@Niki4tap

Current LintPass trait looks something like this:

pub trait LintPass<'ast> {
    fn check_item(
        &mut self,
        cx: &'ast AstContext<'ast>,
        item: ItemKind<'ast>);
    fn check_mod(
        &mut self,
        cx: &'ast AstContext<'ast>,
        item: &'ast ModItem<'ast>);
    fn check_extern_crate(
        &mut self,
        cx: &'ast AstContext<'ast>,
        item: &'ast ExternCrateItem<'ast>);
    fn check_use_decl(
        &mut self,
        cx: &'ast AstContext<'ast>,
        item: &'ast UseItem<'ast>);
    fn check_static_item(
        &mut self,
        cx: &'ast AstContext<'ast>,
        item: &'ast StaticItem<'ast>);
    fn check_const_item(
        &mut self,
        cx: &'ast AstContext<'ast>,
        item: &'ast ConstItem<'ast>);
}

The question is, should we extend its API with simpler functions like check_fn? If yes, for which items should we define them?

The difference in code is basically using a more generic function (check_item) and then checking if the item you are given is the one you want. With proposed helper functions:

impl<'ast> LintPass<'ast> for TestLintPass {
    fn check_fn(&mut self, cx: &'ast AstContext<'ast>, fn_item: &'ast FnItem<'ast>) {
        // do something with the function item
    }
}

Without:

impl<'ast> LintPass<'ast> for TestLintPass {
    fn check_item(&mut self, cx: &'ast AstContext<'ast>, item: &'ast ItemKind<'ast>) {
        if let ItemKind::Fn(fn_item) = item {
            // do something with the function item
        }
    }
}

As you can see, we already provide some of these functions (check_const_item, check_static_item), and rustc does too in its LateLintPass trait (check_fn, check_arm).

@xFrednet's comment from rust-marker/marker#75:

If I look at rustc's LateLintPass documentation, I find it a bit overwhelming and cluttered. Based on this, I think it could be good, to only provide very few check_* methods for ItemKind, ExprKind, StmtKind kind where the user then selects the nodes they're interested in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-stable-apiArea: Stable API, How it should look and what should be included in itC-discussion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions