Skip to content

Commit b50c7a5

Browse files
committed
Implement unused features check
1 parent a866c4e commit b50c7a5

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/librustc/lint/builtin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,9 +1734,9 @@ declare_lint! {
17341734
}
17351735

17361736
declare_lint! {
1737-
pub UNKNOWN_FEATURES,
1737+
pub UNUSED_FEATURES,
17381738
Deny,
1739-
"unknown features found in crate-level #[feature] directives"
1739+
"unused or unknown features found in crate-level #[feature] directives"
17401740
}
17411741

17421742
declare_lint! {
@@ -1780,7 +1780,7 @@ impl LintPass for HardwiredLints {
17801780
DEAD_CODE,
17811781
UNREACHABLE_CODE,
17821782
WARNINGS,
1783-
UNKNOWN_FEATURES,
1783+
UNUSED_FEATURES,
17841784
UNKNOWN_CRATE_TYPES,
17851785
VARIANT_SIZE_DIFFERENCES,
17861786
FAT_PTR_TRANSMUTES

src/librustc/lint/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ impl LintStore {
257257
self.register_renamed("transmute_fat_ptr", "fat_ptr_transmutes");
258258
self.register_renamed("raw_pointer_deriving", "raw_pointer_derive");
259259

260+
self.register_renamed("unknown_features", "unused_features");
260261
}
261262

262263
#[allow(unused_variables)]
@@ -829,6 +830,5 @@ pub fn check_crate(tcx: &ty::ctxt,
829830
}
830831
}
831832

832-
tcx.sess.abort_if_errors();
833833
*tcx.node_lint_levels.borrow_mut() = cx.node_levels.into_inner();
834834
}

src/librustc/middle/stability.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//! propagating default levels lexically from parent to children ast nodes.
1313
1414
use session::Session;
15+
use lint;
1516
use middle::ty;
1617
use metadata::csearch;
1718
use syntax::parse::token::InternedString;
@@ -25,7 +26,7 @@ use syntax::ast_util::is_local;
2526
use syntax::attr::{Stability, AttrMetaMethods};
2627
use syntax::visit::{FnKind, FkMethod, Visitor};
2728
use syntax::feature_gate::emit_feature_warn;
28-
use util::nodemap::{NodeMap, DefIdMap, FnvHashSet};
29+
use util::nodemap::{NodeMap, DefIdMap, FnvHashSet, FnvHashMap};
2930
use util::ppaux::Repr;
3031

3132
use std::mem::replace;
@@ -418,13 +419,20 @@ pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
418419
/// Given the list of enabled features that were not language features (i.e. that
419420
/// were expected to be library features), and the list of features used from
420421
/// libraries, identify activated features that don't exist and error about them.
421-
pub fn check_unknown_features(sess: &Session,
422-
_used_lib_features: &FnvHashSet<InternedString>) {
423-
let ref _lib_features = sess.features.borrow().lib_features;
424-
// TODO
425-
426-
//sess.add_lint(lint::builtin::UNKNOWN_FEATURES,
427-
// ast::CRATE_NODE_ID,
428-
// *uf,
429-
// "unknown feature".to_string());
422+
pub fn check_unused_features(sess: &Session,
423+
used_lib_features: &FnvHashSet<InternedString>) {
424+
let ref lib_features = sess.features.borrow().lib_features;
425+
let mut active_lib_features: FnvHashMap<InternedString, Span>
426+
= lib_features.clone().into_iter().collect();
427+
428+
for used_feature in used_lib_features.iter() {
429+
active_lib_features.remove(used_feature);
430+
}
431+
432+
for (_, &span) in active_lib_features.iter() {
433+
sess.add_lint(lint::builtin::UNUSED_FEATURES,
434+
ast::CRATE_NODE_ID,
435+
span,
436+
"unused or unknown feature".to_string());
437+
}
430438
}

src/librustc_driver/driver.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,14 +668,14 @@ pub fn phase_3_run_analysis_passes<'tcx>(sess: Session,
668668
time(time_passes, "stability checking", (), |_|
669669
stability::check_unstable_api_usage(&ty_cx));
670670

671-
time(time_passes, "unknown feature checking", (), |_|
672-
stability::check_unknown_features(
671+
time(time_passes, "unused feature checking", (), |_|
672+
stability::check_unused_features(
673673
&ty_cx.sess, lib_features_used));
674674

675675
time(time_passes, "lint checking", (), |_|
676676
lint::check_crate(&ty_cx, &exported_items));
677677

678-
// Some of the above passes generate errors
678+
// The above three passes generate errors w/o aborting
679679
ty_cx.sess.abort_if_errors();
680680

681681
ty::CrateAnalysis {

0 commit comments

Comments
 (0)