Skip to content

Commit 3a640f2

Browse files
committed
Clean up hard to follow control flow
1 parent 90e0fed commit 3a640f2

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,40 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
291291
resolver.resolve_str_path_error(DUMMY_SP, &path_str, ns, module_id)
292292
});
293293
debug!("{} resolved to {:?} in namespace {:?}", path_str, result, ns);
294-
let result = match result {
295-
Ok((_, Res::Err)) => Err(()),
296-
x => x,
294+
let result = match result.map(|(_, res)| res) {
295+
Ok(Res::Err) | Err(()) => {
296+
// resolver doesn't know about true and false so we'll have to resolve them
297+
// manually as bool
298+
if let Some((_, res)) = is_bool_value(path_str, ns) { Ok(res) } else { Err(()) }
299+
}
300+
Ok(res) => Ok(res.map_id(|_| panic!("unexpected node_id"))),
297301
};
298302

299-
if let Ok((_, res)) = result {
300-
let res = res.map_id(|_| panic!("unexpected node_id"));
301-
// In case this is a trait item, skip the
302-
// early return and try looking for the trait.
303-
let value = match res {
304-
Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => true,
305-
Res::Def(DefKind::AssocTy, _) => false,
303+
if let Ok(res) = result {
304+
match res {
305+
Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => {
306+
if ns != ValueNS {
307+
return Err(ResolutionFailure::WrongNamespace(res, ns).into());
308+
} else {
309+
// In case this is a trait item, skip the
310+
// early return and try looking for the trait.
311+
}
312+
}
313+
Res::Def(DefKind::AssocTy, _) => {
314+
if ns == ValueNS {
315+
return Err(ResolutionFailure::WrongNamespace(res, ns).into());
316+
} else {
317+
// In case this is a trait item, skip the
318+
// early return and try looking for the trait.
319+
}
320+
}
306321
Res::Def(DefKind::Variant, _) => {
307-
return handle_variant(cx, res, extra_fragment);
322+
if extra_fragment.is_some() {
323+
return Err(ErrorKind::AnchorFailure(
324+
AnchorFailure::RustdocAnchorConflict(res),
325+
));
326+
}
327+
return handle_variant(cx, res);
308328
}
309329
// Not a trait item; just return what we found.
310330
Res::PrimTy(ty) => {
@@ -321,17 +341,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
321341
_ => {
322342
return Ok((res, extra_fragment.clone()));
323343
}
324-
};
325-
326-
if value != (ns == ValueNS) {
327-
return Err(ResolutionFailure::WrongNamespace(res, ns).into());
328344
}
329-
// FIXME: why is this necessary?
330-
} else if let Some((path, prim)) = is_primitive(path_str, ns) {
331-
if extra_fragment.is_some() {
332-
return Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(prim)));
333-
}
334-
return Ok((prim, Some(path.to_owned())));
335345
}
336346

337347
// Try looking for methods and associated items.
@@ -1936,21 +1946,17 @@ fn privacy_error(
19361946
fn handle_variant(
19371947
cx: &DocContext<'_>,
19381948
res: Res,
1939-
extra_fragment: &Option<String>,
19401949
) -> Result<(Res, Option<String>), ErrorKind<'static>> {
19411950
use rustc_middle::ty::DefIdTree;
19421951

1943-
if extra_fragment.is_some() {
1944-
return Err(ErrorKind::AnchorFailure(AnchorFailure::RustdocAnchorConflict(res)));
1945-
}
1946-
let parent = if let Some(parent) = cx.tcx.parent(res.def_id()) {
1947-
parent
1948-
} else {
1949-
return Err(ResolutionFailure::NoParentItem.into());
1950-
};
1951-
let parent_def = Res::Def(DefKind::Enum, parent);
1952-
let variant = cx.tcx.expect_variant_res(res);
1953-
Ok((parent_def, Some(format!("variant.{}", variant.ident.name))))
1952+
cx.tcx.parent(res.def_id()).map_or_else(
1953+
|| Err(ResolutionFailure::NoParentItem.into()),
1954+
|parent| {
1955+
let parent_def = Res::Def(DefKind::Enum, parent);
1956+
let variant = cx.tcx.expect_variant_res(res);
1957+
Ok((parent_def, Some(format!("variant.{}", variant.ident.name))))
1958+
},
1959+
)
19541960
}
19551961

19561962
const PRIMITIVES: &[(&str, Res)] = &[
@@ -1970,19 +1976,16 @@ const PRIMITIVES: &[(&str, Res)] = &[
19701976
("f64", Res::PrimTy(hir::PrimTy::Float(rustc_ast::FloatTy::F64))),
19711977
("str", Res::PrimTy(hir::PrimTy::Str)),
19721978
("bool", Res::PrimTy(hir::PrimTy::Bool)),
1973-
("true", Res::PrimTy(hir::PrimTy::Bool)),
1974-
("false", Res::PrimTy(hir::PrimTy::Bool)),
19751979
("char", Res::PrimTy(hir::PrimTy::Char)),
19761980
];
19771981

19781982
fn is_primitive(path_str: &str, ns: Namespace) -> Option<(&'static str, Res)> {
1979-
if ns == TypeNS {
1980-
PRIMITIVES
1981-
.iter()
1982-
.filter(|x| x.0 == path_str)
1983-
.copied()
1984-
.map(|x| if x.0 == "true" || x.0 == "false" { ("bool", x.1) } else { x })
1985-
.next()
1983+
if ns == TypeNS { PRIMITIVES.iter().find(|x| x.0 == path_str).copied() } else { None }
1984+
}
1985+
1986+
fn is_bool_value(path_str: &str, ns: Namespace) -> Option<(&'static str, Res)> {
1987+
if ns == TypeNS && (path_str == "true" || path_str == "false") {
1988+
Some(("bool", Res::PrimTy(hir::PrimTy::Bool)))
19861989
} else {
19871990
None
19881991
}

0 commit comments

Comments
 (0)