Do not complain about unused code when used in impl Self type

This commit is contained in:
Esteban Küber 2019-08-05 19:23:46 -07:00
parent 7b0085a613
commit 053afa7aec
4 changed files with 66 additions and 20 deletions

View file

@ -30,10 +30,11 @@ fn should_explore(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
Some(Node::Item(..)) | Some(Node::Item(..)) |
Some(Node::ImplItem(..)) | Some(Node::ImplItem(..)) |
Some(Node::ForeignItem(..)) | Some(Node::ForeignItem(..)) |
Some(Node::TraitItem(..)) => Some(Node::TraitItem(..)) |
true, Some(Node::Variant(..)) |
_ => Some(Node::AnonConst(..)) |
false Some(Node::Pat(..)) => true,
_ => false
} }
} }
@ -75,7 +76,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
self.check_def_id(res.def_id()); self.check_def_id(res.def_id());
} }
_ if self.in_pat => {}, _ if self.in_pat => {},
Res::PrimTy(..) | Res::SelfTy(..) | Res::SelfCtor(..) | Res::PrimTy(..) | Res::SelfCtor(..) |
Res::Local(..) => {} Res::Local(..) => {}
Res::Def(DefKind::Ctor(CtorOf::Variant, ..), ctor_def_id) => { Res::Def(DefKind::Ctor(CtorOf::Variant, ..), ctor_def_id) => {
let variant_id = self.tcx.parent(ctor_def_id).unwrap(); let variant_id = self.tcx.parent(ctor_def_id).unwrap();
@ -92,6 +93,14 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
self.check_def_id(variant_id); self.check_def_id(variant_id);
} }
} }
Res::SelfTy(t, i) => {
if let Some(t) = t {
self.check_def_id(t);
}
if let Some(i) = i {
self.check_def_id(i);
}
}
Res::ToolMod | Res::NonMacroAttr(..) | Res::Err => {} Res::ToolMod | Res::NonMacroAttr(..) | Res::Err => {}
_ => { _ => {
self.check_def_id(res.def_id()); self.check_def_id(res.def_id());
@ -271,7 +280,7 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
let res = self.tables.qpath_res(path, pat.hir_id); let res = self.tables.qpath_res(path, pat.hir_id);
self.handle_field_pattern_match(pat, res, fields); self.handle_field_pattern_match(pat, res, fields);
} }
PatKind::Path(ref qpath @ hir::QPath::TypeRelative(..)) => { PatKind::Path(ref qpath) => {
let res = self.tables.qpath_res(qpath, pat.hir_id); let res = self.tables.qpath_res(qpath, pat.hir_id);
self.handle_res(res); self.handle_res(res);
} }
@ -298,6 +307,11 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
} }
intravisit::walk_ty(self, ty); intravisit::walk_ty(self, ty);
} }
fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
self.live_symbols.insert(c.hir_id);
intravisit::walk_anon_const(self, c);
}
} }
fn has_allow_dead_code_or_lang_attr( fn has_allow_dead_code_or_lang_attr(

View file

@ -5,12 +5,15 @@
// when deriving Debug on an empty enum // when deriving Debug on an empty enum
#[derive(Debug)] #[derive(Debug)]
enum Void {} //~ WARN never used enum Void {}
#[derive(Debug)] #[derive(Debug)]
enum Foo { //~ WARN never used enum Foo {
Bar(u8), Bar(u8),
Void(Void), Void(Void), //~ WARN never used
} }
fn main() {} fn main() {
let x = Foo::Bar(42);
println!("{:?}", x);
}

View file

@ -1,14 +1,8 @@
warning: enum is never used: `Void` warning: variant is never constructed: `Void`
--> $DIR/derive-uninhabited-enum-38885.rs:8:1 --> $DIR/derive-uninhabited-enum-38885.rs:13:5
| |
LL | enum Void {} LL | Void(Void),
| ^^^^^^^^^ | ^^^^^^^^^^
| |
= note: `-W dead-code` implied by `-W unused` = note: `-W dead-code` implied by `-W unused`
warning: enum is never used: `Foo`
--> $DIR/derive-uninhabited-enum-38885.rs:11:1
|
LL | enum Foo {
| ^^^^^^^^

View file

@ -0,0 +1,35 @@
// check-pass
#![deny(dead_code)]
const TLC: usize = 4;
trait Tr { fn doit(&self); }
impl Tr for [usize; TLC] {
fn doit(&self) {
println!("called 4");
}
}
struct X;
struct Y;
struct Z;
trait Foo<T> {
type Ty;
fn foo() -> Self::Ty;
}
impl Foo<Y> for X {
type Ty = Z;
fn foo() -> Self::Ty {
unimplemented!()
}
}
fn main() {
let s = [0,1,2,3];
s.doit();
X::foo();
}