Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9bdac17

Browse files
committed
Simplify control flow in AstValidator::visit_item.
Currently some code paths return early, while others fall through to the `visit::walk_item` call, which is easy to overlook (I did, at first), even with the explanatory comments. This commit removes the early returns and moves the `visit::walk_item` calls up where necessary. This makes the function easier to read and slightly shorter.
1 parent 0b4a81a commit 9bdac17

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
863863
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl { of_trait: true });
864864
});
865865
walk_list!(self, visit_attribute, &item.attrs);
866-
return; // Avoid visiting again.
867866
}
868867
ItemKind::Impl(box Impl {
869868
safety,
@@ -915,7 +914,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
915914
walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl { of_trait: false });
916915
});
917916
walk_list!(self, visit_attribute, &item.attrs);
918-
return; // Avoid visiting again.
919917
}
920918
ItemKind::Fn(
921919
func @ box Fn {
@@ -960,7 +958,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
960958
let kind = FnKind::Fn(FnCtxt::Free, &item.vis, &*func);
961959
self.visit_fn(kind, item.span, item.id);
962960
walk_list!(self, visit_attribute, &item.attrs);
963-
return; // Avoid visiting again.
964961
}
965962
ItemKind::ForeignMod(ForeignMod { extern_span, abi, safety, .. }) => {
966963
self.with_in_extern_mod(*safety, |this| {
@@ -991,7 +988,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
991988
visit::walk_item(this, item);
992989
this.extern_mod = old_item;
993990
});
994-
return; // Avoid visiting again.
995991
}
996992
ItemKind::Enum(_, def, _) => {
997993
for variant in &def.variants {
@@ -1006,6 +1002,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10061002
);
10071003
}
10081004
}
1005+
visit::walk_item(self, item)
10091006
}
10101007
ItemKind::Trait(box Trait { is_auto, generics, ident, bounds, items, .. }) => {
10111008
let is_const_trait =
@@ -1033,7 +1030,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10331030
walk_list!(this, visit_assoc_item, items, AssocCtxt::Trait);
10341031
});
10351032
walk_list!(self, visit_attribute, &item.attrs);
1036-
return; // Avoid visiting again
10371033
}
10381034
ItemKind::Mod(safety, ident, mod_kind) => {
10391035
if let &Safety::Unsafe(span) = safety {
@@ -1045,6 +1041,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10451041
{
10461042
self.check_mod_file_item_asciionly(*ident);
10471043
}
1044+
visit::walk_item(self, item)
10481045
}
10491046
ItemKind::Struct(ident, vdata, generics) => match vdata {
10501047
VariantData::Struct { fields, .. } => {
@@ -1054,9 +1051,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10541051
// Permit `Anon{Struct,Union}` as field type.
10551052
walk_list!(self, visit_struct_field_def, fields);
10561053
walk_list!(self, visit_attribute, &item.attrs);
1057-
return;
10581054
}
1059-
_ => {}
1055+
_ => visit::walk_item(self, item),
10601056
},
10611057
ItemKind::Union(ident, vdata, generics) => {
10621058
if vdata.fields().is_empty() {
@@ -1070,9 +1066,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10701066
// Permit `Anon{Struct,Union}` as field type.
10711067
walk_list!(self, visit_struct_field_def, fields);
10721068
walk_list!(self, visit_attribute, &item.attrs);
1073-
return;
10741069
}
1075-
_ => {}
1070+
_ => visit::walk_item(self, item),
10761071
}
10771072
}
10781073
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
@@ -1083,6 +1078,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10831078
replace_span: self.ending_semi_or_hi(item.span),
10841079
});
10851080
}
1081+
visit::walk_item(self, item);
10861082
}
10871083
ItemKind::Static(box StaticItem { expr, safety, .. }) => {
10881084
self.check_item_safety(item.span, *safety);
@@ -1096,6 +1092,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10961092
replace_span: self.ending_semi_or_hi(item.span),
10971093
});
10981094
}
1095+
visit::walk_item(self, item);
10991096
}
11001097
ItemKind::TyAlias(
11011098
ty_alias @ box TyAlias { defaultness, bounds, where_clauses, ty, .. },
@@ -1119,11 +1116,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11191116
help: self.sess.is_nightly_build(),
11201117
});
11211118
}
1119+
visit::walk_item(self, item);
11221120
}
1123-
_ => {}
1121+
_ => visit::walk_item(self, item),
11241122
}
1125-
1126-
visit::walk_item(self, item);
11271123
}
11281124

11291125
fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {

0 commit comments

Comments
 (0)