Skip to content

Commit 15d6837

Browse files
committed
Mark main-like functions allow(dead_code) in tests
Fixes rust-lang#12327.
1 parent 45de9de commit 15d6837

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

src/libsyntax/test.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![allow(unused_imports)]
1515
use self::HasTestSignature::*;
1616

17+
use std::iter;
1718
use std::slice;
1819
use std::mem;
1920
use std::vec;
@@ -24,6 +25,7 @@ use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
2425
use codemap;
2526
use diagnostic;
2627
use config;
28+
use entry::{self, EntryPointType};
2729
use ext::base::ExtCtxt;
2830
use ext::build::AstBuilder;
2931
use ext::expand::ExpansionConfig;
@@ -177,22 +179,39 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
177179
// the one we're going to add. Only if compiling an executable.
178180

179181
mod_folded.items = mem::replace(&mut mod_folded.items, vec![]).move_map(|item| {
180-
item.map(|ast::Item {id, ident, attrs, node, vis, span}| {
181-
ast::Item {
182-
id: id,
183-
ident: ident,
184-
attrs: attrs.into_iter().filter_map(|attr| {
185-
if !attr.check_name("main") {
186-
Some(attr)
187-
} else {
188-
None
182+
match entry::entry_point_type(&item, self.cx.path.len() + 1) {
183+
EntryPointType::MainNamed |
184+
EntryPointType::MainAttr |
185+
EntryPointType::Start =>
186+
item.map(|ast::Item {id, ident, attrs, node, vis, span}| {
187+
let allow_str = InternedString::new("allow");
188+
let dead_code_str = InternedString::new("dead_code");
189+
let allow_dead_code_item =
190+
attr::mk_list_item(allow_str,
191+
vec![attr::mk_word_item(dead_code_str)]);
192+
let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
193+
allow_dead_code_item);
194+
195+
ast::Item {
196+
id: id,
197+
ident: ident,
198+
attrs: attrs.into_iter().filter_map(|attr| {
199+
if !attr.check_name("main") {
200+
Some(attr)
201+
} else {
202+
None
203+
}
204+
})
205+
.chain(iter::once(allow_dead_code))
206+
.collect(),
207+
node: node,
208+
vis: vis,
209+
span: span
189210
}
190-
}).collect(),
191-
node: node,
192-
vis: vis,
193-
span: span
194-
}
195-
})
211+
}),
212+
EntryPointType::None |
213+
EntryPointType::OtherMain => item,
214+
}
196215
});
197216

198217
if !tests.is_empty() || !tested_submods.is_empty() {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
#![deny(dead_code)]
14+
15+
fn dead() {} //~ error: function is never used: `dead`
16+
17+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
#![feature(main)]
14+
15+
#![deny(dead_code)]
16+
17+
#[main]
18+
fn foo() { panic!(); }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
#![deny(dead_code)]
14+
15+
fn main() { panic!(); }

0 commit comments

Comments
 (0)