Skip to content
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

Warn unused trait imports, rebased #33091

Merged
merged 7 commits into from
May 8, 2016
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
1 change: 0 additions & 1 deletion src/libcoretest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#![feature(box_syntax)]
#![feature(cell_extras)]
#![feature(const_fn)]
#![feature(core_float)]
#![feature(core_private_bignum)]
#![feature(core_private_diy_float)]
#![feature(dec2flt)]
Expand Down
1 change: 0 additions & 1 deletion src/libcoretest/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ pub fn test_num<T>(ten: T, two: T) where
mod tests {
use core::option::Option;
use core::option::Option::{Some, None};
use core::num::Float;

#[test]
fn from_str_issue7588() {
Expand Down
1 change: 1 addition & 0 deletions src/librand/distributions/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

//! The exponential distribution.

#[cfg(not(test))] // only necessary for no_std
use FloatMath;

use {Rng, Rand};
Expand Down
1 change: 1 addition & 0 deletions src/librand/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use self::GammaRepr::*;
use self::ChiSquaredRepr::*;

#[cfg(not(test))] // only necessary for no_std
use FloatMath;

use {Rng, Open01};
Expand Down
2 changes: 2 additions & 0 deletions src/librand/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
//! internally. The `IndependentSample` trait is for generating values
//! that do not need to record state.

#[cfg(not(test))] // only necessary for no_std
use core::num::Float;

use core::marker::PhantomData;

use {Rng, Rand};
Expand Down
1 change: 1 addition & 0 deletions src/librand/distributions/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

//! The normal and derived distributions.

#[cfg(not(test))] // only necessary for no_std
use FloatMath;

use {Rng, Rand, Open01};
Expand Down
2 changes: 1 addition & 1 deletion src/librand/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
#![unstable(feature = "rand",
reason = "use `rand` from crates.io",
issue = "27703")]
#![feature(core_float)]
#![feature(core_intrinsics)]
#![feature(staged_api)]
#![feature(step_by)]
#![feature(custom_attribute)]
#![allow(unused_attributes)]

#![cfg_attr(not(test), feature(core_float))] // only necessary for no_std
#![cfg_attr(test, feature(test, rand))]

#![allow(deprecated)]
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub enum DepNode<D: Clone + Debug> {
TypeckItemBody(D),
Dropck,
DropckImpl(D),
UnusedTraitCheck,
CheckConst(D),
Privacy,
IntrinsicCheck(D),
Expand Down Expand Up @@ -163,6 +164,7 @@ impl<D: Clone + Debug> DepNode<D> {
CheckEntryFn => Some(CheckEntryFn),
Variance => Some(Variance),
Dropck => Some(Dropck),
UnusedTraitCheck => Some(UnusedTraitCheck),
Privacy => Some(Privacy),
Reachability => Some(Reachability),
DeadCheck => Some(DeadCheck),
Expand Down
7 changes: 6 additions & 1 deletion src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1639,8 +1639,13 @@ pub type FreevarMap = NodeMap<Vec<Freevar>>;

pub type CaptureModeMap = NodeMap<CaptureClause>;

pub struct TraitCandidate {
pub def_id: DefId,
pub import_id: Option<NodeId>,
}

// Trait method resolution
pub type TraitMap = NodeMap<Vec<DefId>>;
pub type TraitMap = NodeMap<Vec<TraitCandidate>>;

// Map from the NodeId of a glob import to a list of items which are actually
// imported.
Expand Down
9 changes: 9 additions & 0 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ pub struct TyCtxt<'tcx> {
// scratch every time.
pub freevars: RefCell<FreevarMap>,

pub maybe_unused_trait_imports: NodeSet,

// Records the type of every item.
pub tcache: RefCell<DepTrackingMap<maps::Tcache<'tcx>>>,

Expand Down Expand Up @@ -338,6 +340,10 @@ pub struct TyCtxt<'tcx> {
/// about.
pub used_mut_nodes: RefCell<NodeSet>,

/// Set of trait imports actually used in the method resolution.
/// This is used for warning unused imports.
pub used_trait_imports: RefCell<NodeSet>,

/// The set of external nominal types whose implementations have been read.
/// This is used for lazy resolution of methods.
pub populated_external_types: RefCell<DefIdSet>,
Expand Down Expand Up @@ -543,6 +549,7 @@ impl<'tcx> TyCtxt<'tcx> {
named_region_map: resolve_lifetime::NamedRegionMap,
map: ast_map::Map<'tcx>,
freevars: FreevarMap,
maybe_unused_trait_imports: NodeSet,
region_maps: RegionMaps,
lang_items: middle::lang_items::LanguageItems,
stability: stability::Index<'tcx>,
Expand Down Expand Up @@ -581,6 +588,7 @@ impl<'tcx> TyCtxt<'tcx> {
fulfilled_predicates: RefCell::new(fulfilled_predicates),
map: map,
freevars: RefCell::new(freevars),
maybe_unused_trait_imports: maybe_unused_trait_imports,
tcache: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
rcache: RefCell::new(FnvHashMap()),
tc_cache: RefCell::new(FnvHashMap()),
Expand All @@ -595,6 +603,7 @@ impl<'tcx> TyCtxt<'tcx> {
impl_items: RefCell::new(DepTrackingMap::new(dep_graph.clone())),
used_unsafe: RefCell::new(NodeSet()),
used_mut_nodes: RefCell::new(NodeSet()),
used_trait_imports: RefCell::new(NodeSet()),
populated_external_types: RefCell::new(DefIdSet()),
populated_external_primitive_impls: RefCell::new(DefIdSet()),
extern_const_statics: RefCell::new(DefIdMap()),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_back/sha2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ mod tests {
use self::rand::isaac::IsaacRng;
use serialize::hex::FromHex;
use std::u64;
use super::{Digest, Sha256, FixedBuffer};
use super::{Digest, Sha256};

// A normal addition - no overflow occurs
#[test]
Expand Down Expand Up @@ -648,7 +648,7 @@ mod tests {
mod bench {
extern crate test;
use self::test::Bencher;
use super::{Sha256, FixedBuffer, Digest};
use super::{Sha256, Digest};

#[bench]
pub fn sha256_10(b: &mut Bencher) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_const_eval/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use self::EvalHint::*;

use rustc::hir::map as ast_map;
use rustc::hir::map::blocks::FnLikeNode;
use rustc::middle::cstore::{self, CrateStore, InlinedItem};
use rustc::middle::cstore::{self, InlinedItem};
use rustc::{infer, traits};
use rustc::hir::def::Def;
use rustc::hir::def_id::DefId;
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
let resolve::CrateMap {
def_map,
freevars,
maybe_unused_trait_imports,
export_map,
trait_map,
glob_map,
Expand Down Expand Up @@ -844,6 +845,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
named_region_map,
hir_map,
freevars,
maybe_unused_trait_imports,
region_map,
lang_items,
index,
Expand Down
1 change: 0 additions & 1 deletion src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ use rustc_trans::back::link;
use rustc::session::{self, config, Session, build_session, CompileResult};
use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
use rustc::session::config::{get_unstable_features_setting, nightly_options};
use rustc::middle::cstore::CrateStore;
use rustc::lint::Lint;
use rustc::lint;
use rustc_metadata::loader;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use rustc::ty::subst;
use rustc::ty::subst::Subst;
use rustc::traits::ProjectionMode;
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::ty::relate::TypeRelation;
use rustc::infer::{self, InferOk, InferResult, TypeOrigin};
use rustc_metadata::cstore::CStore;
use rustc_metadata::creader::LocalCrateReader;
Expand Down Expand Up @@ -132,7 +131,7 @@ fn test_env<F>(source_string: &str,

// run just enough stuff to build a tcx:
let lang_items = lang_items::collect_language_items(&sess, &ast_map);
let resolve::CrateMap { def_map, freevars, .. } =
let resolve::CrateMap { def_map, freevars, maybe_unused_trait_imports, .. } =
resolve::resolve_crate(&sess, &ast_map, resolve::MakeGlobMap::No);
let named_region_map = resolve_lifetime::krate(&sess, &ast_map, &def_map.borrow());
let region_map = region::resolve_crate(&sess, &ast_map);
Expand All @@ -143,6 +142,7 @@ fn test_env<F>(source_string: &str,
named_region_map.unwrap(),
ast_map,
freevars,
maybe_unused_trait_imports,
region_map,
lang_items,
index,
Expand Down
1 change: 0 additions & 1 deletion src/librustc_incremental/persist/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//! The data that we will serialize and deserialize.

use rustc::dep_graph::DepNode;
use rustc_serialize::{Decoder as RustcDecoder, Encoder as RustcEncoder};

use super::directory::DefPathIndex;

Expand Down
1 change: 0 additions & 1 deletion src/librustc_incremental/persist/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use rustc::hir::map::DefPath;
use rustc::hir::def_id::DefId;
use rustc::ty;
use rustc::util::nodemap::DefIdMap;
use rustc_serialize::{Decoder as RustcDecoder, Encoder as RustcEncoder};
use std::fmt::{self, Debug};

/// Index into the DefIdDirectory
Expand Down
1 change: 0 additions & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
//! a `pub fn new()`.

use rustc::hir::def::Def;
use middle::cstore::CrateStore;
use rustc::hir::def_id::DefId;
use middle::stability;
use rustc::{cfg, infer};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};

use syntax::ast;
use syntax::abi::Abi;
use syntax::attr::{self, AttrMetaMethods};
use syntax::attr;
use syntax::codemap::{self, Span};

use rustc::hir;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
let mut visible_parent_map = self.visible_parent_map.borrow_mut();
if !visible_parent_map.is_empty() { return visible_parent_map; }

use rustc::middle::cstore::{CrateStore, ChildItem};
use rustc::middle::cstore::ChildItem;
use std::collections::vec_deque::VecDeque;
use std::collections::hash_map::Entry;
for cnum in 1 .. self.next_crate_num() {
Expand Down
1 change: 0 additions & 1 deletion src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use rustc::mir;
use rustc::mir::visit::MutVisitor;

use std::cell::Cell;
use std::io::prelude::*;
use std::io;
use std::rc::Rc;
use std::str;
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use def_key;
use tyencode;
use index::{self, IndexData};

use middle::cstore::{LOCAL_CRATE, CrateStore, InlinedItemRef, LinkMeta, tls};
use middle::cstore::{LOCAL_CRATE, InlinedItemRef, LinkMeta, tls};
use rustc::hir::def;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
use middle::dependency_format::Linkage;
Expand All @@ -46,7 +46,6 @@ use syntax::abi::Abi;
use syntax::ast::{self, NodeId, Name, CRATE_NODE_ID, CrateNum};
use syntax::codemap::BytePos;
use syntax::attr;
use syntax::attr::AttrMetaMethods;
use syntax::errors::Handler;
use syntax;
use rbml::writer::Encoder;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ use std::cmp;
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::io::prelude::*;
use std::io;
use std::path::{Path, PathBuf};
use std::ptr;
Expand Down
1 change: 0 additions & 1 deletion src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use rustc::hir::intravisit::{self, Visitor};

use rustc::dep_graph::DepNode;
use rustc::lint;
use rustc::middle::cstore::CrateStore;
use rustc::hir::def::{self, Def};
use rustc::hir::def_id::DefId;
use rustc::middle::privacy::{AccessLevel, AccessLevels};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use ParentLink::{ModuleParentLink, BlockParentLink};
use Resolver;
use {resolve_error, resolve_struct_error, ResolutionError};

use rustc::middle::cstore::{CrateStore, ChildItem, DlDef};
use rustc::middle::cstore::{ChildItem, DlDef};
use rustc::lint;
use rustc::hir::def::*;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
Expand Down
10 changes: 10 additions & 0 deletions src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// resolve data structures and because it finalises the privacy information for
// `use` directives.
//
// Unused trait imports can't be checked until the method resolution. We save
// candidates here, and do the acutal check in librustc_typeck/check_unused.rs.

use std::ops::{Deref, DerefMut};

Expand Down Expand Up @@ -55,10 +57,18 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
fn check_import(&mut self, id: ast::NodeId, span: Span) {
if !self.used_imports.contains(&(id, TypeNS)) &&
!self.used_imports.contains(&(id, ValueNS)) {
if self.maybe_unused_trait_imports.contains(&id) {
// Check later.
return;
}
self.session.add_lint(lint::builtin::UNUSED_IMPORTS,
id,
span,
"unused import".to_string());
} else {
// This trait import is definitely used, in a way other than
// method resolution.
self.maybe_unused_trait_imports.remove(&id);
}
}
}
Expand Down
Loading