Skip to content

Commit 09ab35b

Browse files
authored
Rollup merge of #108076 - GuillaumeGomez:more-let-chain, r=notriddle
rustdoc: Use more let chain Got the idea after yesterday's review. r? `@notriddle`
2 parents 8259755 + 86fd5a1 commit 09ab35b

16 files changed

+220
-251
lines changed

src/librustdoc/clean/cfg.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ impl Cfg {
164164
/// Renders the configuration for human display, as a short HTML description.
165165
pub(crate) fn render_short_html(&self) -> String {
166166
let mut msg = Display(self, Format::ShortHtml).to_string();
167-
if self.should_capitalize_first_letter() {
168-
if let Some(i) = msg.find(|c: char| c.is_ascii_alphanumeric()) {
169-
msg[i..i + 1].make_ascii_uppercase();
170-
}
167+
if self.should_capitalize_first_letter() &&
168+
let Some(i) = msg.find(|c: char| c.is_ascii_alphanumeric())
169+
{
170+
msg[i..i + 1].make_ascii_uppercase();
171171
}
172172
msg
173173
}

src/librustdoc/clean/inline.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,17 @@ pub(crate) fn build_impl(
390390

391391
// Only inline impl if the implemented trait is
392392
// reachable in rustdoc generated documentation
393-
if !did.is_local() {
394-
if let Some(traitref) = associated_trait {
395-
let did = traitref.def_id;
396-
if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
397-
return;
398-
}
393+
if !did.is_local() && let Some(traitref) = associated_trait {
394+
let did = traitref.def_id;
395+
if !cx.cache.effective_visibilities.is_directly_public(tcx, did) {
396+
return;
397+
}
399398

400-
if let Some(stab) = tcx.lookup_stability(did) {
401-
if stab.is_unstable() && stab.feature == sym::rustc_private {
402-
return;
403-
}
404-
}
399+
if let Some(stab) = tcx.lookup_stability(did) &&
400+
stab.is_unstable() &&
401+
stab.feature == sym::rustc_private
402+
{
403+
return;
405404
}
406405
}
407406

@@ -525,10 +524,8 @@ pub(crate) fn build_impl(
525524
}
526525

527526
while let Some(ty) = stack.pop() {
528-
if let Some(did) = ty.def_id(&cx.cache) {
529-
if tcx.is_doc_hidden(did) {
530-
return;
531-
}
527+
if let Some(did) = ty.def_id(&cx.cache) && tcx.is_doc_hidden(did) {
528+
return;
532529
}
533530
if let Some(generics) = ty.generics() {
534531
stack.extend(generics);

src/librustdoc/clean/mod.rs

+57-62
Original file line numberDiff line numberDiff line change
@@ -787,43 +787,43 @@ fn clean_ty_generics<'tcx>(
787787
None
788788
})();
789789

790-
if let Some(param_idx) = param_idx {
791-
if let Some(b) = impl_trait.get_mut(&param_idx.into()) {
792-
let p: WherePredicate = clean_predicate(*p, cx)?;
790+
if let Some(param_idx) = param_idx
791+
&& let Some(b) = impl_trait.get_mut(&param_idx.into())
792+
{
793+
let p: WherePredicate = clean_predicate(*p, cx)?;
794+
795+
b.extend(
796+
p.get_bounds()
797+
.into_iter()
798+
.flatten()
799+
.cloned()
800+
.filter(|b| !b.is_sized_bound(cx)),
801+
);
793802

794-
b.extend(
795-
p.get_bounds()
803+
let proj = projection.map(|p| {
804+
(
805+
clean_projection(p.map_bound(|p| p.projection_ty), cx, None),
806+
p.map_bound(|p| p.term),
807+
)
808+
});
809+
if let Some(((_, trait_did, name), rhs)) = proj
810+
.as_ref()
811+
.and_then(|(lhs, rhs): &(Type, _)| Some((lhs.projection()?, rhs)))
812+
{
813+
// FIXME(...): Remove this unwrap()
814+
impl_trait_proj.entry(param_idx).or_default().push((
815+
trait_did,
816+
name,
817+
rhs.map_bound(|rhs| rhs.ty().unwrap()),
818+
p.get_bound_params()
796819
.into_iter()
797820
.flatten()
798-
.cloned()
799-
.filter(|b| !b.is_sized_bound(cx)),
800-
);
801-
802-
let proj = projection.map(|p| {
803-
(
804-
clean_projection(p.map_bound(|p| p.projection_ty), cx, None),
805-
p.map_bound(|p| p.term),
806-
)
807-
});
808-
if let Some(((_, trait_did, name), rhs)) = proj
809-
.as_ref()
810-
.and_then(|(lhs, rhs): &(Type, _)| Some((lhs.projection()?, rhs)))
811-
{
812-
// FIXME(...): Remove this unwrap()
813-
impl_trait_proj.entry(param_idx).or_default().push((
814-
trait_did,
815-
name,
816-
rhs.map_bound(|rhs| rhs.ty().unwrap()),
817-
p.get_bound_params()
818-
.into_iter()
819-
.flatten()
820-
.map(|param| GenericParamDef::lifetime(param.0))
821-
.collect(),
822-
));
823-
}
824-
825-
return None;
821+
.map(|param| GenericParamDef::lifetime(param.0))
822+
.collect(),
823+
));
826824
}
825+
826+
return None;
827827
}
828828

829829
Some(p)
@@ -886,7 +886,7 @@ fn clean_ty_generics<'tcx>(
886886
// `?Sized` bound for each one we didn't find to be `Sized`.
887887
for tp in &stripped_params {
888888
if let types::GenericParamDefKind::Type { .. } = tp.kind
889-
&& !sized_params.contains(&tp.name)
889+
&& !sized_params.contains(&tp.name)
890890
{
891891
where_predicates.push(WherePredicate::BoundPredicate {
892892
ty: Type::Generic(tp.name),
@@ -1461,10 +1461,10 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
14611461
// Try to normalize `<X as Y>::T` to a type
14621462
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
14631463
// `hir_to_ty` can return projection types with escaping vars for GATs, e.g. `<() as Trait>::Gat<'_>`
1464-
if !ty.has_escaping_bound_vars() {
1465-
if let Some(normalized_value) = normalize(cx, ty::Binder::dummy(ty)) {
1466-
return clean_middle_ty(normalized_value, cx, None);
1467-
}
1464+
if !ty.has_escaping_bound_vars()
1465+
&& let Some(normalized_value) = normalize(cx, ty::Binder::dummy(ty))
1466+
{
1467+
return clean_middle_ty(normalized_value, cx, None);
14681468
}
14691469

14701470
let trait_segments = &p.segments[..p.segments.len() - 1];
@@ -1878,11 +1878,9 @@ fn clean_middle_opaque_bounds<'tcx>(
18781878
_ => return None,
18791879
};
18801880

1881-
if let Some(sized) = cx.tcx.lang_items().sized_trait() {
1882-
if trait_ref.def_id() == sized {
1883-
has_sized = true;
1884-
return None;
1885-
}
1881+
if let Some(sized) = cx.tcx.lang_items().sized_trait() && trait_ref.def_id() == sized {
1882+
has_sized = true;
1883+
return None;
18861884
}
18871885

18881886
let bindings: ThinVec<_> = bounds
@@ -2392,17 +2390,15 @@ fn clean_use_statement_inner<'tcx>(
23922390
let is_visible_from_parent_mod =
23932391
visibility.is_accessible_from(parent_mod, cx.tcx) && !current_mod.is_top_level_module();
23942392

2395-
if pub_underscore {
2396-
if let Some(ref inline) = inline_attr {
2397-
rustc_errors::struct_span_err!(
2398-
cx.tcx.sess,
2399-
inline.span(),
2400-
E0780,
2401-
"anonymous imports cannot be inlined"
2402-
)
2403-
.span_label(import.span, "anonymous import")
2404-
.emit();
2405-
}
2393+
if pub_underscore && let Some(ref inline) = inline_attr {
2394+
rustc_errors::struct_span_err!(
2395+
cx.tcx.sess,
2396+
inline.span(),
2397+
E0780,
2398+
"anonymous imports cannot be inlined"
2399+
)
2400+
.span_label(import.span, "anonymous import")
2401+
.emit();
24062402
}
24072403

24082404
// We consider inlining the documentation of `pub use` statements, but we
@@ -2438,14 +2434,13 @@ fn clean_use_statement_inner<'tcx>(
24382434
}
24392435
Import::new_glob(resolve_use_source(cx, path), true)
24402436
} else {
2441-
if inline_attr.is_none() {
2442-
if let Res::Def(DefKind::Mod, did) = path.res {
2443-
if !did.is_local() && did.is_crate_root() {
2444-
// if we're `pub use`ing an extern crate root, don't inline it unless we
2445-
// were specifically asked for it
2446-
denied = true;
2447-
}
2448-
}
2437+
if inline_attr.is_none()
2438+
&& let Res::Def(DefKind::Mod, did) = path.res
2439+
&& !did.is_local() && did.is_crate_root()
2440+
{
2441+
// if we're `pub use`ing an extern crate root, don't inline it unless we
2442+
// were specifically asked for it
2443+
denied = true;
24492444
}
24502445
if !denied {
24512446
let mut visited = DefIdSet::default();

src/librustdoc/clean/types.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,8 @@ impl ExternalCrate {
182182
return Local;
183183
}
184184

185-
if extern_url_takes_precedence {
186-
if let Some(url) = extern_url {
187-
return to_remote(url);
188-
}
185+
if extern_url_takes_precedence && let Some(url) = extern_url {
186+
return to_remote(url);
189187
}
190188

191189
// Failing that, see if there's an attribute specifying where to find this
@@ -1176,10 +1174,10 @@ impl GenericBound {
11761174

11771175
pub(crate) fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool {
11781176
use rustc_hir::TraitBoundModifier as TBM;
1179-
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1180-
if Some(trait_.def_id()) == cx.tcx.lang_items().sized_trait() {
1181-
return true;
1182-
}
1177+
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self &&
1178+
Some(trait_.def_id()) == cx.tcx.lang_items().sized_trait()
1179+
{
1180+
return true;
11831181
}
11841182
false
11851183
}

src/librustdoc/clean/utils.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,10 @@ pub(crate) fn is_literal_expr(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
345345
return true;
346346
}
347347

348-
if let hir::ExprKind::Unary(hir::UnOp::Neg, expr) = &expr.kind {
349-
if let hir::ExprKind::Lit(_) = &expr.kind {
350-
return true;
351-
}
348+
if let hir::ExprKind::Unary(hir::UnOp::Neg, expr) = &expr.kind &&
349+
let hir::ExprKind::Lit(_) = &expr.kind
350+
{
351+
return true;
352352
}
353353
}
354354

src/librustdoc/doctest.rs

+27-32
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ fn scrape_test_config(attrs: &[ast::Attribute]) -> GlobalTestOptions {
229229
if attr.has_name(sym::no_crate_inject) {
230230
opts.no_crate_inject = true;
231231
}
232-
if attr.has_name(sym::attr) {
233-
if let Some(l) = attr.meta_item_list() {
234-
for item in l {
235-
opts.attrs.push(pprust::meta_list_item_to_string(item));
236-
}
232+
if attr.has_name(sym::attr)
233+
&& let Some(l) = attr.meta_item_list()
234+
{
235+
for item in l {
236+
opts.attrs.push(pprust::meta_list_item_to_string(item));
237237
}
238238
}
239239
}
@@ -594,31 +594,28 @@ pub(crate) fn make_test(
594594
loop {
595595
match parser.parse_item(ForceCollect::No) {
596596
Ok(Some(item)) => {
597-
if !found_main {
598-
if let ast::ItemKind::Fn(..) = item.kind {
599-
if item.ident.name == sym::main {
600-
found_main = true;
601-
}
602-
}
597+
if !found_main &&
598+
let ast::ItemKind::Fn(..) = item.kind &&
599+
item.ident.name == sym::main
600+
{
601+
found_main = true;
603602
}
604603

605-
if !found_extern_crate {
606-
if let ast::ItemKind::ExternCrate(original) = item.kind {
607-
// This code will never be reached if `crate_name` is none because
608-
// `found_extern_crate` is initialized to `true` if it is none.
609-
let crate_name = crate_name.unwrap();
604+
if !found_extern_crate &&
605+
let ast::ItemKind::ExternCrate(original) = item.kind
606+
{
607+
// This code will never be reached if `crate_name` is none because
608+
// `found_extern_crate` is initialized to `true` if it is none.
609+
let crate_name = crate_name.unwrap();
610610

611-
match original {
612-
Some(name) => found_extern_crate = name.as_str() == crate_name,
613-
None => found_extern_crate = item.ident.as_str() == crate_name,
614-
}
611+
match original {
612+
Some(name) => found_extern_crate = name.as_str() == crate_name,
613+
None => found_extern_crate = item.ident.as_str() == crate_name,
615614
}
616615
}
617616

618-
if !found_macro {
619-
if let ast::ItemKind::MacCall(..) = item.kind {
620-
found_macro = true;
621-
}
617+
if !found_macro && let ast::ItemKind::MacCall(..) = item.kind {
618+
found_macro = true;
622619
}
623620

624621
if found_main && found_extern_crate {
@@ -972,14 +969,12 @@ impl Collector {
972969
fn get_filename(&self) -> FileName {
973970
if let Some(ref source_map) = self.source_map {
974971
let filename = source_map.span_to_filename(self.position);
975-
if let FileName::Real(ref filename) = filename {
976-
if let Ok(cur_dir) = env::current_dir() {
977-
if let Some(local_path) = filename.local_path() {
978-
if let Ok(path) = local_path.strip_prefix(&cur_dir) {
979-
return path.to_owned().into();
980-
}
981-
}
982-
}
972+
if let FileName::Real(ref filename) = filename &&
973+
let Ok(cur_dir) = env::current_dir() &&
974+
let Some(local_path) = filename.local_path() &&
975+
let Ok(path) = local_path.strip_prefix(&cur_dir)
976+
{
977+
return path.to_owned().into();
983978
}
984979
filename
985980
} else if let Some(ref filename) = self.filename {

src/librustdoc/formats/cache.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,15 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
229229
}
230230

231231
// Collect all the implementors of traits.
232-
if let clean::ImplItem(ref i) = *item.kind {
233-
if let Some(trait_) = &i.trait_ {
234-
if !i.kind.is_blanket() {
235-
self.cache
236-
.implementors
237-
.entry(trait_.def_id())
238-
.or_default()
239-
.push(Impl { impl_item: item.clone() });
240-
}
241-
}
232+
if let clean::ImplItem(ref i) = *item.kind &&
233+
let Some(trait_) = &i.trait_ &&
234+
!i.kind.is_blanket()
235+
{
236+
self.cache
237+
.implementors
238+
.entry(trait_.def_id())
239+
.or_default()
240+
.push(Impl { impl_item: item.clone() });
242241
}
243242

244243
// Index this method for searching later on.

src/librustdoc/html/format.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,9 @@ pub(crate) fn href_with_root_path(
710710
}
711711
}
712712
};
713-
if !is_remote {
714-
if let Some(root_path) = root_path {
715-
let root = root_path.trim_end_matches('/');
716-
url_parts.push_front(root);
717-
}
713+
if !is_remote && let Some(root_path) = root_path {
714+
let root = root_path.trim_end_matches('/');
715+
url_parts.push_front(root);
718716
}
719717
debug!(?url_parts);
720718
match shortty {

0 commit comments

Comments
 (0)