Description
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 fewcheck_*
methods forItemKind
,ExprKind
,StmtKind
kind where the user then selects the nodes they're interested in.