diff options
| author | Eric Huss <eric@huss.org> | 2022-05-22 18:34:37 -0700 |
|---|---|---|
| committer | Eric Huss <eric@huss.org> | 2022-08-11 21:48:39 -0700 |
| commit | dcd5177fd43d5fdd9b89a7700fe3c283a9a52a48 (patch) | |
| tree | 2811f41f4860579fc830121cb7b9ac176f8e693f /compiler/rustc_lint | |
| parent | 6c7cb2bb770d43d1078bfbf3d14e7b4197b23900 (diff) | |
| download | rust-dcd5177fd43d5fdd9b89a7700fe3c283a9a52a48.tar.gz rust-dcd5177fd43d5fdd9b89a7700fe3c283a9a52a48.zip | |
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.
Diffstat (limited to 'compiler/rustc_lint')
| -rw-r--r-- | compiler/rustc_lint/src/levels.rs | 47 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/nonstandard_style.rs | 17 |
2 files changed, 19 insertions, 45 deletions
diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index bae36647992..1cabb58bbeb 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -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>) { diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index 8d04d68bf1c..2868aabfa07 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -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); } |
