Skip to content

Commit

Permalink
Rollup merge of #108076 - GuillaumeGomez:more-let-chain, r=notriddle
Browse files Browse the repository at this point in the history
rustdoc: Use more let chain

Got the idea after yesterday's review.

r? `@notriddle`
  • Loading branch information
matthiaskrgr authored Feb 15, 2023
2 parents 8259755 + 86fd5a1 commit 09ab35b
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 251 deletions.
8 changes: 4 additions & 4 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 12 additions & 15 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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);
Expand Down
119 changes: 57 additions & 62 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
.into_iter()
.flatten()
.cloned()
.filter(|b| !b.is_sized_bound(cx)),
);

b.extend(
p.get_bounds()
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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 6 additions & 8 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
59 changes: 27 additions & 32 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -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 {
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();
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,
}
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 {
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 9 additions & 10 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 3 additions & 5 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 09ab35b

Please sign in to comment.