]> git.lizzy.rs Git - rust.git/commitdiff
Add dead-code warning pass
authorKiet Tran <ktt3ja@gmail.com>
Sun, 8 Dec 2013 07:55:27 +0000 (02:55 -0500)
committerKiet Tran <ktt3ja@gmail.com>
Sun, 8 Dec 2013 07:55:27 +0000 (02:55 -0500)
33 files changed:
src/etc/extract-tests.py
src/librustc/driver/driver.rs
src/librustc/lib.rs
src/librustc/middle/dead.rs [new file with mode: 0644]
src/librustc/middle/lint.rs
src/libsyntax/ext/expand.rs
src/libsyntax/ext/format.rs
src/test/compile-fail/issue-2150.rs
src/test/compile-fail/issue-6804.rs
src/test/compile-fail/issue-7246.rs
src/test/compile-fail/lint-change-warnings.rs
src/test/compile-fail/lint-ctypes-enum.rs
src/test/compile-fail/lint-dead-code-1.rs [new file with mode: 0644]
src/test/compile-fail/lint-dead-code-2.rs [new file with mode: 0644]
src/test/compile-fail/lint-dead-code-3.rs [new file with mode: 0644]
src/test/compile-fail/lint-heap-memory.rs
src/test/compile-fail/lint-impl-fn.rs
src/test/compile-fail/lint-missing-doc.rs
src/test/compile-fail/lint-non-camel-case-types.rs
src/test/compile-fail/lint-non-uppercase-statics.rs
src/test/compile-fail/lint-obsolete-attr.rs
src/test/compile-fail/lint-stability.rs
src/test/compile-fail/lint-type-limits.rs
src/test/compile-fail/lint-unsafe-block.rs
src/test/compile-fail/lint-unused-import-tricky-globs.rs
src/test/compile-fail/lint-unused-import-tricky-names.rs
src/test/compile-fail/lint-unused-imports.rs
src/test/compile-fail/lint-unused-mut-variables.rs
src/test/compile-fail/lint-unused-unsafe.rs
src/test/compile-fail/liveness-dead.rs
src/test/compile-fail/match-static-const-lc.rs
src/test/compile-fail/static-assert.rs
src/test/compile-fail/static-assert2.rs

index 5904e10a08dbae7a013285a33b14c46aebb48713..ab2556f24c6d3d66e71dedfd320ae247d9be4c6a 100644 (file)
@@ -64,6 +64,7 @@ while cur < len(lines):
 #[ allow(dead_assignment) ];\n
 #[ allow(unused_mut) ];\n
 #[ allow(attribute_usage) ];\n
+#[ allow(dead_code) ];\n
 #[ feature(macro_rules, globs, struct_variant, managed_boxes) ];\n
 """ + block
             if xfail:
index 63d6c60d2698d07fae13ef37818960d58c6e668c..e08726ad8a61cdef406183b62391353d5b37d878 100644 (file)
@@ -310,6 +310,10 @@ pub fn phase_3_run_analysis_passes(sess: Session,
         time(time_passes, "reachability checking", (), |_|
              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", (), |_|
          lint::check_crate(ty_cx, &exported_items, crate));
 
@@ -510,19 +514,6 @@ pub fn pretty_print_input(sess: Session,
                           cfg: ast::CrateConfig,
                           input: &input,
                           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, is_expanded) = match ppm {
index 7484550b3f3fd4e4882b1bd93d183c133a10712c..2185617c79f0d38d497b71a582afdda4964f8e82 100644 (file)
@@ -77,6 +77,7 @@ pub mod middle {
     pub mod reachable;
     pub mod graph;
     pub mod cfg;
+    pub mod dead;
 }
 
 pub mod front {
diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs
new file mode 100644 (file)
index 0000000..2327425
--- /dev/null
@@ -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, ());
+}
index 942d9b957ae320d1b0f93f9bdf8282edff2c982e..3e6803feadbfd39de1e9cb28171651078727bd8f 100644 (file)
@@ -88,6 +88,7 @@ pub enum lint {
     dead_assignment,
     unused_mut,
     unnecessary_allocation,
+    dead_code,
 
     missing_doc,
     unreachable_code,
@@ -282,6 +283,13 @@ enum LintSource {
         default: warn
     }),
 
+    ("dead_code",
+     LintSpec {
+        lint: dead_code,
+        desc: "detect piece of code that will never be used",
+        default: warn
+    }),
+
     ("missing_doc",
      LintSpec {
         lint: missing_doc,
index 4bb35457182f75db8cd1d942d43bb652e02f6abc..a6e45c7e1bbb38d923a7b097dc8f55d217564ebe 100644 (file)
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // 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::{item_mac, Mrk, Stmt, StmtDecl, StmtMac, StmtExpr, StmtSemi};
 use ast::{token_tree};
@@ -21,7 +21,6 @@
 use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
 use ext::base::*;
 use fold::*;
-use opt_vec;
 use parse;
 use parse::{parse_item_from_source_str};
 use parse::token;
@@ -140,29 +139,6 @@ pub fn expand_expr(extsbox: @mut SyntaxEnv,
 
             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:
             //
             // {
@@ -714,14 +690,6 @@ pub fn renames_to_fold(renames: @mut ~[(ast::Ident,ast::Name)]) -> @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 {
     /* this discards information in the case of macro-defining macros */
     Span {
@@ -739,6 +707,7 @@ pub fn std_macros() -> @str {
 @r#"mod __std_macros {
     #[macro_escape];
     #[doc(hidden)];
+    #[allow(dead_code)];
 
     macro_rules! ignore (($($x:tt)*) => (()))
 
@@ -900,6 +869,7 @@ pub mod $c {
             mod $c {
                 #[allow(unused_imports)];
                 #[allow(non_uppercase_statics)];
+                #[allow(dead_code)];
 
                 use super::*;
 
@@ -979,12 +949,6 @@ pub fn inject_std_macros(parse_sess: @mut parse::ParseSess,
     injector.fold_crate(c)
 }
 
-struct NoOpFolder {
-    contents: (),
-}
-
-impl ast_fold for NoOpFolder {}
-
 pub struct MacroExpander {
     extsbox: @mut SyntaxEnv,
     cx: @ExtCtxt,
index f6fb521ff7cd8ee285e00e611ae92504d158c15c..9193a9cee17d23009229b2b555a989974c0d4459 100644 (file)
@@ -331,7 +331,12 @@ fn static_attrs(&self) -> ~[ast::Attribute] {
         let unnamed = self.ecx.meta_word(self.fmtsp, @"address_insignificant");
         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`
index 64344ab4277939605bdaea6b4c13063dab6fa0fd..2c54b622021153c44ebf5878a54a0d4655f0f44d 100644 (file)
@@ -10,6 +10,7 @@
 
 #[deny(unreachable_code)];
 #[allow(unused_variable)];
+#[allow(dead_code)];
 
 fn fail_len(v: ~[int]) -> uint {
     let mut i = 3;
index c2e0c40d8ca2f4201a79a55f3bec2be27f785e99..23d9f3199c1779eac9ffc22994c2957f23367355 100644 (file)
@@ -1,3 +1,5 @@
+#[allow(dead_code)];
+
 // Matching against NaN should result in a warning
 
 use std::f64::NAN;
index dacc31a573accabedfb1eb39e284cc7ef45cbeb3..ce31ac2e8fa0a3bc7305b5f4843816c91fccd7c4 100644 (file)
@@ -9,6 +9,8 @@
 // except according to those terms.
 
 #[deny(unreachable_code)];
+#[allow(dead_code)];
+
 use std::ptr;
 pub unsafe fn g() {
     return; 
index 977abc4dc0d12d7edd9622b9160ef3b533539659..e9985430adfa5858e3b304657e81de86460c6df3 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[deny(warnings)];
+#[allow(dead_code)];
 
 fn main() {
     while true {} //~ ERROR: infinite
index 857e3bb4b8d83658571792eb73cd8a6ede7f4aa8..2e47695f0fc0fa1a9ff1ed1e5cca73eaf0ccc655 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[deny(ctypes)];
+#[allow(dead_code)];
 
 enum Z { }
 enum U { A }
diff --git a/src/test/compile-fail/lint-dead-code-1.rs b/src/test/compile-fail/lint-dead-code-1.rs
new file mode 100644 (file)
index 0000000..7d7cbce
--- /dev/null
@@ -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();
+}
diff --git a/src/test/compile-fail/lint-dead-code-2.rs b/src/test/compile-fail/lint-dead-code-2.rs
new file mode 100644 (file)
index 0000000..663e789
--- /dev/null
@@ -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();
+}
diff --git a/src/test/compile-fail/lint-dead-code-3.rs b/src/test/compile-fail/lint-dead-code-3.rs
new file mode 100644 (file)
index 0000000..8a5f239
--- /dev/null
@@ -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);
+}
index fadda4173a6afd9eb52bd206c5fe8b623744585f..b550c227898f29361c556c4b5509d61d0ae469e7 100644 (file)
@@ -10,6 +10,7 @@
 
 #[feature(managed_boxes)];
 #[forbid(heap_memory)];
+#[allow(dead_code)];
 
 struct Foo {
     x: @int //~ ERROR type uses managed
index 3cc0495206d98e52417f45607c4f5cc31dd35229..ad35a22b4efa0e2b9407d67afcd886ddc7add2ac 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[allow(while_true)];
+#[allow(dead_code)];
 
 struct A(int);
 
index 12d9cbe0dbc5c741b439c261a33cae4108fd0e6e..a083948bf8406786fb4d80e93470d32c58ab82c2 100644 (file)
@@ -13,6 +13,7 @@
 #[feature(struct_variant)];
 #[feature(globs)];
 #[deny(missing_doc)];
+#[allow(dead_code)];
 
 //! Some garbage docs for the crate here
 #[doc="More garbage"];
index 2cabdfe5bb0981be0fd6ff0fa3d6850da5598a76..6d217656cd01039768f9e1ffd16f62795d60bc98 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[forbid(non_camel_case_types)];
+#[allow(dead_code)];
 
 struct foo { //~ ERROR type `foo` should have a camel case identifier
     bar: int,
index 4da4d3ada384eb20b2dfb1927a88eb451513d831..d41a4ccda8d120aa4cb0c6cb7cdda51e44ed61a9 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[forbid(non_uppercase_statics)];
+#[allow(dead_code)];
 
 static foo: int = 1; //~ ERROR static constant should have an uppercase identifier
 
index 919769783152dd1d2c89e19a8487dbd6483b49b0..442bcaa0923140bed7780a6e1a7e1b49aa6b5170 100644 (file)
@@ -12,6 +12,7 @@
 // injected intrinsics by the compiler.
 
 #[deny(attribute_usage)];
+#[allow(dead_code)];
 
 #[abi="stdcall"] extern {} //~ ERROR: obsolete attribute
 
index 1046a638ff9473bbe8c45580d60c2c1a08f4bebc..9cc06cc5395bf1cec7ee44a7831f7a3de59a498c 100644 (file)
@@ -15,6 +15,7 @@
 #[deny(unstable)];
 #[deny(deprecated)];
 #[deny(experimental)];
+#[allow(dead_code)];
 
 mod cross_crate {
     extern mod lint_stability;
index 08714e3a044b45733ae31184809b03e133b910c0..f609debb5bd8fc881fd6ccfd50a69470ba58cf9b 100644 (file)
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[allow(dead_code)];
+
 // compile-flags: -D type-limits
 fn main() { }
 
index 7f3075e90bc7b6b268dbf240c6fd24dfd570adc8..529d3e921a069e23fc071ec993b2aa4aab9b6678 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[allow(unused_unsafe)];
+#[allow(dead_code)];
 #[deny(unsafe_block)];
 #[feature(macro_rules)];
 
index 85edbd1d147ccae9c7dd6ef77a21c19c89b38296..9d6140b8fd233cc20dccdc87ae823d63882b6aa9 100644 (file)
@@ -10,6 +10,7 @@
 
 #[feature(globs)];
 #[deny(unused_imports)];
+#[allow(dead_code)];
 
 mod A {
     pub fn p() {}
index 0347c673779446ce68aa575d2016aac0ff42f156..24511296a0b12bb2b0395ec7afdbd249656ddcc4 100644 (file)
@@ -9,6 +9,7 @@
 // except according to those terms.
 
 #[deny(unused_imports)];
+#[allow(dead_code)];
 
 // Regression test for issue #6633
 mod issue6633 {
index 9bb58453539753e93fa67eb0979f8fc20a3ab808..e248184d5e2004f0ada2a342c65e94160d0ca209 100644 (file)
@@ -10,6 +10,7 @@
 
 #[feature(globs)];
 #[deny(unused_imports)];
+#[allow(dead_code)];
 
 use cal = bar::c::cc;
 
index 798c1194af8723cf61ad7598868c7d452f5b4b05..271aedd3f6a6ba001814e618c278e2ffe5bb350d 100644 (file)
@@ -12,6 +12,7 @@
 
 #[allow(dead_assignment)];
 #[allow(unused_variable)];
+#[allow(dead_code)];
 #[deny(unused_mut)];
 
 fn main() {
index f28322d3bf76321b4fae5dd14637043a77d65b7f..96a4c2adca32c93df98dbd3bce38d07664adaa61 100644 (file)
@@ -10,6 +10,7 @@
 
 // Exercise the unused_unsafe attribute in some positive and negative cases
 
+#[allow(dead_code)];
 #[deny(unused_unsafe)];
 
 mod foo {
index df78b25187bf2304f257ee542a4bc650c8e9f35b..a3d388d7c341ba9350e486f845be4e5786c91cc2 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+#[allow(dead_code)];
 #[deny(dead_assignment)];
 
 fn f1(x: &mut int) {
index 1cdceaca6b4b5e541a79eb84f88ba6e5de7df0fc..fd605b79dbecd48796f916f2c69b907aa5c0ebb9 100644 (file)
@@ -10,6 +10,7 @@
 
 // Issue #7526: lowercase static constants in patterns look like bindings
 
+#[allow(dead_code)];
 #[deny(non_uppercase_pattern_statics)];
 
 pub static a : int = 97;
index 019a4b88aedfd0b2983aa6a3643afd49cab610e0..4f7f7dd7408af1372bdafe2878d7868831fece2f 100644 (file)
@@ -1,3 +1,5 @@
+#[allow(dead_code)];
+
 #[static_assert]
 static A: bool = false; //~ ERROR static assertion failed
 
index 42e475dac8b6718e35a650ad1a35432517ed2ee1..ceaa388917984a7117a0ed519baf2b14f6123327 100644 (file)
@@ -1,3 +1,5 @@
+#[allow(dead_code)];
+
 #[static_assert]
 static E: bool = 1 == 2; //~ ERROR static assertion failed