Add dead-code warning pass
This commit is contained in:
parent
49b751dda1
commit
c06dd0e0af
33 changed files with 572 additions and 53 deletions
|
@ -64,6 +64,7 @@ while cur < len(lines):
|
||||||
#[ allow(dead_assignment) ];\n
|
#[ allow(dead_assignment) ];\n
|
||||||
#[ allow(unused_mut) ];\n
|
#[ allow(unused_mut) ];\n
|
||||||
#[ allow(attribute_usage) ];\n
|
#[ allow(attribute_usage) ];\n
|
||||||
|
#[ allow(dead_code) ];\n
|
||||||
#[ feature(macro_rules, globs, struct_variant, managed_boxes) ];\n
|
#[ feature(macro_rules, globs, struct_variant, managed_boxes) ];\n
|
||||||
""" + block
|
""" + block
|
||||||
if xfail:
|
if xfail:
|
||||||
|
|
|
@ -310,6 +310,10 @@ pub fn phase_3_run_analysis_passes(sess: Session,
|
||||||
time(time_passes, "reachability checking", (), |_|
|
time(time_passes, "reachability checking", (), |_|
|
||||||
reachable::find_reachable(ty_cx, method_map, &exported_items));
|
reachable::find_reachable(ty_cx, method_map, &exported_items));
|
||||||
|
|
||||||
|
time(time_passes, "death checking", (), |_|
|
||||||
|
middle::dead::check_crate(ty_cx, method_map,
|
||||||
|
&exported_items, reachable_map, crate));
|
||||||
|
|
||||||
time(time_passes, "lint checking", (), |_|
|
time(time_passes, "lint checking", (), |_|
|
||||||
lint::check_crate(ty_cx, &exported_items, crate));
|
lint::check_crate(ty_cx, &exported_items, crate));
|
||||||
|
|
||||||
|
@ -510,19 +514,6 @@ pub fn pretty_print_input(sess: Session,
|
||||||
cfg: ast::CrateConfig,
|
cfg: ast::CrateConfig,
|
||||||
input: &input,
|
input: &input,
|
||||||
ppm: PpMode) {
|
ppm: PpMode) {
|
||||||
fn ann_typed_post(tcx: ty::ctxt, node: pprust::ann_node) {
|
|
||||||
match node {
|
|
||||||
pprust::node_expr(s, expr) => {
|
|
||||||
pp::space(s.s);
|
|
||||||
pp::word(s.s, "as");
|
|
||||||
pp::space(s.s);
|
|
||||||
pp::word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr)));
|
|
||||||
pprust::pclose(s);
|
|
||||||
}
|
|
||||||
_ => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let crate = phase_1_parse_input(sess, cfg.clone(), input);
|
let crate = phase_1_parse_input(sess, cfg.clone(), input);
|
||||||
|
|
||||||
let (crate, is_expanded) = match ppm {
|
let (crate, is_expanded) = match ppm {
|
||||||
|
|
|
@ -77,6 +77,7 @@ pub mod middle {
|
||||||
pub mod reachable;
|
pub mod reachable;
|
||||||
pub mod graph;
|
pub mod graph;
|
||||||
pub mod cfg;
|
pub mod cfg;
|
||||||
|
pub mod dead;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod front {
|
pub mod front {
|
||||||
|
|
352
src/librustc/middle/dead.rs
Normal file
352
src/librustc/middle/dead.rs
Normal file
|
@ -0,0 +1,352 @@
|
||||||
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
// This implements the dead-code warning pass. It follows middle::reachable
|
||||||
|
// closely. The idea is that all reachable symbols are live, codes called
|
||||||
|
// from live codes are live, and everything else is dead.
|
||||||
|
|
||||||
|
use middle::ty;
|
||||||
|
use middle::typeck;
|
||||||
|
use middle::privacy;
|
||||||
|
use middle::lint::dead_code;
|
||||||
|
|
||||||
|
use std::hashmap::HashSet;
|
||||||
|
use syntax::ast;
|
||||||
|
use syntax::ast_map;
|
||||||
|
use syntax::ast_util::{local_def, def_id_of_def, is_local};
|
||||||
|
use syntax::codemap;
|
||||||
|
use syntax::parse::token;
|
||||||
|
use syntax::visit::Visitor;
|
||||||
|
use syntax::visit;
|
||||||
|
|
||||||
|
// Any local node that may call something in its body block should be
|
||||||
|
// explored. For example, if it's a live node_item that is a
|
||||||
|
// function, then we should explore its block to check for codes that
|
||||||
|
// may need to be marked as live.
|
||||||
|
fn should_explore(tcx: ty::ctxt, def_id: ast::DefId) -> bool {
|
||||||
|
if !is_local(def_id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
match tcx.items.find(&def_id.node) {
|
||||||
|
Some(&ast_map::node_item(..))
|
||||||
|
| Some(&ast_map::node_method(..))
|
||||||
|
| Some(&ast_map::node_trait_method(..)) => true,
|
||||||
|
_ => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MarkSymbolVisitor {
|
||||||
|
worklist: ~[ast::NodeId],
|
||||||
|
method_map: typeck::method_map,
|
||||||
|
tcx: ty::ctxt,
|
||||||
|
live_symbols: ~HashSet<ast::NodeId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MarkSymbolVisitor {
|
||||||
|
fn new(tcx: ty::ctxt,
|
||||||
|
method_map: typeck::method_map,
|
||||||
|
worklist: ~[ast::NodeId]) -> MarkSymbolVisitor {
|
||||||
|
MarkSymbolVisitor {
|
||||||
|
worklist: worklist,
|
||||||
|
method_map: method_map,
|
||||||
|
tcx: tcx,
|
||||||
|
live_symbols: ~HashSet::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookup_and_handle_definition(&mut self, id: &ast::NodeId,
|
||||||
|
span: codemap::Span) {
|
||||||
|
let def = match self.tcx.def_map.find(id) {
|
||||||
|
Some(&def) => def,
|
||||||
|
None => self.tcx.sess.span_bug(span, "def ID not in def map?!"),
|
||||||
|
};
|
||||||
|
let def_id = match def {
|
||||||
|
ast::DefVariant(enum_id, _, _) => Some(enum_id),
|
||||||
|
ast::DefPrimTy(_) => None,
|
||||||
|
_ => Some(def_id_of_def(def)),
|
||||||
|
};
|
||||||
|
match def_id {
|
||||||
|
Some(def_id) => {
|
||||||
|
if should_explore(self.tcx, def_id) {
|
||||||
|
self.worklist.push(def_id.node);
|
||||||
|
}
|
||||||
|
self.live_symbols.insert(def_id.node);
|
||||||
|
}
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mark_live_symbols(&mut self) {
|
||||||
|
let mut scanned = HashSet::new();
|
||||||
|
while self.worklist.len() > 0 {
|
||||||
|
let id = self.worklist.pop();
|
||||||
|
if scanned.contains(&id) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
scanned.insert(id);
|
||||||
|
match self.tcx.items.find(&id) {
|
||||||
|
Some(node) => {
|
||||||
|
self.live_symbols.insert(id);
|
||||||
|
self.visit_node(node);
|
||||||
|
}
|
||||||
|
None => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_node(&mut self, node: &ast_map::ast_node) {
|
||||||
|
match *node {
|
||||||
|
ast_map::node_item(item, _) => {
|
||||||
|
match item.node {
|
||||||
|
ast::item_fn(..)
|
||||||
|
| ast::item_ty(..)
|
||||||
|
| ast::item_static(..)
|
||||||
|
| ast::item_foreign_mod(_) => {
|
||||||
|
visit::walk_item(self, item, ());
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ast_map::node_trait_method(trait_method, _, _) => {
|
||||||
|
visit::walk_trait_method(self, trait_method, ());
|
||||||
|
}
|
||||||
|
ast_map::node_method(method, _, _) => {
|
||||||
|
visit::walk_block(self, method.body, ());
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Visitor<()> for MarkSymbolVisitor {
|
||||||
|
|
||||||
|
fn visit_expr(&mut self, expr: @ast::Expr, _: ()) {
|
||||||
|
match expr.node {
|
||||||
|
ast::ExprPath(_) | ast::ExprStruct(..) => {
|
||||||
|
self.lookup_and_handle_definition(&expr.id, expr.span);
|
||||||
|
}
|
||||||
|
ast::ExprMethodCall(..) => {
|
||||||
|
match self.method_map.find(&expr.id) {
|
||||||
|
Some(&typeck::method_map_entry {
|
||||||
|
origin: typeck::method_static(def_id),
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if should_explore(self.tcx, def_id) {
|
||||||
|
self.worklist.push(def_id.node);
|
||||||
|
}
|
||||||
|
self.live_symbols.insert(def_id.node);
|
||||||
|
}
|
||||||
|
Some(_) => (),
|
||||||
|
None => {
|
||||||
|
self.tcx.sess.span_bug(expr.span,
|
||||||
|
"method call expression not \
|
||||||
|
in method map?!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
|
||||||
|
visit::walk_expr(self, expr, ())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_ty(&mut self, typ: &ast::Ty, _: ()) {
|
||||||
|
match typ.node {
|
||||||
|
ast::ty_path(_, _, ref id) => {
|
||||||
|
self.lookup_and_handle_definition(id, typ.span);
|
||||||
|
}
|
||||||
|
_ => visit::walk_ty(self, typ, ()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_item(&mut self, _item: @ast::item, _: ()) {
|
||||||
|
// Do not recurse into items. These items will be added to the
|
||||||
|
// worklist and recursed into manually if necessary.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This visitor is used to mark the implemented methods of a trait. Since we
|
||||||
|
// can not be sure if such methods are live or dead, we simply mark them
|
||||||
|
// as live.
|
||||||
|
struct TraitMethodSeeder {
|
||||||
|
worklist: ~[ast::NodeId],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Visitor<()> for TraitMethodSeeder {
|
||||||
|
fn visit_item(&mut self, item: @ast::item, _: ()) {
|
||||||
|
match item.node {
|
||||||
|
ast::item_impl(_, Some(ref _trait_ref), _, ref methods) => {
|
||||||
|
for method in methods.iter() {
|
||||||
|
self.worklist.push(method.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ast::item_mod(..) | ast::item_fn(..) => {
|
||||||
|
visit::walk_item(self, item, ());
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_and_seed_worklist(tcx: ty::ctxt,
|
||||||
|
exported_items: &privacy::ExportedItems,
|
||||||
|
reachable_symbols: &HashSet<ast::NodeId>,
|
||||||
|
crate: &ast::Crate) -> ~[ast::NodeId] {
|
||||||
|
let mut worklist = ~[];
|
||||||
|
|
||||||
|
// Preferably, we would only need to seed the worklist with reachable
|
||||||
|
// symbols. However, since the set of reachable symbols differs
|
||||||
|
// depending on whether a crate is built as bin or lib, and we want
|
||||||
|
// the warning to be consistent, we also seed the worklist with
|
||||||
|
// exported symbols.
|
||||||
|
for &id in exported_items.iter() {
|
||||||
|
worklist.push(id);
|
||||||
|
}
|
||||||
|
for &id in reachable_symbols.iter() {
|
||||||
|
worklist.push(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed entry point
|
||||||
|
match *tcx.sess.entry_fn {
|
||||||
|
Some((id, _)) => worklist.push(id),
|
||||||
|
None => ()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed implemeneted trait methods
|
||||||
|
let mut trait_method_seeder = TraitMethodSeeder {
|
||||||
|
worklist: worklist
|
||||||
|
};
|
||||||
|
visit::walk_crate(&mut trait_method_seeder, crate, ());
|
||||||
|
|
||||||
|
return trait_method_seeder.worklist;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_live(tcx: ty::ctxt,
|
||||||
|
method_map: typeck::method_map,
|
||||||
|
exported_items: &privacy::ExportedItems,
|
||||||
|
reachable_symbols: &HashSet<ast::NodeId>,
|
||||||
|
crate: &ast::Crate)
|
||||||
|
-> ~HashSet<ast::NodeId> {
|
||||||
|
let worklist = create_and_seed_worklist(tcx, exported_items,
|
||||||
|
reachable_symbols, crate);
|
||||||
|
let mut symbol_visitor = MarkSymbolVisitor::new(tcx, method_map, worklist);
|
||||||
|
symbol_visitor.mark_live_symbols();
|
||||||
|
symbol_visitor.live_symbols
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_warn(item: @ast::item) -> bool {
|
||||||
|
match item.node {
|
||||||
|
ast::item_static(..)
|
||||||
|
| ast::item_fn(..)
|
||||||
|
| ast::item_enum(..)
|
||||||
|
| ast::item_struct(..) => true,
|
||||||
|
_ => false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_struct_ctor_id(item: &ast::item) -> Option<ast::NodeId> {
|
||||||
|
match item.node {
|
||||||
|
ast::item_struct(struct_def, _) => struct_def.ctor_id,
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DeadVisitor {
|
||||||
|
tcx: ty::ctxt,
|
||||||
|
live_symbols: ~HashSet<ast::NodeId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeadVisitor {
|
||||||
|
// id := node id of an item's definition.
|
||||||
|
// ctor_id := `Some` if the item is a struct_ctor (tuple struct),
|
||||||
|
// `None` otherwise.
|
||||||
|
// If the item is a struct_ctor, then either its `id` or
|
||||||
|
// `ctor_id` (unwrapped) is in the live_symbols set. More specifically,
|
||||||
|
// DefMap maps the ExprPath of a struct_ctor to the node referred by
|
||||||
|
// `ctor_id`. On the other hand, in a statement like
|
||||||
|
// `type <ident> <generics> = <ty>;` where <ty> refers to a struct_ctor,
|
||||||
|
// DefMap maps <ty> to `id` instead.
|
||||||
|
fn symbol_is_live(&mut self, id: ast::NodeId,
|
||||||
|
ctor_id: Option<ast::NodeId>) -> bool {
|
||||||
|
if self.live_symbols.contains(&id)
|
||||||
|
|| ctor_id.map_default(false,
|
||||||
|
|ctor| self.live_symbols.contains(&ctor)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// If it's a type whose methods are live, then it's live, too.
|
||||||
|
// This is done to handle the case where, for example, the static
|
||||||
|
// method of a private type is used, but the type itself is never
|
||||||
|
// called directly.
|
||||||
|
let def_id = local_def(id);
|
||||||
|
match self.tcx.inherent_impls.find(&def_id) {
|
||||||
|
None => (),
|
||||||
|
Some(ref impl_list) => {
|
||||||
|
for impl_ in impl_list.iter() {
|
||||||
|
for method in impl_.methods.iter() {
|
||||||
|
if self.live_symbols.contains(&method.def_id.node) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Visitor<()> for DeadVisitor {
|
||||||
|
fn visit_item(&mut self, item: @ast::item, _: ()) {
|
||||||
|
let ctor_id = get_struct_ctor_id(item);
|
||||||
|
if !self.symbol_is_live(item.id, ctor_id) && should_warn(item) {
|
||||||
|
self.tcx.sess.add_lint(dead_code, item.id, item.span,
|
||||||
|
format!("code is never used: `{}`",
|
||||||
|
token::ident_to_str(&item.ident)));
|
||||||
|
}
|
||||||
|
visit::walk_item(self, item, ());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_fn(&mut self, fk: &visit::fn_kind,
|
||||||
|
_: &ast::fn_decl, block: ast::P<ast::Block>,
|
||||||
|
span: codemap::Span, id: ast::NodeId, _: ()) {
|
||||||
|
// Have to warn method here because methods are not ast::item
|
||||||
|
match *fk {
|
||||||
|
visit::fk_method(..) => {
|
||||||
|
let ident = visit::name_of_fn(fk);
|
||||||
|
if !self.symbol_is_live(id, None) {
|
||||||
|
self.tcx.sess
|
||||||
|
.add_lint(dead_code, id, span,
|
||||||
|
format!("code is never used: `{}`",
|
||||||
|
token::ident_to_str(&ident)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
visit::walk_block(self, block, ());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overwrite so that we don't warn the trait method itself.
|
||||||
|
fn visit_trait_method(&mut self, trait_method :&ast::trait_method, _: ()) {
|
||||||
|
match *trait_method {
|
||||||
|
ast::provided(method) => visit::walk_block(self, method.body, ()),
|
||||||
|
ast::required(_) => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_crate(tcx: ty::ctxt,
|
||||||
|
method_map: typeck::method_map,
|
||||||
|
exported_items: &privacy::ExportedItems,
|
||||||
|
reachable_symbols: &HashSet<ast::NodeId>,
|
||||||
|
crate: &ast::Crate) {
|
||||||
|
let live_symbols = find_live(tcx, method_map, exported_items,
|
||||||
|
reachable_symbols, crate);
|
||||||
|
let mut visitor = DeadVisitor { tcx: tcx, live_symbols: live_symbols };
|
||||||
|
visit::walk_crate(&mut visitor, crate, ());
|
||||||
|
}
|
|
@ -88,6 +88,7 @@ pub enum lint {
|
||||||
dead_assignment,
|
dead_assignment,
|
||||||
unused_mut,
|
unused_mut,
|
||||||
unnecessary_allocation,
|
unnecessary_allocation,
|
||||||
|
dead_code,
|
||||||
|
|
||||||
missing_doc,
|
missing_doc,
|
||||||
unreachable_code,
|
unreachable_code,
|
||||||
|
@ -282,6 +283,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
|
||||||
default: warn
|
default: warn
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
("dead_code",
|
||||||
|
LintSpec {
|
||||||
|
lint: dead_code,
|
||||||
|
desc: "detect piece of code that will never be used",
|
||||||
|
default: warn
|
||||||
|
}),
|
||||||
|
|
||||||
("missing_doc",
|
("missing_doc",
|
||||||
LintSpec {
|
LintSpec {
|
||||||
lint: missing_doc,
|
lint: missing_doc,
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use ast::{P, Block, Crate, DeclLocal, Expr_, ExprMac, SyntaxContext};
|
use ast::{P, Block, Crate, DeclLocal, ExprMac, SyntaxContext};
|
||||||
use ast::{Local, Ident, mac_invoc_tt};
|
use ast::{Local, Ident, mac_invoc_tt};
|
||||||
use ast::{item_mac, Mrk, Stmt, StmtDecl, StmtMac, StmtExpr, StmtSemi};
|
use ast::{item_mac, Mrk, Stmt, StmtDecl, StmtMac, StmtExpr, StmtSemi};
|
||||||
use ast::{token_tree};
|
use ast::{token_tree};
|
||||||
|
@ -21,7 +21,6 @@ use codemap;
|
||||||
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
|
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
|
||||||
use ext::base::*;
|
use ext::base::*;
|
||||||
use fold::*;
|
use fold::*;
|
||||||
use opt_vec;
|
|
||||||
use parse;
|
use parse;
|
||||||
use parse::{parse_item_from_source_str};
|
use parse::{parse_item_from_source_str};
|
||||||
use parse::token;
|
use parse::token;
|
||||||
|
@ -140,29 +139,6 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
|
||||||
|
|
||||||
let span = e.span;
|
let span = e.span;
|
||||||
|
|
||||||
fn mk_expr(_: @ExtCtxt, span: Span, node: Expr_)
|
|
||||||
-> @ast::Expr {
|
|
||||||
@ast::Expr {
|
|
||||||
id: ast::DUMMY_NODE_ID,
|
|
||||||
node: node,
|
|
||||||
span: span,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mk_simple_path(ident: ast::Ident, span: Span) -> ast::Path {
|
|
||||||
ast::Path {
|
|
||||||
span: span,
|
|
||||||
global: false,
|
|
||||||
segments: ~[
|
|
||||||
ast::PathSegment {
|
|
||||||
identifier: ident,
|
|
||||||
lifetimes: opt_vec::Empty,
|
|
||||||
types: opt_vec::Empty,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// to:
|
// to:
|
||||||
//
|
//
|
||||||
// {
|
// {
|
||||||
|
@ -714,14 +690,6 @@ pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> @ast_fold {
|
||||||
} as @ast_fold
|
} as @ast_fold
|
||||||
}
|
}
|
||||||
|
|
||||||
// perform a bunch of renames
|
|
||||||
fn apply_pending_renames(folder : @ast_fold, stmt : ast::Stmt) -> @ast::Stmt {
|
|
||||||
folder.fold_stmt(&stmt)
|
|
||||||
.expect_one("renaming of stmt did not produce one stmt")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn new_span(cx: @ExtCtxt, sp: Span) -> Span {
|
pub fn new_span(cx: @ExtCtxt, sp: Span) -> Span {
|
||||||
/* this discards information in the case of macro-defining macros */
|
/* this discards information in the case of macro-defining macros */
|
||||||
Span {
|
Span {
|
||||||
|
@ -739,6 +707,7 @@ pub fn std_macros() -> @str {
|
||||||
@r#"mod __std_macros {
|
@r#"mod __std_macros {
|
||||||
#[macro_escape];
|
#[macro_escape];
|
||||||
#[doc(hidden)];
|
#[doc(hidden)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
macro_rules! ignore (($($x:tt)*) => (()))
|
macro_rules! ignore (($($x:tt)*) => (()))
|
||||||
|
|
||||||
|
@ -900,6 +869,7 @@ pub fn std_macros() -> @str {
|
||||||
mod $c {
|
mod $c {
|
||||||
#[allow(unused_imports)];
|
#[allow(unused_imports)];
|
||||||
#[allow(non_uppercase_statics)];
|
#[allow(non_uppercase_statics)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
@ -979,12 +949,6 @@ pub fn inject_std_macros(parse_sess: @mut parse::ParseSess,
|
||||||
injector.fold_crate(c)
|
injector.fold_crate(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct NoOpFolder {
|
|
||||||
contents: (),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ast_fold for NoOpFolder {}
|
|
||||||
|
|
||||||
pub struct MacroExpander {
|
pub struct MacroExpander {
|
||||||
extsbox: @mut SyntaxEnv,
|
extsbox: @mut SyntaxEnv,
|
||||||
cx: @ExtCtxt,
|
cx: @ExtCtxt,
|
||||||
|
|
|
@ -331,7 +331,12 @@ impl Context {
|
||||||
let unnamed = self.ecx.meta_word(self.fmtsp, @"address_insignificant");
|
let unnamed = self.ecx.meta_word(self.fmtsp, @"address_insignificant");
|
||||||
let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
|
let unnamed = self.ecx.attribute(self.fmtsp, unnamed);
|
||||||
|
|
||||||
return ~[unnamed];
|
// Do not warn format string as dead code
|
||||||
|
let dead_code = self.ecx.meta_word(self.fmtsp, @"dead_code");
|
||||||
|
let allow_dead_code = self.ecx.meta_list(self.fmtsp,
|
||||||
|
@"allow", ~[dead_code]);
|
||||||
|
let allow_dead_code = self.ecx.attribute(self.fmtsp, allow_dead_code);
|
||||||
|
return ~[unnamed, allow_dead_code];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Translate a `parse::Piece` to a static `rt::Piece`
|
/// Translate a `parse::Piece` to a static `rt::Piece`
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#[deny(unreachable_code)];
|
#[deny(unreachable_code)];
|
||||||
#[allow(unused_variable)];
|
#[allow(unused_variable)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
fn fail_len(v: ~[int]) -> uint {
|
fn fail_len(v: ~[int]) -> uint {
|
||||||
let mut i = 3;
|
let mut i = 3;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
// Matching against NaN should result in a warning
|
// Matching against NaN should result in a warning
|
||||||
|
|
||||||
use std::f64::NAN;
|
use std::f64::NAN;
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[deny(unreachable_code)];
|
#[deny(unreachable_code)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
pub unsafe fn g() {
|
pub unsafe fn g() {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[deny(warnings)];
|
#[deny(warnings)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
while true {} //~ ERROR: infinite
|
while true {} //~ ERROR: infinite
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[deny(ctypes)];
|
#[deny(ctypes)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
enum Z { }
|
enum Z { }
|
||||||
enum U { A }
|
enum U { A }
|
||||||
|
|
69
src/test/compile-fail/lint-dead-code-1.rs
Normal file
69
src/test/compile-fail/lint-dead-code-1.rs
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
#[allow(unused_variable)];
|
||||||
|
#[deny(dead_code)];
|
||||||
|
|
||||||
|
#[crate_type="lib"];
|
||||||
|
|
||||||
|
pub use foo2::Bar2;
|
||||||
|
mod foo {
|
||||||
|
pub struct Bar; //~ ERROR: code is never used
|
||||||
|
}
|
||||||
|
|
||||||
|
mod foo2 {
|
||||||
|
pub struct Bar2;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub static pub_static: int = 0;
|
||||||
|
static priv_static: int = 0; //~ ERROR: code is never used
|
||||||
|
static used_static: int = 0;
|
||||||
|
pub static used_static2: int = used_static;
|
||||||
|
|
||||||
|
pub fn pub_fn() {
|
||||||
|
used_fn();
|
||||||
|
let used_struct1 = UsedStruct1 { x: 1 };
|
||||||
|
let used_struct2 = UsedStruct2(1);
|
||||||
|
let used_struct3 = UsedStruct3;
|
||||||
|
let e = foo3;
|
||||||
|
SemiUsedStruct::la_la_la();
|
||||||
|
|
||||||
|
}
|
||||||
|
fn priv_fn() { //~ ERROR: code is never used
|
||||||
|
let unused_struct = PrivStruct;
|
||||||
|
}
|
||||||
|
fn used_fn() {}
|
||||||
|
|
||||||
|
pub type typ = ~UsedStruct4;
|
||||||
|
pub struct PubStruct();
|
||||||
|
struct PrivStruct; //~ ERROR: code is never used
|
||||||
|
struct UsedStruct1 { x: int }
|
||||||
|
struct UsedStruct2(int);
|
||||||
|
struct UsedStruct3;
|
||||||
|
struct UsedStruct4;
|
||||||
|
// this struct is never used directly, but its method is, so we don't want
|
||||||
|
// to warn it
|
||||||
|
struct SemiUsedStruct;
|
||||||
|
impl SemiUsedStruct {
|
||||||
|
fn la_la_la() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum pub_enum { foo1, bar1 }
|
||||||
|
enum priv_enum { foo2, bar2 } //~ ERROR: code is never used
|
||||||
|
enum used_enum { foo3, bar3 }
|
||||||
|
|
||||||
|
fn foo() { //~ ERROR: code is never used
|
||||||
|
bar();
|
||||||
|
let unused_enum = foo2;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar() { //~ ERROR: code is never used
|
||||||
|
foo();
|
||||||
|
}
|
50
src/test/compile-fail/lint-dead-code-2.rs
Normal file
50
src/test/compile-fail/lint-dead-code-2.rs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
#[allow(unused_variable)];
|
||||||
|
#[deny(dead_code)];
|
||||||
|
|
||||||
|
struct Foo;
|
||||||
|
|
||||||
|
trait Bar {
|
||||||
|
fn bar1(&self);
|
||||||
|
fn bar2(&self) {
|
||||||
|
self.bar1();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Bar for Foo {
|
||||||
|
fn bar1(&self) {
|
||||||
|
live_fn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn live_fn() {}
|
||||||
|
|
||||||
|
fn dead_fn() {} //~ ERROR: code is never used
|
||||||
|
|
||||||
|
#[main]
|
||||||
|
fn dead_fn2() {} //~ ERROR: code is never used
|
||||||
|
|
||||||
|
fn used_fn() {}
|
||||||
|
|
||||||
|
#[start]
|
||||||
|
fn start(_: int, _: **u8) -> int {
|
||||||
|
used_fn();
|
||||||
|
let foo = Foo;
|
||||||
|
foo.bar2();
|
||||||
|
0
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is not main
|
||||||
|
fn main() { //~ ERROR: code is never used
|
||||||
|
dead_fn();
|
||||||
|
dead_fn2();
|
||||||
|
}
|
50
src/test/compile-fail/lint-dead-code-3.rs
Normal file
50
src/test/compile-fail/lint-dead-code-3.rs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
#[allow(unused_variable)];
|
||||||
|
#[deny(dead_code)];
|
||||||
|
|
||||||
|
#[crate_type="lib"];
|
||||||
|
|
||||||
|
struct Foo; //~ ERROR: code is never used
|
||||||
|
impl Foo {
|
||||||
|
fn foo(&self) { //~ ERROR: code is never used
|
||||||
|
bar()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar() { //~ ERROR: code is never used
|
||||||
|
fn baz() {} //~ ERROR: code is never used
|
||||||
|
|
||||||
|
Foo.foo();
|
||||||
|
baz();
|
||||||
|
}
|
||||||
|
|
||||||
|
// no warning
|
||||||
|
struct Foo2;
|
||||||
|
impl Foo2 { fn foo2(&self) { bar2() } }
|
||||||
|
fn bar2() {
|
||||||
|
fn baz2() {}
|
||||||
|
|
||||||
|
Foo2.foo2();
|
||||||
|
baz2();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pub_fn() {
|
||||||
|
let foo2_struct = Foo2;
|
||||||
|
foo2_struct.foo2();
|
||||||
|
}
|
||||||
|
|
||||||
|
// not warned because it's used in the parameter of `free` below
|
||||||
|
enum c_void {}
|
||||||
|
|
||||||
|
extern {
|
||||||
|
fn free(p: *c_void);
|
||||||
|
}
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#[feature(managed_boxes)];
|
#[feature(managed_boxes)];
|
||||||
#[forbid(heap_memory)];
|
#[forbid(heap_memory)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
struct Foo {
|
struct Foo {
|
||||||
x: @int //~ ERROR type uses managed
|
x: @int //~ ERROR type uses managed
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[allow(while_true)];
|
#[allow(while_true)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
struct A(int);
|
struct A(int);
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#[feature(struct_variant)];
|
#[feature(struct_variant)];
|
||||||
#[feature(globs)];
|
#[feature(globs)];
|
||||||
#[deny(missing_doc)];
|
#[deny(missing_doc)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
//! Some garbage docs for the crate here
|
//! Some garbage docs for the crate here
|
||||||
#[doc="More garbage"];
|
#[doc="More garbage"];
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[forbid(non_camel_case_types)];
|
#[forbid(non_camel_case_types)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
struct foo { //~ ERROR type `foo` should have a camel case identifier
|
struct foo { //~ ERROR type `foo` should have a camel case identifier
|
||||||
bar: int,
|
bar: int,
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[forbid(non_uppercase_statics)];
|
#[forbid(non_uppercase_statics)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
static foo: int = 1; //~ ERROR static constant should have an uppercase identifier
|
static foo: int = 1; //~ ERROR static constant should have an uppercase identifier
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
// injected intrinsics by the compiler.
|
// injected intrinsics by the compiler.
|
||||||
|
|
||||||
#[deny(attribute_usage)];
|
#[deny(attribute_usage)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
#[abi="stdcall"] extern {} //~ ERROR: obsolete attribute
|
#[abi="stdcall"] extern {} //~ ERROR: obsolete attribute
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#[deny(unstable)];
|
#[deny(unstable)];
|
||||||
#[deny(deprecated)];
|
#[deny(deprecated)];
|
||||||
#[deny(experimental)];
|
#[deny(experimental)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
mod cross_crate {
|
mod cross_crate {
|
||||||
extern mod lint_stability;
|
extern mod lint_stability;
|
||||||
|
|
|
@ -8,6 +8,8 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
// compile-flags: -D type-limits
|
// compile-flags: -D type-limits
|
||||||
fn main() { }
|
fn main() { }
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[allow(unused_unsafe)];
|
#[allow(unused_unsafe)];
|
||||||
|
#[allow(dead_code)];
|
||||||
#[deny(unsafe_block)];
|
#[deny(unsafe_block)];
|
||||||
#[feature(macro_rules)];
|
#[feature(macro_rules)];
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#[feature(globs)];
|
#[feature(globs)];
|
||||||
#[deny(unused_imports)];
|
#[deny(unused_imports)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
mod A {
|
mod A {
|
||||||
pub fn p() {}
|
pub fn p() {}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#[deny(unused_imports)];
|
#[deny(unused_imports)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
// Regression test for issue #6633
|
// Regression test for issue #6633
|
||||||
mod issue6633 {
|
mod issue6633 {
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#[feature(globs)];
|
#[feature(globs)];
|
||||||
#[deny(unused_imports)];
|
#[deny(unused_imports)];
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
use cal = bar::c::cc;
|
use cal = bar::c::cc;
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
#[allow(dead_assignment)];
|
#[allow(dead_assignment)];
|
||||||
#[allow(unused_variable)];
|
#[allow(unused_variable)];
|
||||||
|
#[allow(dead_code)];
|
||||||
#[deny(unused_mut)];
|
#[deny(unused_mut)];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
// Exercise the unused_unsafe attribute in some positive and negative cases
|
// Exercise the unused_unsafe attribute in some positive and negative cases
|
||||||
|
|
||||||
|
#[allow(dead_code)];
|
||||||
#[deny(unused_unsafe)];
|
#[deny(unused_unsafe)];
|
||||||
|
|
||||||
mod foo {
|
mod foo {
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
|
#[allow(dead_code)];
|
||||||
#[deny(dead_assignment)];
|
#[deny(dead_assignment)];
|
||||||
|
|
||||||
fn f1(x: &mut int) {
|
fn f1(x: &mut int) {
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
// Issue #7526: lowercase static constants in patterns look like bindings
|
// Issue #7526: lowercase static constants in patterns look like bindings
|
||||||
|
|
||||||
|
#[allow(dead_code)];
|
||||||
#[deny(non_uppercase_pattern_statics)];
|
#[deny(non_uppercase_pattern_statics)];
|
||||||
|
|
||||||
pub static a : int = 97;
|
pub static a : int = 97;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
#[static_assert]
|
#[static_assert]
|
||||||
static A: bool = false; //~ ERROR static assertion failed
|
static A: bool = false; //~ ERROR static assertion failed
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#[allow(dead_code)];
|
||||||
|
|
||||||
#[static_assert]
|
#[static_assert]
|
||||||
static E: bool = 1 == 2; //~ ERROR static assertion failed
|
static E: bool = 1 == 2; //~ ERROR static assertion failed
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue