jsondoclint: simplify code using idiomatic Rust

- Use matches!() macro instead of match in item_kind.rs
- Remove unnecessary derefs and simplify Option mapping in validator.rs
- Replace clone() on Copy type with *id
- Fix doc comment indentation
- Remove redundant tuple parens in tests.rs
- Remove needless borrow and closure in main.rs
This commit is contained in:
AMMAAR-IC
2026-04-12 01:33:03 +05:30
parent 5f36a7f807
commit 31bd2c954a
4 changed files with 17 additions and 23 deletions
+1 -5
View File
@@ -77,11 +77,7 @@ pub fn can_appear_in_import(self) -> bool {
}
pub fn can_appear_in_glob_import(self) -> bool {
match self {
Kind::Module => true,
Kind::Enum => true,
_ => false,
}
matches!(self, Kind::Module | Kind::Enum)
}
pub fn can_appear_in_trait(self) -> bool {
+2 -2
View File
@@ -81,7 +81,7 @@ fn main() -> Result<()> {
[sel] => eprintln!(
"{} not in index or paths, but referred to at '{}'",
err.id.0,
json_find::to_jsonpath(&sel)
json_find::to_jsonpath(sel)
),
[sel, ..] => {
if verbose {
@@ -99,7 +99,7 @@ fn main() -> Result<()> {
eprintln!(
"{} not in index or paths, but referred to at '{}' and {} more",
err.id.0,
json_find::to_jsonpath(&sel),
json_find::to_jsonpath(sel),
sels.len() - 1,
)
}
+13 -15
View File
@@ -20,10 +20,10 @@
/// It is made of several parts.
///
/// - `check_*`: These take a type from [`rustdoc_json_types`], and check that
/// it is well formed. This involves calling `check_*` functions on
/// fields of that item, and `add_*` functions on [`Id`]s.
/// it is well formed. This involves calling `check_*` functions on
/// fields of that item, and `add_*` functions on [`Id`]s.
/// - `add_*`: These add an [`Id`] to the worklist, after validating it to check if
/// the `Id` is a kind expected in this situation.
/// the `Id` is a kind expected in this situation.
#[derive(Debug)]
pub struct Validator<'a> {
pub(crate) errs: Vec<Error>,
@@ -262,17 +262,17 @@ fn check_type(&mut self, x: &'a Type) {
Type::Generic(_) => {}
Type::Primitive(_) => {}
Type::Pat { type_, __pat_unstable_do_not_use: _ } => self.check_type(type_),
Type::FunctionPointer(fp) => self.check_function_pointer(&**fp),
Type::FunctionPointer(fp) => self.check_function_pointer(fp),
Type::Tuple(tys) => tys.iter().for_each(|ty| self.check_type(ty)),
Type::Slice(inner) => self.check_type(&**inner),
Type::Array { type_, len: _ } => self.check_type(&**type_),
Type::Slice(inner) => self.check_type(inner),
Type::Array { type_, len: _ } => self.check_type(type_),
Type::ImplTrait(bounds) => bounds.iter().for_each(|b| self.check_generic_bound(b)),
Type::Infer => {}
Type::RawPointer { is_mutable: _, type_ } => self.check_type(&**type_),
Type::BorrowedRef { lifetime: _, is_mutable: _, type_ } => self.check_type(&**type_),
Type::RawPointer { is_mutable: _, type_ } => self.check_type(type_),
Type::BorrowedRef { lifetime: _, is_mutable: _, type_ } => self.check_type(type_),
Type::QualifiedPath { name: _, args, self_type, trait_ } => {
self.check_opt_generic_args(&args);
self.check_type(&**self_type);
self.check_opt_generic_args(args);
self.check_type(self_type);
if let Some(trait_) = trait_ {
self.check_path(trait_, PathKind::Trait);
}
@@ -404,7 +404,7 @@ fn check_item_info(&mut self, id: &Id, item_info: &ItemSummary) {
// which encodes rustc implementation details.
if item_info.crate_id == LOCAL_CRATE_ID && !self.krate.index.contains_key(id) {
self.errs.push(Error {
id: id.clone(),
id: *id,
kind: ErrorKind::Custom(
"Id for local item in `paths` but not in `index`".to_owned(),
),
@@ -483,16 +483,14 @@ fn fail_expecting(&mut self, id: &Id, expected: &str) {
}
fn fail(&mut self, id: &Id, kind: ErrorKind) {
self.errs.push(Error { id: id.clone(), kind });
self.errs.push(Error { id: *id, kind });
}
fn kind_of(&mut self, id: &Id) -> Option<Kind> {
if let Some(item) = self.krate.index.get(id) {
Some(Kind::from_item(item))
} else if let Some(summary) = self.krate.paths.get(id) {
Some(Kind::from_summary(summary))
} else {
None
self.krate.paths.get(id).map(Kind::from_summary)
}
}
}
+1 -1
View File
@@ -78,7 +78,7 @@ fn errors_on_local_in_paths_and_not_index() {
span: None,
visibility: Visibility::Public,
docs: None,
links: FxHashMap::from_iter([(("prim@i32".to_owned(), Id(2)))]),
links: FxHashMap::from_iter([("prim@i32".to_owned(), Id(2))]),
attrs: Vec::new(),
deprecation: None,
inner: ItemEnum::Module(Module {