Skip to content

Commit 739320a

Browse files
committed
Auto merge of #51450 - estebank:inner-fn-test, r=@pnkfelix
Add lint warning for inner function marked as `#[test]` Fix #36629.
2 parents 860d169 + 51a0425 commit 739320a

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

src/librustc_lint/builtin.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -1709,7 +1709,6 @@ impl LintPass for SoftLints {
17091709
}
17101710
}
17111711

1712-
17131712
declare_lint! {
17141713
pub ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
17151714
Allow,
@@ -1744,3 +1743,44 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
17441743
}
17451744
}
17461745
}
1746+
1747+
declare_lint! {
1748+
UNNAMEABLE_TEST_FUNCTIONS,
1749+
Warn,
1750+
"detects an function that cannot be named being marked as #[test]"
1751+
}
1752+
1753+
pub struct UnnameableTestFunctions;
1754+
1755+
impl LintPass for UnnameableTestFunctions {
1756+
fn get_lints(&self) -> LintArray {
1757+
lint_array!(UNNAMEABLE_TEST_FUNCTIONS)
1758+
}
1759+
}
1760+
1761+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnameableTestFunctions {
1762+
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
1763+
match it.node {
1764+
hir::ItemFn(..) => {
1765+
for attr in &it.attrs {
1766+
if attr.name() == "test" {
1767+
let parent = cx.tcx.hir.get_parent(it.id);
1768+
match cx.tcx.hir.find(parent) {
1769+
Some(hir_map::NodeItem(hir::Item {node: hir::ItemMod(_), ..})) |
1770+
None => {}
1771+
_ => {
1772+
cx.struct_span_lint(
1773+
UNNAMEABLE_TEST_FUNCTIONS,
1774+
attr.span,
1775+
"cannot test inner function",
1776+
).emit();
1777+
}
1778+
}
1779+
break;
1780+
}
1781+
}
1782+
}
1783+
_ => return,
1784+
};
1785+
}
1786+
}

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
130130
MutableTransmutes: MutableTransmutes,
131131
UnionsWithDropFields: UnionsWithDropFields,
132132
UnreachablePub: UnreachablePub,
133+
UnnameableTestFunctions: UnnameableTestFunctions,
133134
TypeAliasBounds: TypeAliasBounds,
134135
UnusedBrokenConst: UnusedBrokenConst,
135136
TrivialConstraints: TrivialConstraints,

src/test/ui/lint/test-inner-fn.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 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 -D unnameable_test_functions
12+
13+
#[test]
14+
fn foo() {
15+
#[test] //~ ERROR cannot test inner function [unnameable_test_functions]
16+
fn bar() {}
17+
bar();
18+
}
19+
20+
mod x {
21+
#[test]
22+
fn foo() {
23+
#[test] //~ ERROR cannot test inner function [unnameable_test_functions]
24+
fn bar() {}
25+
bar();
26+
}
27+
}
28+
29+
fn main() {}

src/test/ui/lint/test-inner-fn.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: cannot test inner function
2+
--> $DIR/test-inner-fn.rs:15:5
3+
|
4+
LL | #[test] //~ ERROR cannot test inner function [unnameable_test_functions]
5+
| ^^^^^^^
6+
|
7+
= note: requested on the command line with `-D unnameable-test-functions`
8+
9+
error: cannot test inner function
10+
--> $DIR/test-inner-fn.rs:23:9
11+
|
12+
LL | #[test] //~ ERROR cannot test inner function [unnameable_test_functions]
13+
| ^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)