Skip to content

Commit c363092

Browse files
Rollup merge of rust-lang#155169 - AmmaarBakshi:jsondoclint-cleanup, r=camelid
jsondoclint: simplify code using idiomatic Rust Simplify `jsondoclint` with small idiomatic Rust cleanups. No behavior changes. - 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` Verified: - `cargo clippy -p jsondoclint --tests --quiet` → no warnings - `cargo test -p jsondoclint --quiet` → all 5 tests pass
2 parents bd2d904 + 31bd2c9 commit c363092

4 files changed

Lines changed: 17 additions & 23 deletions

File tree

src/tools/jsondoclint/src/item_kind.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ impl Kind {
7777
}
7878

7979
pub fn can_appear_in_glob_import(self) -> bool {
80-
match self {
81-
Kind::Module => true,
82-
Kind::Enum => true,
83-
_ => false,
84-
}
80+
matches!(self, Kind::Module | Kind::Enum)
8581
}
8682

8783
pub fn can_appear_in_trait(self) -> bool {

src/tools/jsondoclint/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn main() -> Result<()> {
8181
[sel] => eprintln!(
8282
"{} not in index or paths, but referred to at '{}'",
8383
err.id.0,
84-
json_find::to_jsonpath(&sel)
84+
json_find::to_jsonpath(sel)
8585
),
8686
[sel, ..] => {
8787
if verbose {
@@ -99,7 +99,7 @@ fn main() -> Result<()> {
9999
eprintln!(
100100
"{} not in index or paths, but referred to at '{}' and {} more",
101101
err.id.0,
102-
json_find::to_jsonpath(&sel),
102+
json_find::to_jsonpath(sel),
103103
sels.len() - 1,
104104
)
105105
}

src/tools/jsondoclint/src/validator.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ const LOCAL_CRATE_ID: u32 = 0;
2020
/// It is made of several parts.
2121
///
2222
/// - `check_*`: These take a type from [`rustdoc_json_types`], and check that
23-
/// it is well formed. This involves calling `check_*` functions on
24-
/// fields of that item, and `add_*` functions on [`Id`]s.
23+
/// it is well formed. This involves calling `check_*` functions on
24+
/// fields of that item, and `add_*` functions on [`Id`]s.
2525
/// - `add_*`: These add an [`Id`] to the worklist, after validating it to check if
26-
/// the `Id` is a kind expected in this situation.
26+
/// the `Id` is a kind expected in this situation.
2727
#[derive(Debug)]
2828
pub struct Validator<'a> {
2929
pub(crate) errs: Vec<Error>,
@@ -262,17 +262,17 @@ impl<'a> Validator<'a> {
262262
Type::Generic(_) => {}
263263
Type::Primitive(_) => {}
264264
Type::Pat { type_, __pat_unstable_do_not_use: _ } => self.check_type(type_),
265-
Type::FunctionPointer(fp) => self.check_function_pointer(&**fp),
265+
Type::FunctionPointer(fp) => self.check_function_pointer(fp),
266266
Type::Tuple(tys) => tys.iter().for_each(|ty| self.check_type(ty)),
267-
Type::Slice(inner) => self.check_type(&**inner),
268-
Type::Array { type_, len: _ } => self.check_type(&**type_),
267+
Type::Slice(inner) => self.check_type(inner),
268+
Type::Array { type_, len: _ } => self.check_type(type_),
269269
Type::ImplTrait(bounds) => bounds.iter().for_each(|b| self.check_generic_bound(b)),
270270
Type::Infer => {}
271-
Type::RawPointer { is_mutable: _, type_ } => self.check_type(&**type_),
272-
Type::BorrowedRef { lifetime: _, is_mutable: _, type_ } => self.check_type(&**type_),
271+
Type::RawPointer { is_mutable: _, type_ } => self.check_type(type_),
272+
Type::BorrowedRef { lifetime: _, is_mutable: _, type_ } => self.check_type(type_),
273273
Type::QualifiedPath { name: _, args, self_type, trait_ } => {
274-
self.check_opt_generic_args(&args);
275-
self.check_type(&**self_type);
274+
self.check_opt_generic_args(args);
275+
self.check_type(self_type);
276276
if let Some(trait_) = trait_ {
277277
self.check_path(trait_, PathKind::Trait);
278278
}
@@ -404,7 +404,7 @@ impl<'a> Validator<'a> {
404404
// which encodes rustc implementation details.
405405
if item_info.crate_id == LOCAL_CRATE_ID && !self.krate.index.contains_key(id) {
406406
self.errs.push(Error {
407-
id: id.clone(),
407+
id: *id,
408408
kind: ErrorKind::Custom(
409409
"Id for local item in `paths` but not in `index`".to_owned(),
410410
),
@@ -483,16 +483,14 @@ impl<'a> Validator<'a> {
483483
}
484484

485485
fn fail(&mut self, id: &Id, kind: ErrorKind) {
486-
self.errs.push(Error { id: id.clone(), kind });
486+
self.errs.push(Error { id: *id, kind });
487487
}
488488

489489
fn kind_of(&mut self, id: &Id) -> Option<Kind> {
490490
if let Some(item) = self.krate.index.get(id) {
491491
Some(Kind::from_item(item))
492-
} else if let Some(summary) = self.krate.paths.get(id) {
493-
Some(Kind::from_summary(summary))
494492
} else {
495-
None
493+
self.krate.paths.get(id).map(Kind::from_summary)
496494
}
497495
}
498496
}

src/tools/jsondoclint/src/validator/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn errors_on_local_in_paths_and_not_index() {
7878
span: None,
7979
visibility: Visibility::Public,
8080
docs: None,
81-
links: FxHashMap::from_iter([(("prim@i32".to_owned(), Id(2)))]),
81+
links: FxHashMap::from_iter([("prim@i32".to_owned(), Id(2))]),
8282
attrs: Vec::new(),
8383
deprecation: None,
8484
inner: ItemEnum::Module(Module {

0 commit comments

Comments
 (0)