From dac2509bacd9e4c3f7cc27a4aeff38c5b0388840 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Thu, 22 Aug 2019 10:03:26 +0700 Subject: [PATCH 1/2] Account for trait alias when looking for defid --- clippy_lints/src/methods/mod.rs | 5 ++++- clippy_lints/src/utils/mod.rs | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index f7544e095b8..0eaec449dc4 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2781,7 +2781,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()]) } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 2d772d77ed1..408deb1b402 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -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 { 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 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, } } From c222e7eca716755f7edcf91a0db8bc523781f0cc Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 26 Aug 2019 17:11:47 +0700 Subject: [PATCH 2/2] Add regression test for looking for trait defid in nocore --- tests/ui/def_id_nocore.rs | 29 +++++++++++++++++++++++++++++ tests/ui/def_id_nocore.stderr | 10 ++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/ui/def_id_nocore.rs create mode 100644 tests/ui/def_id_nocore.stderr diff --git a/tests/ui/def_id_nocore.rs b/tests/ui/def_id_nocore.rs new file mode 100644 index 00000000000..2a948d60b10 --- /dev/null +++ b/tests/ui/def_id_nocore.rs @@ -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" + } +} diff --git a/tests/ui/def_id_nocore.stderr b/tests/ui/def_id_nocore.stderr new file mode 100644 index 00000000000..ed87a50547d --- /dev/null +++ b/tests/ui/def_id_nocore.stderr @@ -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 +