Auto merge of #4430 - lzutao:defid_trait_alias, r=flip1995

Account for trait alias when looking for defid

I hit the crash on the `expect` call when running clippy on rustc libcore.
Hopefully this will fix it.
changelog: none
This commit is contained in:
bors 2019-08-26 12:46:00 +00:00
commit f760088820
4 changed files with 46 additions and 2 deletions

View File

@ -2800,7 +2800,10 @@ impl SelfKind {
hir::Mutability::MutMutable => &paths::ASMUT_TRAIT,
};
let trait_def_id = get_trait_def_id(cx, trait_path).expect("trait def id not found");
let trait_def_id = match get_trait_def_id(cx, trait_path) {
Some(did) => did,
None => return false,
};
implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
}

View File

@ -261,6 +261,7 @@ pub fn path_to_res(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<(def::Res)
}
/// Convenience function to get the `DefId` of a trait by path.
/// It could be a trait or trait alias.
pub fn get_trait_def_id(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<DefId> {
let res = match path_to_res(cx, path) {
Some(res) => res,
@ -268,7 +269,8 @@ pub fn get_trait_def_id(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<DefId
};
match res {
def::Res::Def(DefKind::Trait, trait_id) => Some(trait_id),
Res::Def(DefKind::Trait, trait_id) | Res::Def(DefKind::TraitAlias, trait_id) => Some(trait_id),
Res::Err => unreachable!("this trait resolution is impossible: {:?}", &path),
_ => None,
}
}

29
tests/ui/def_id_nocore.rs Normal file
View File

@ -0,0 +1,29 @@
// ignore-windows
// ignore-macos
#![feature(no_core, lang_items, start)]
#![no_core]
#[link(name = "c")]
extern "C" {}
#[lang = "sized"]
pub trait Sized {}
#[lang = "copy"]
pub trait Copy {}
#[lang = "freeze"]
pub unsafe trait Freeze {}
#[lang = "start"]
#[start]
fn start(_argc: isize, _argv: *const *const u8) -> isize {
0
}
pub struct A;
impl A {
pub fn as_ref(self) -> &'static str {
"A"
}
}

View File

@ -0,0 +1,10 @@
error: methods called `as_*` usually take self by reference or self by mutable reference; consider choosing a less ambiguous name
--> $DIR/def_id_nocore.rs:26:19
|
LL | pub fn as_ref(self) -> &'static str {
| ^^^^
|
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
error: aborting due to previous error