Skip to content

Commit f760088

Browse files
committed
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
2 parents ba66813 + c222e7e commit f760088

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

clippy_lints/src/methods/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2800,7 +2800,10 @@ impl SelfKind {
28002800
hir::Mutability::MutMutable => &paths::ASMUT_TRAIT,
28012801
};
28022802

2803-
let trait_def_id = get_trait_def_id(cx, trait_path).expect("trait def id not found");
2803+
let trait_def_id = match get_trait_def_id(cx, trait_path) {
2804+
Some(did) => did,
2805+
None => return false,
2806+
};
28042807
implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
28052808
}
28062809

clippy_lints/src/utils/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,16 @@ pub fn path_to_res(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<(def::Res)
261261
}
262262

263263
/// Convenience function to get the `DefId` of a trait by path.
264+
/// It could be a trait or trait alias.
264265
pub fn get_trait_def_id(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<DefId> {
265266
let res = match path_to_res(cx, path) {
266267
Some(res) => res,
267268
None => return None,
268269
};
269270

270271
match res {
271-
def::Res::Def(DefKind::Trait, trait_id) => Some(trait_id),
272+
Res::Def(DefKind::Trait, trait_id) | Res::Def(DefKind::TraitAlias, trait_id) => Some(trait_id),
273+
Res::Err => unreachable!("this trait resolution is impossible: {:?}", &path),
272274
_ => None,
273275
}
274276
}

tests/ui/def_id_nocore.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// ignore-windows
2+
// ignore-macos
3+
4+
#![feature(no_core, lang_items, start)]
5+
#![no_core]
6+
7+
#[link(name = "c")]
8+
extern "C" {}
9+
10+
#[lang = "sized"]
11+
pub trait Sized {}
12+
#[lang = "copy"]
13+
pub trait Copy {}
14+
#[lang = "freeze"]
15+
pub unsafe trait Freeze {}
16+
17+
#[lang = "start"]
18+
#[start]
19+
fn start(_argc: isize, _argv: *const *const u8) -> isize {
20+
0
21+
}
22+
23+
pub struct A;
24+
25+
impl A {
26+
pub fn as_ref(self) -> &'static str {
27+
"A"
28+
}
29+
}

tests/ui/def_id_nocore.stderr

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

0 commit comments

Comments
 (0)