Change:
E.g. (The repetition is .) However, all of these are useless: because one cannot implement them for anyway, it would be better to just filter them out, e.g. reject external traits when the self type itself is external. cc
Good point, although it's not always as simple as that -- with multidispatch it is plausible you COULD implement the trait for if another type parameter were local, though not with the current orphan rules. Nonetheless, it might be good to just screen them out anyway, as it is unlikely to be what you plan to do.

Discussion:
method_name: ast::Name, error: MethodError) { <ins> // avoid suggestions when we don't know what's going on. if ty::type_is_error(rcvr_ty) { return } </ins> match error { MethodError::NoMatch(static_sources, out_of_scope_traits) => { let cx = fcx.tcx();

fn suggest_traits_to_import<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, span: Span, <del> _rcvr_ty: Ty<'tcx>, </del> <ins> rcvr_ty: Ty<'tcx>, </ins> method_name: ast::Name, valid_out_of_scope_traits: Vec<ast::DefId>) {

return } <del> // there's no implemented traits, so lets suggest some traits to implement </del> <ins> let type_is_local = type_derefs_to_local(fcx, span, rcvr_ty); // there's no implemented traits, so lets suggest some traits to // implement, by finding ones that have the method name, and are // legal to implement. </ins> let mut candidates = all_traits(fcx.ccx) <del> .filter(|info| trait_method(tcx, info.def_id, method_name).is_some()) </del> <ins> .filter(|info| { // we approximate the coherence rules to only suggest // traits that are legal to implement by requiring that // either the type or trait is local. Multidispatch means // this isn't perfect (that is, there are cases when // implementing a trait would be legal but is rejected // here). (type_is_local || ast_util::is_local(info.def_id)) && trait_method(tcx, info.def_id, method_name).is_some() }) </ins> .collect::<Vec<_>>(); if candidates.len() > 0 {

candidates.sort_by(|a, b| a.cmp(b).reverse()); candidates.dedup(); <ins> // FIXME #21673 this help message could be tuned to the case // of a type parameter: suggest adding a trait bound rather // than implementing. </ins> let msg = format!( "methods from traits can only be called if the trait is implemented and in scope;  the following {traits_define} a method `{name}`, 

} } <ins> /// Checks whether there is a local type somewhere in the chain of /// autoderefs of `rcvr_ty`. fn type_derefs_to_local<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, span: Span, rcvr_ty: Ty<'tcx>) -> bool { check::autoderef(fcx, span, rcvr_ty, None, check::UnresolvedTypeAction::Ignore, check::NoPreference, |&: ty, _| { let is_local = match ty.sty { ty::ty_enum(did, _) | ty::ty_struct(did, _) => ast_util::is_local(did), ty::ty_trait(ref tr) => ast_util::is_local(tr.principal_def_id()), ty::ty_param(_) => true, // the user cannot implement traits for unboxed closures, so // there's no point suggesting anything at all, local or not. ty::ty_closure(..) => return Some(false), // everything else (primitive types etc.) is effectively // non-local (there are "edge" cases, e.g. (LocalType,), but // the noise from these sort of types is usually just really // annoying, rather than any sort of help). _ => false }; if is_local { Some(true) } else { None } }).2.unwrap_or(false) } </ins> #[derive(Copy)] pub struct TraitInfo { pub def_id: ast::DefId,

pub use reexport::Reexported; <ins> pub struct Foo; pub enum Bar { X } </ins> pub mod foo { pub trait PubPub { fn method(&self) {}

// issue #21405 <del> fn foo<F>(f: F) where F: FnMut(usize) {} </del> <ins> struct Foo; fn foo<F>(f: F) where F: FnMut(Foo) {} </ins> fn main() { foo(|s| s.is_empty());

extern crate no_method_suggested_traits; <ins> struct Foo; enum Bar { X } </ins> mod foo { trait Bar { fn method(&self) {}

} fn main() { <ins> // test the values themselves, and autoderef. </ins> 1u32.method(); //~^ HELP following traits are implemented but not in scope, perhaps add a `use` for one of them //~^^ ERROR does not implement //~^^^ HELP `foo::Bar` //~^^^^ HELP `no_method_suggested_traits::foo::PubPub` <ins> std::rc::Rc::new(&mut Box::new(&1u32)).method(); //~^ HELP following traits are implemented but not in scope, perhaps add a `use` for one of them //~^^ ERROR does not implement //~^^^ HELP `foo::Bar` //~^^^^ HELP `no_method_suggested_traits::foo::PubPub` </ins> 'a'.method(); //~^ ERROR does not implement //~^^ HELP the following trait is implemented but not in scope, perhaps add a `use` for it: //~^^^ HELP `foo::Bar` <ins> std::rc::Rc::new(&mut Box::new(&'a')).method(); //~^ ERROR does not implement //~^^ HELP the following trait is implemented but not in scope, perhaps add a `use` for it: //~^^^ HELP `foo::Bar` </ins> 1i32.method(); //~^ ERROR does not implement //~^^ HELP the following trait is implemented but not in scope, perhaps add a `use` for it: //~^^^ HELP `no_method_suggested_traits::foo::PubPub` <ins> std::rc::Rc::new(&mut Box::new(&1i32)).method(); //~^ ERROR does not implement //~^^ HELP the following trait is implemented but not in scope, perhaps add a `use` for it: //~^^^ HELP `no_method_suggested_traits::foo::PubPub` </ins> <del> 1u64.method(); </del> <ins> Foo.method(); //~^ ERROR does not implement //~^^ HELP following traits define a method `method`, perhaps you need to implement one of them //~^^^ HELP `foo::Bar` //~^^^^ HELP `no_method_suggested_traits::foo::PubPub` //~^^^^^ HELP `no_method_suggested_traits::reexport::Reexported` //~^^^^^^ HELP `no_method_suggested_traits::bar::PubPriv` //~^^^^^^^ HELP `no_method_suggested_traits::qux::PrivPub` //~^^^^^^^^ HELP `no_method_suggested_traits::quz::PrivPriv` std::rc::Rc::new(&mut Box::new(&Foo)).method(); </ins> //~^ ERROR does not implement //~^^ HELP following traits define a method `method`, perhaps you need to implement one of them //~^^^ HELP `foo::Bar`

//~^ ERROR does not implement //~^^ HELP the following trait defines a method `method2`, perhaps you need to implement it //~^^^ HELP `foo::Bar` <del> 1u64.method3(); </del> <ins> std::rc::Rc::new(&mut Box::new(&1u64)).method2(); </ins> //~^ ERROR does not implement <del> //~^^ HELP the following trait defines a method `method3`, perhaps you need to implement it </del> <ins> //~^^ HELP the following trait defines a method `method2`, perhaps you need to implement it //~^^^ HELP `foo::Bar` no_method_suggested_traits::Foo.method2(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method2`, perhaps you need to implement it //~^^^ HELP `foo::Bar` std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Foo)).method2(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method2`, perhaps you need to implement it //~^^^ HELP `foo::Bar` no_method_suggested_traits::Bar::X.method2(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method2`, perhaps you need to implement it //~^^^ HELP `foo::Bar` std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Bar::X)).method2(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method2`, perhaps you need to implement it //~^^^ HELP `foo::Bar` Foo.method3(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method3`, perhaps you need to implement it //~^^^ HELP `no_method_suggested_traits::foo::PubPub` std::rc::Rc::new(&mut Box::new(&Foo)).method3(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method3`, perhaps you need to implement it //~^^^ HELP `no_method_suggested_traits::foo::PubPub` Bar::X.method3(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method3`, perhaps you need to implement it </ins> //~^^^ HELP `no_method_suggested_traits::foo::PubPub` <ins> std::rc::Rc::new(&mut Box::new(&Bar::X)).method3(); //~^ ERROR does not implement //~^^ HELP following trait defines a method `method3`, perhaps you need to implement it //~^^^ HELP `no_method_suggested_traits::foo::PubPub` // should have no help: 1us.method3(); //~ ERROR does not implement std::rc::Rc::new(&mut Box::new(&1us)).method3(); //~ ERROR does not implement no_method_suggested_traits::Foo.method3();  //~ ERROR does not implement std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Foo)).method3(); //~^ ERROR does not implement no_method_suggested_traits::Bar::X.method3();  //~ ERROR does not implement std::rc::Rc::new(&mut Box::new(&no_method_suggested_traits::Bar::X)).method3(); //~^ ERROR does not implement </ins> }