Skip to content

Account for trait alias when looking for defid #4430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
}

Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,16 @@ 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,
None => return None,
};

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,
}
}
Expand Down
29 changes: 29 additions & 0 deletions tests/ui/def_id_nocore.rs
Original file line number Diff line number Diff line change
@@ -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"
}
}
10 changes: 10 additions & 0 deletions tests/ui/def_id_nocore.stderr
Original file line number Diff line number Diff line change
@@ -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`
Copy link
Member

@flip1995 flip1995 Aug 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact: If you add the & to self in a no_core crate, rustc complains that

error[E0658]: `&A` cannot be used as the type of `self` without the `arbitrary_self_types` feature
  --> tests/ui/def_id_nocore.rs:26:19
   |
26 |     pub fn as_ref(&self) -> &'static str {
   |                   ^^^^^
   |
   = note: for more information, see https://github.com/rust-lang/rust/issues/44874
   = help: add `#![feature(arbitrary_self_types)]` to the crate attributes to enable
   = help: consider changing to `self`, `&self`, `&mut self`, `self: Box<Self>`, `self: Rc<Self>`, `self: Arc<Self>`, or `self: Pin<P>` (where P is one of the previous types except `Self`)

I don't think, that we need to adapt the warning though 😄


error: aborting due to previous error