1
Fork 0

Add visitors for PatField and ExprField.

This helps simplify the code. It also fixes it to use the correct parent
when lowering. One consequence is the `non_snake_case` lint needed
to change the way it looked for parent nodes in a struct pattern.

This also includes a small fix to use the correct `Target` for
expression field attribute validation.
This commit is contained in:
Eric Huss 2022-05-22 18:34:37 -07:00
parent 6c7cb2bb77
commit dcd5177fd4
6 changed files with 55 additions and 73 deletions

View file

@ -761,26 +761,15 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'tcx> {
}
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
match e.kind {
hir::ExprKind::Struct(qpath, fields, base_expr) => {
self.with_lint_attrs(e.hir_id, |builder| {
builder.visit_qpath(qpath, e.hir_id, e.span);
for field in fields {
builder.with_lint_attrs(field.hir_id, |field_builder| {
field_builder.visit_id(field.hir_id);
field_builder.visit_ident(field.ident);
field_builder.visit_expr(field.expr);
});
}
if let Some(base_expr) = base_expr {
builder.visit_expr(base_expr);
}
});
}
_ => self.with_lint_attrs(e.hir_id, |builder| {
intravisit::walk_expr(builder, e);
}),
}
self.with_lint_attrs(e.hir_id, |builder| {
intravisit::walk_expr(builder, e);
})
}
fn visit_expr_field(&mut self, field: &'tcx hir::ExprField<'tcx>) {
self.with_lint_attrs(field.hir_id, |builder| {
intravisit::walk_expr_field(builder, field);
})
}
fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
@ -819,20 +808,10 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'tcx> {
});
}
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
match &p.kind {
hir::PatKind::Struct(qpath, fields, _) => {
self.visit_qpath(&qpath, p.hir_id, p.span);
for field in *fields {
self.with_lint_attrs(field.hir_id, |builder| {
builder.visit_id(field.hir_id);
builder.visit_ident(field.ident);
builder.visit_pat(field.pat);
})
}
}
_ => intravisit::walk_pat(self, p),
}
fn visit_pat_field(&mut self, field: &'tcx hir::PatField<'tcx>) {
self.with_lint_attrs(field.hir_id, |builder| {
intravisit::walk_pat_field(builder, field);
})
}
fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {

View file

@ -437,19 +437,14 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
if let PatKind::Binding(_, hid, ident, _) = p.kind {
if let hir::Node::Pat(parent_pat) = cx.tcx.hir().get(cx.tcx.hir().get_parent_node(hid))
if let hir::Node::PatField(field) = cx.tcx.hir().get(cx.tcx.hir().get_parent_node(hid))
{
if let PatKind::Struct(_, field_pats, _) = &parent_pat.kind {
if field_pats
.iter()
.any(|field| !field.is_shorthand && field.pat.hir_id == p.hir_id)
{
// Only check if a new name has been introduced, to avoid warning
// on both the struct definition and this pattern.
self.check_snake_case(cx, "variable", &ident);
}
return;
if !field.is_shorthand {
// Only check if a new name has been introduced, to avoid warning
// on both the struct definition and this pattern.
self.check_snake_case(cx, "variable", &ident);
}
return;
}
self.check_snake_case(cx, "variable", &ident);
}