Skip to content

Commit 9fd4f3e

Browse files
committed
Auto merge of rust-lang#6664 - camsteffen:path-to-res, r=Manishearth
Remove Option from `path_to_res` return type changelog: none Tiny cleanup for `path_to_res` to return `Res` instead of `Option<Res>`.
2 parents 28794e9 + 939136d commit 9fd4f3e

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

clippy_lints/src/utils/internal_lints.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ impl<'tcx> LateLintPass<'tcx> for MatchTypeOnDiagItem {
760760
// Extract the path to the matched type
761761
if let Some(segments) = path_to_matched_type(cx, ty_path);
762762
let segments: Vec<&str> = segments.iter().map(|sym| &**sym).collect();
763-
if let Some(ty_did) = path_to_res(cx, &segments[..]).and_then(|res| res.opt_def_id());
763+
if let Some(ty_did) = path_to_res(cx, &segments[..]).opt_def_id();
764764
// Check if the matched type is a diagnostic item
765765
let diag_items = cx.tcx.diagnostic_items(ty_did.krate);
766766
if let Some(item_name) = diag_items.iter().find_map(|(k, v)| if *v == ty_did { Some(k) } else { None });
@@ -833,7 +833,7 @@ fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Ve
833833
// This is not a complete resolver for paths. It works on all the paths currently used in the paths
834834
// module. That's all it does and all it needs to do.
835835
pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool {
836-
if path_to_res(cx, path).is_some() {
836+
if path_to_res(cx, path) != Res::Err {
837837
return true;
838838
}
839839

@@ -906,7 +906,7 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
906906
}
907907

908908
for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] {
909-
if let Some(Res::Def(_, def_id)) = path_to_res(cx, module) {
909+
if let Some(def_id) = path_to_res(cx, module).opt_def_id() {
910910
for item in cx.tcx.item_children(def_id).iter() {
911911
if_chain! {
912912
if let Res::Def(DefKind::Const, item_def_id) = item.res;

clippy_lints/src/utils/mod.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,15 @@ pub fn match_path_ast(path: &ast::Path, segments: &[&str]) -> bool {
309309

310310
/// Gets the definition associated to a path.
311311
#[allow(clippy::shadow_unrelated)] // false positive #6563
312-
pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Option<Res> {
312+
pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
313+
macro_rules! try_res {
314+
($e:expr) => {
315+
match $e {
316+
Some(e) => e,
317+
None => return Res::Err,
318+
}
319+
};
320+
}
313321
fn item_child_by_name<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, name: &str) -> Option<&'tcx Export<HirId>> {
314322
tcx.item_children(def_id)
315323
.iter()
@@ -318,12 +326,12 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Option<Res> {
318326

319327
let (krate, first, path) = match *path {
320328
[krate, first, ref path @ ..] => (krate, first, path),
321-
_ => return None,
329+
_ => return Res::Err,
322330
};
323331
let tcx = cx.tcx;
324332
let crates = tcx.crates();
325-
let krate = crates.iter().find(|&&num| tcx.crate_name(num).as_str() == krate)?;
326-
let first = item_child_by_name(tcx, krate.as_def_id(), first)?;
333+
let krate = try_res!(crates.iter().find(|&&num| tcx.crate_name(num).as_str() == krate));
334+
let first = try_res!(item_child_by_name(tcx, krate.as_def_id(), first));
327335
let last = path
328336
.iter()
329337
.copied()
@@ -343,21 +351,15 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Option<Res> {
343351
} else {
344352
None
345353
}
346-
})?;
347-
Some(last.res)
354+
});
355+
try_res!(last).res
348356
}
349357

350358
/// Convenience function to get the `DefId` of a trait by path.
351359
/// It could be a trait or trait alias.
352360
pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option<DefId> {
353-
let res = match path_to_res(cx, path) {
354-
Some(res) => res,
355-
None => return None,
356-
};
357-
358-
match res {
361+
match path_to_res(cx, path) {
359362
Res::Def(DefKind::Trait | DefKind::TraitAlias, trait_id) => Some(trait_id),
360-
Res::Err => unreachable!("this trait resolution is impossible: {:?}", &path),
361363
_ => None,
362364
}
363365
}

0 commit comments

Comments
 (0)