Skip to content

Commit 6e21a28

Browse files
committed
jsondoclint: More precise Path checks
1 parent 24c751b commit 6e21a28

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

src/tools/jsondoclint/src/item_kind.rs

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ impl Kind {
114114
pub fn is_variant(self) -> bool {
115115
matches!(self, Kind::Variant)
116116
}
117+
pub fn is_trait(self) -> bool {
118+
matches!(self, Kind::Trait)
119+
}
120+
pub fn is_struct_enum_union(self) -> bool {
121+
matches!(self, Kind::Struct | Kind::Enum | Kind::Union)
122+
}
117123

118124
pub fn from_item(i: &Item) -> Self {
119125
use Kind::*;

src/tools/jsondoclint/src/validator.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ pub struct Validator<'a> {
3030
missing_ids: HashSet<&'a Id>,
3131
}
3232

33+
enum PathKind {
34+
Trait,
35+
StructEnumUnion,
36+
}
37+
3338
impl<'a> Validator<'a> {
3439
pub fn new(krate: &'a Crate) -> Self {
3540
Self {
@@ -165,7 +170,7 @@ impl<'a> Validator<'a> {
165170
fn check_impl(&mut self, x: &'a Impl) {
166171
self.check_generics(&x.generics);
167172
if let Some(path) = &x.trait_ {
168-
self.check_path(path); // TODO: Check is trait.
173+
self.check_path(path, PathKind::Trait);
169174
}
170175
self.check_type(&x.for_);
171176
x.items.iter().for_each(|i| self.add_trait_item_id(i));
@@ -211,7 +216,7 @@ impl<'a> Validator<'a> {
211216

212217
fn check_type(&mut self, x: &'a Type) {
213218
match x {
214-
Type::ResolvedPath(path) => self.check_path(path),
219+
Type::ResolvedPath(path) => self.check_path(path, PathKind::StructEnumUnion),
215220
Type::DynTrait(dyn_trait) => self.check_dyn_trait(dyn_trait),
216221
Type::Generic(_) => {}
217222
Type::Primitive(_) => {}
@@ -226,7 +231,7 @@ impl<'a> Validator<'a> {
226231
Type::QualifiedPath { name: _, args, self_type, trait_ } => {
227232
self.check_generic_args(&**args);
228233
self.check_type(&**self_type);
229-
self.check_path(trait_);
234+
self.check_path(trait_, PathKind::Trait);
230235
}
231236
}
232237
}
@@ -241,15 +246,18 @@ impl<'a> Validator<'a> {
241246
fn check_generic_bound(&mut self, x: &'a GenericBound) {
242247
match x {
243248
GenericBound::TraitBound { trait_, generic_params, modifier: _ } => {
244-
self.check_path(trait_);
249+
self.check_path(trait_, PathKind::Trait);
245250
generic_params.iter().for_each(|gpd| self.check_generic_param_def(gpd));
246251
}
247252
GenericBound::Outlives(_) => {}
248253
}
249254
}
250255

251-
fn check_path(&mut self, x: &'a Path) {
252-
self.add_id(&x.id); // TODO: What kinds are allowed here.
256+
fn check_path(&mut self, x: &'a Path, kind: PathKind) {
257+
match kind {
258+
PathKind::Trait => self.add_trait_id(&x.id),
259+
PathKind::StructEnumUnion => self.add_struct_enum_union_id(&x.id),
260+
}
253261
if let Some(args) = &x.args {
254262
self.check_generic_args(&**args);
255263
}
@@ -330,7 +338,7 @@ impl<'a> Validator<'a> {
330338

331339
fn check_dyn_trait(&mut self, dyn_trait: &'a DynTrait) {
332340
for pt in &dyn_trait.traits {
333-
self.check_path(&pt.trait_);
341+
self.check_path(&pt.trait_, PathKind::Trait);
334342
pt.generic_params.iter().for_each(|gpd| self.check_generic_param_def(gpd));
335343
}
336344
}
@@ -340,13 +348,6 @@ impl<'a> Validator<'a> {
340348
fp.generic_params.iter().for_each(|gpd| self.check_generic_param_def(gpd));
341349
}
342350

343-
// TODO: Remove
344-
fn add_id(&mut self, id: &'a Id) {
345-
if !self.seen_ids.contains(id) {
346-
self.todo.insert(id);
347-
}
348-
}
349-
350351
fn add_id_checked(&mut self, id: &'a Id, valid: fn(Kind) -> bool, expected: &str) {
351352
if let Some(kind) = self.kind_of(id) {
352353
if valid(kind) {
@@ -379,6 +380,14 @@ impl<'a> Validator<'a> {
379380
self.add_id_checked(id, Kind::is_variant, "Variant");
380381
}
381382

383+
fn add_trait_id(&mut self, id: &'a Id) {
384+
self.add_id_checked(id, Kind::is_trait, "Trait");
385+
}
386+
387+
fn add_struct_enum_union_id(&mut self, id: &'a Id) {
388+
self.add_id_checked(id, Kind::is_struct_enum_union, "Struct or Enum or Union");
389+
}
390+
382391
/// Add an Id that appeared in a trait
383392
fn add_trait_item_id(&mut self, id: &'a Id) {
384393
self.add_id_checked(id, Kind::can_appear_in_trait, "Trait inner item");

0 commit comments

Comments
 (0)