Rollup merge of #108076 - GuillaumeGomez:more-let-chain, r=notriddle

rustdoc: Use more let chain

Got the idea after yesterday's review.

r? `@notriddle`
This commit is contained in:
Matthias Krüger 2023-02-15 21:30:59 +01:00 committed by GitHub
commit 09ab35b574
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 223 additions and 254 deletions

View File

@ -164,10 +164,10 @@ impl Cfg {
/// Renders the configuration for human display, as a short HTML description.
pub(crate) fn render_short_html(&self) -> String {
let mut msg = Display(self, Format::ShortHtml).to_string();
if self.should_capitalize_first_letter() {
if let Some(i) = msg.find(|c: char| c.is_ascii_alphanumeric()) {
msg[i..i + 1].make_ascii_uppercase();
}
if self.should_capitalize_first_letter() &&
let Some(i) = msg.find(|c: char| c.is_ascii_alphanumeric())
{
msg[i..i + 1].make_ascii_uppercase();
}
msg
}

View File

@ -390,18 +390,17 @@ pub(crate) fn build_impl(
// Only inline impl if the implemented trait is
// reachable in rustdoc generated documentation
if !did.is_local() {
if let Some(traitref) = associated_trait {
let did = traitref.def_id;
if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
return;
}
if !did.is_local() && let Some(traitref) = associated_trait {
let did = traitref.def_id;
if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
return;
}
if let Some(stab) = tcx.lookup_stability(did) {
if stab.is_unstable() && stab.feature == sym::rustc_private {
return;
}
}
if let Some(stab) = tcx.lookup_stability(did) &&
stab.is_unstable() &&
stab.feature == sym::rustc_private
{
return;
}
}
@ -525,10 +524,8 @@ pub(crate) fn build_impl(
}
while let Some(ty) = stack.pop() {
if let Some(did) = ty.def_id(&cx.cache) {
if tcx.is_doc_hidden(did) {
return;
}
if let Some(did) = ty.def_id(&cx.cache) && tcx.is_doc_hidden(did) {
return;
}
if let Some(generics) = ty.generics() {
stack.extend(generics);

View File

@ -787,43 +787,43 @@ fn clean_ty_generics<'tcx>(
None
})();
if let Some(param_idx) = param_idx {
if let Some(b) = impl_trait.get_mut(&param_idx.into()) {
let p: WherePredicate = clean_predicate(*p, cx)?;
if let Some(param_idx) = param_idx
&& let Some(b) = impl_trait.get_mut(&param_idx.into())
{
let p: WherePredicate = clean_predicate(*p, cx)?;
b.extend(
p.get_bounds()
b.extend(
p.get_bounds()
.into_iter()
.flatten()
.cloned()
.filter(|b| !b.is_sized_bound(cx)),
);
let proj = projection.map(|p| {
(
clean_projection(p.map_bound(|p| p.projection_ty), cx, None),
p.map_bound(|p| p.term),
)
});
if let Some(((_, trait_did, name), rhs)) = proj
.as_ref()
.and_then(|(lhs, rhs): &(Type, _)| Some((lhs.projection()?, rhs)))
{
// FIXME(...): Remove this unwrap()
impl_trait_proj.entry(param_idx).or_default().push((
trait_did,
name,
rhs.map_bound(|rhs| rhs.ty().unwrap()),
p.get_bound_params()
.into_iter()
.flatten()
.cloned()
.filter(|b| !b.is_sized_bound(cx)),
);
let proj = projection.map(|p| {
(
clean_projection(p.map_bound(|p| p.projection_ty), cx, None),
p.map_bound(|p| p.term),
)
});
if let Some(((_, trait_did, name), rhs)) = proj
.as_ref()
.and_then(|(lhs, rhs): &(Type, _)| Some((lhs.projection()?, rhs)))
{
// FIXME(...): Remove this unwrap()
impl_trait_proj.entry(param_idx).or_default().push((
trait_did,
name,
rhs.map_bound(|rhs| rhs.ty().unwrap()),
p.get_bound_params()
.into_iter()
.flatten()
.map(|param| GenericParamDef::lifetime(param.0))
.collect(),
));
}
return None;
.map(|param| GenericParamDef::lifetime(param.0))
.collect(),
));
}
return None;
}
Some(p)
@ -886,7 +886,7 @@ fn clean_ty_generics<'tcx>(
// `?Sized` bound for each one we didn't find to be `Sized`.
for tp in &stripped_params {
if let types::GenericParamDefKind::Type { .. } = tp.kind
&& !sized_params.contains(&tp.name)
&& !sized_params.contains(&tp.name)
{
where_predicates.push(WherePredicate::BoundPredicate {
ty: Type::Generic(tp.name),
@ -1461,10 +1461,10 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
// Try to normalize `<X as Y>::T` to a type
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
// `hir_to_ty` can return projection types with escaping vars for GATs, e.g. `<() as Trait>::Gat<'_>`
if !ty.has_escaping_bound_vars() {
if let Some(normalized_value) = normalize(cx, ty::Binder::dummy(ty)) {
return clean_middle_ty(normalized_value, cx, None);
}
if !ty.has_escaping_bound_vars()
&& let Some(normalized_value) = normalize(cx, ty::Binder::dummy(ty))
{
return clean_middle_ty(normalized_value, cx, None);
}
let trait_segments = &p.segments[..p.segments.len() - 1];
@ -1878,11 +1878,9 @@ fn clean_middle_opaque_bounds<'tcx>(
_ => return None,
};
if let Some(sized) = cx.tcx.lang_items().sized_trait() {
if trait_ref.def_id() == sized {
has_sized = true;
return None;
}
if let Some(sized) = cx.tcx.lang_items().sized_trait() && trait_ref.def_id() == sized {
has_sized = true;
return None;
}
let bindings: ThinVec<_> = bounds
@ -2392,17 +2390,15 @@ fn clean_use_statement_inner<'tcx>(
let is_visible_from_parent_mod =
visibility.is_accessible_from(parent_mod, cx.tcx) && !current_mod.is_top_level_module();
if pub_underscore {
if let Some(ref inline) = inline_attr {
rustc_errors::struct_span_err!(
cx.tcx.sess,
inline.span(),
E0780,
"anonymous imports cannot be inlined"
)
.span_label(import.span, "anonymous import")
.emit();
}
if pub_underscore && let Some(ref inline) = inline_attr {
rustc_errors::struct_span_err!(
cx.tcx.sess,
inline.span(),
E0780,
"anonymous imports cannot be inlined"
)
.span_label(import.span, "anonymous import")
.emit();
}
// We consider inlining the documentation of `pub use` statements, but we
@ -2438,14 +2434,13 @@ fn clean_use_statement_inner<'tcx>(
}
Import::new_glob(resolve_use_source(cx, path), true)
} else {
if inline_attr.is_none() {
if let Res::Def(DefKind::Mod, did) = path.res {
if !did.is_local() && did.is_crate_root() {
// if we're `pub use`ing an extern crate root, don't inline it unless we
// were specifically asked for it
denied = true;
}
}
if inline_attr.is_none()
&& let Res::Def(DefKind::Mod, did) = path.res
&& !did.is_local() && did.is_crate_root()
{
// if we're `pub use`ing an extern crate root, don't inline it unless we
// were specifically asked for it
denied = true;
}
if !denied {
let mut visited = DefIdSet::default();

View File

@ -182,10 +182,8 @@ impl ExternalCrate {
return Local;
}
if extern_url_takes_precedence {
if let Some(url) = extern_url {
return to_remote(url);
}
if extern_url_takes_precedence && let Some(url) = extern_url {
return to_remote(url);
}
// Failing that, see if there's an attribute specifying where to find this
@ -1176,10 +1174,10 @@ impl GenericBound {
pub(crate) fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool {
use rustc_hir::TraitBoundModifier as TBM;
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
if Some(trait_.def_id()) == cx.tcx.lang_items().sized_trait() {
return true;
}
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self &&
Some(trait_.def_id()) == cx.tcx.lang_items().sized_trait()
{
return true;
}
false
}

View File

@ -345,10 +345,10 @@ pub(crate) fn is_literal_expr(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
return true;
}
if let hir::ExprKind::Unary(hir::UnOp::Neg, expr) = &expr.kind {
if let hir::ExprKind::Lit(_) = &expr.kind {
return true;
}
if let hir::ExprKind::Unary(hir::UnOp::Neg, expr) = &expr.kind &&
let hir::ExprKind::Lit(_) = &expr.kind
{
return true;
}
}

View File

@ -229,11 +229,11 @@ fn scrape_test_config(attrs: &[ast::Attribute]) -> GlobalTestOptions {
if attr.has_name(sym::no_crate_inject) {
opts.no_crate_inject = true;
}
if attr.has_name(sym::attr) {
if let Some(l) = attr.meta_item_list() {
for item in l {
opts.attrs.push(pprust::meta_list_item_to_string(item));
}
if attr.has_name(sym::attr)
&& let Some(l) = attr.meta_item_list()
{
for item in l {
opts.attrs.push(pprust::meta_list_item_to_string(item));
}
}
}
@ -594,31 +594,28 @@ pub(crate) fn make_test(
loop {
match parser.parse_item(ForceCollect::No) {
Ok(Some(item)) => {
if !found_main {
if let ast::ItemKind::Fn(..) = item.kind {
if item.ident.name == sym::main {
found_main = true;
}
if !found_main &&
let ast::ItemKind::Fn(..) = item.kind &&
item.ident.name == sym::main
{
found_main = true;
}
if !found_extern_crate &&
let ast::ItemKind::ExternCrate(original) = item.kind
{
// This code will never be reached if `crate_name` is none because
// `found_extern_crate` is initialized to `true` if it is none.
let crate_name = crate_name.unwrap();
match original {
Some(name) => found_extern_crate = name.as_str() == crate_name,
None => found_extern_crate = item.ident.as_str() == crate_name,
}
}
if !found_extern_crate {
if let ast::ItemKind::ExternCrate(original) = item.kind {
// This code will never be reached if `crate_name` is none because
// `found_extern_crate` is initialized to `true` if it is none.
let crate_name = crate_name.unwrap();
match original {
Some(name) => found_extern_crate = name.as_str() == crate_name,
None => found_extern_crate = item.ident.as_str() == crate_name,
}
}
}
if !found_macro {
if let ast::ItemKind::MacCall(..) = item.kind {
found_macro = true;
}
if !found_macro && let ast::ItemKind::MacCall(..) = item.kind {
found_macro = true;
}
if found_main && found_extern_crate {
@ -972,14 +969,12 @@ impl Collector {
fn get_filename(&self) -> FileName {
if let Some(ref source_map) = self.source_map {
let filename = source_map.span_to_filename(self.position);
if let FileName::Real(ref filename) = filename {
if let Ok(cur_dir) = env::current_dir() {
if let Some(local_path) = filename.local_path() {
if let Ok(path) = local_path.strip_prefix(&cur_dir) {
return path.to_owned().into();
}
}
}
if let FileName::Real(ref filename) = filename &&
let Ok(cur_dir) = env::current_dir() &&
let Some(local_path) = filename.local_path() &&
let Ok(path) = local_path.strip_prefix(&cur_dir)
{
return path.to_owned().into();
}
filename
} else if let Some(ref filename) = self.filename {

View File

@ -229,16 +229,15 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
}
// Collect all the implementors of traits.
if let clean::ImplItem(ref i) = *item.kind {
if let Some(trait_) = &i.trait_ {
if !i.kind.is_blanket() {
self.cache
.implementors
.entry(trait_.def_id())
.or_default()
.push(Impl { impl_item: item.clone() });
}
}
if let clean::ImplItem(ref i) = *item.kind &&
let Some(trait_) = &i.trait_ &&
!i.kind.is_blanket()
{
self.cache
.implementors
.entry(trait_.def_id())
.or_default()
.push(Impl { impl_item: item.clone() });
}
// Index this method for searching later on.

View File

@ -710,11 +710,9 @@ pub(crate) fn href_with_root_path(
}
}
};
if !is_remote {
if let Some(root_path) = root_path {
let root = root_path.trim_end_matches('/');
url_parts.push_front(root);
}
if !is_remote && let Some(root_path) = root_path {
let root = root_path.trim_end_matches('/');
url_parts.push_front(root);
}
debug!(?url_parts);
match shortty {

View File

@ -466,10 +466,8 @@ impl<'a> PeekIter<'a> {
}
/// Returns the next item after the current one. It doesn't interfere with `peek_next` output.
fn peek(&mut self) -> Option<&(TokenKind, &'a str)> {
if self.stored.is_empty() {
if let Some(next) = self.iter.next() {
self.stored.push_back(next);
}
if self.stored.is_empty() && let Some(next) = self.iter.next() {
self.stored.push_back(next);
}
self.stored.front()
}

View File

@ -705,14 +705,12 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
shared.fs.write(scrape_examples_help_file, v)?;
}
if let Some(ref redirections) = shared.redirections {
if !redirections.borrow().is_empty() {
let redirect_map_path =
self.dst.join(crate_name.as_str()).join("redirect-map.json");
let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
shared.ensure_dir(&self.dst.join(crate_name.as_str()))?;
shared.fs.write(redirect_map_path, paths)?;
}
if let Some(ref redirections) = shared.redirections && !redirections.borrow().is_empty() {
let redirect_map_path =
self.dst.join(crate_name.as_str()).join("redirect-map.json");
let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
shared.ensure_dir(&self.dst.join(crate_name.as_str()))?;
shared.fs.write(redirect_map_path, paths)?;
}
// No need for it anymore.

View File

@ -2225,14 +2225,13 @@ fn sidebar_deref_methods(
})
{
debug!("found target, real_target: {:?} {:?}", target, real_target);
if let Some(did) = target.def_id(c) {
if let Some(type_did) = impl_.inner_impl().for_.def_id(c) {
// `impl Deref<Target = S> for S`
if did == type_did || !derefs.insert(did) {
// Avoid infinite cycles
return;
}
}
if let Some(did) = target.def_id(c) &&
let Some(type_did) = impl_.inner_impl().for_.def_id(c) &&
// `impl Deref<Target = S> for S`
(did == type_did || !derefs.insert(did))
{
// Avoid infinite cycles
return;
}
let deref_mut = v.iter().any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait());
let inner_impl = target
@ -2266,25 +2265,24 @@ fn sidebar_deref_methods(
}
// Recurse into any further impls that might exist for `target`
if let Some(target_did) = target.def_id(c) {
if let Some(target_impls) = c.impls.get(&target_did) {
if let Some(target_deref_impl) = target_impls.iter().find(|i| {
i.inner_impl()
.trait_
.as_ref()
.map(|t| Some(t.def_id()) == cx.tcx().lang_items().deref_trait())
.unwrap_or(false)
}) {
sidebar_deref_methods(
cx,
out,
target_deref_impl,
target_impls,
derefs,
used_links,
);
}
}
if let Some(target_did) = target.def_id(c) &&
let Some(target_impls) = c.impls.get(&target_did) &&
let Some(target_deref_impl) = target_impls.iter().find(|i| {
i.inner_impl()
.trait_
.as_ref()
.map(|t| Some(t.def_id()) == cx.tcx().lang_items().deref_trait())
.unwrap_or(false)
})
{
sidebar_deref_methods(
cx,
out,
target_deref_impl,
target_impls,
derefs,
used_links,
);
}
}
}

View File

@ -80,12 +80,11 @@ impl<'tcx> JsonRenderer<'tcx> {
// document primitive items in an arbitrary crate by using
// `doc(primitive)`.
let mut is_primitive_impl = false;
if let clean::types::ItemKind::ImplItem(ref impl_) = *item.kind {
if impl_.trait_.is_none() {
if let clean::types::Type::Primitive(_) = impl_.for_ {
is_primitive_impl = true;
}
}
if let clean::types::ItemKind::ImplItem(ref impl_) = *item.kind &&
impl_.trait_.is_none() &&
let clean::types::Type::Primitive(_) = impl_.for_
{
is_primitive_impl = true;
}
if item.item_id.is_local() || is_primitive_impl {

View File

@ -82,18 +82,17 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
let def_id = item.item_id.expect_def_id().expect_local();
// check if parent is trait impl
if let Some(parent_def_id) = cx.tcx.opt_local_parent(def_id) {
if let Some(parent_node) = cx.tcx.hir().find_by_def_id(parent_def_id) {
if matches!(
parent_node,
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }),
..
})
) {
return false;
}
}
if let Some(parent_def_id) = cx.tcx.opt_local_parent(def_id) &&
let Some(parent_node) = cx.tcx.hir().find_by_def_id(parent_def_id) &&
matches!(
parent_node,
hir::Node::Item(hir::Item {
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }),
..
})
)
{
return false;
}
if cx.tcx.is_doc_hidden(def_id.to_def_id())

View File

@ -156,39 +156,38 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
// scan through included items ahead of time to splice in Deref targets to the "valid" sets
for it in new_items_external.iter().chain(new_items_local.iter()) {
if let ImplItem(box Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
if trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait()
&& cleaner.keep_impl(for_, true)
{
let target = items
.iter()
.find_map(|item| match *item.kind {
AssocTypeItem(ref t, _) => Some(&t.type_),
_ => None,
})
.expect("Deref impl without Target type");
if let ImplItem(box Impl { ref for_, ref trait_, ref items, .. }) = *it.kind &&
trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait() &&
cleaner.keep_impl(for_, true)
{
let target = items
.iter()
.find_map(|item| match *item.kind {
AssocTypeItem(ref t, _) => Some(&t.type_),
_ => None,
})
.expect("Deref impl without Target type");
if let Some(prim) = target.primitive_type() {
cleaner.prims.insert(prim);
} else if let Some(did) = target.def_id(&cx.cache) {
cleaner.items.insert(did.into());
}
if let Some(for_did) = for_.def_id(&cx.cache) {
if type_did_to_deref_target.insert(for_did, target).is_none() {
// Since only the `DefId` portion of the `Type` instances is known to be same for both the
// `Deref` target type and the impl for type positions, this map of types is keyed by
// `DefId` and for convenience uses a special cleaner that accepts `DefId`s directly.
if cleaner.keep_impl_with_def_id(for_did.into()) {
let mut targets = DefIdSet::default();
targets.insert(for_did);
add_deref_target(
cx,
&type_did_to_deref_target,
&mut cleaner,
&mut targets,
for_did,
);
}
if let Some(prim) = target.primitive_type() {
cleaner.prims.insert(prim);
} else if let Some(did) = target.def_id(&cx.cache) {
cleaner.items.insert(did.into());
}
if let Some(for_did) = for_.def_id(&cx.cache) {
if type_did_to_deref_target.insert(for_did, target).is_none() {
// Since only the `DefId` portion of the `Type` instances is known to be same for both the
// `Deref` target type and the impl for type positions, this map of types is keyed by
// `DefId` and for convenience uses a special cleaner that accepts `DefId`s directly.
if cleaner.keep_impl_with_def_id(for_did.into()) {
let mut targets = DefIdSet::default();
targets.insert(for_did);
add_deref_target(
cx,
&type_did_to_deref_target,
&mut cleaner,
&mut targets,
for_did,
);
}
}
}

View File

@ -210,11 +210,9 @@ fn extract_path_backwards(text: &str, end_pos: usize) -> Option<usize> {
.take_while(|(_, c)| is_id_start(*c) || is_id_continue(*c))
.reduce(|_accum, item| item)
.and_then(|(new_pos, c)| is_id_start(c).then_some(new_pos));
if let Some(new_pos) = new_pos {
if current_pos != new_pos {
current_pos = new_pos;
continue;
}
if let Some(new_pos) = new_pos && current_pos != new_pos {
current_pos = new_pos;
continue;
}
break;
}

View File

@ -201,27 +201,25 @@ impl<'a> DocFolder for ImplStripper<'a, '_> {
// Because we don't inline in `maybe_inline_local` if the output format is JSON,
// we need to make a special check for JSON output: we want to keep it unless it has
// a `#[doc(hidden)]` attribute if the `for_` type is exported.
if let Some(did) = imp.for_.def_id(self.cache) {
if !imp.for_.is_assoc_ty() && !self.should_keep_impl(&i, did) {
debug!("ImplStripper: impl item for stripped type; removing");
return None;
}
if let Some(did) = imp.for_.def_id(self.cache) &&
!imp.for_.is_assoc_ty() && !self.should_keep_impl(&i, did)
{
debug!("ImplStripper: impl item for stripped type; removing");
return None;
}
if let Some(did) = imp.trait_.as_ref().map(|t| t.def_id()) {
if !self.should_keep_impl(&i, did) {
debug!("ImplStripper: impl item for stripped trait; removing");
return None;
}
if let Some(did) = imp.trait_.as_ref().map(|t| t.def_id()) &&
!self.should_keep_impl(&i, did) {
debug!("ImplStripper: impl item for stripped trait; removing");
return None;
}
if let Some(generics) = imp.trait_.as_ref().and_then(|t| t.generics()) {
for typaram in generics {
if let Some(did) = typaram.def_id(self.cache) {
if !self.should_keep_impl(&i, did) {
debug!(
"ImplStripper: stripped item in trait's generics; removing impl"
);
return None;
}
if let Some(did) = typaram.def_id(self.cache) && !self.should_keep_impl(&i, did)
{
debug!(
"ImplStripper: stripped item in trait's generics; removing impl"
);
return None;
}
}
}