Skip to content

Commit 6a2003e

Browse files
committed
Lint inner fn marked as #[test]
1 parent d6e2239 commit 6a2003e

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
@@ -1704,7 +1704,6 @@ impl LintPass for SoftLints {
17041704
}
17051705
}
17061706

1707-
17081707
declare_lint! {
17091708
pub ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
17101709
Allow,
@@ -1739,3 +1738,44 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
17391738
}
17401739
}
17411740
}
1741+
1742+
declare_lint! {
1743+
UNTESTABLE_METHOD,
1744+
Warn,
1745+
"detects untestable method marked as #[test]"
1746+
}
1747+
1748+
pub struct UntestableMethod;
1749+
1750+
impl LintPass for UntestableMethod {
1751+
fn get_lints(&self) -> LintArray {
1752+
lint_array!(UNTESTABLE_METHOD)
1753+
}
1754+
}
1755+
1756+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UntestableMethod {
1757+
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
1758+
match it.node {
1759+
hir::ItemFn(..) => {
1760+
for attr in &it.attrs {
1761+
if attr.name() == "test" {
1762+
let parent = cx.tcx.hir.get_parent(it.id);
1763+
match cx.tcx.hir.find(parent) {
1764+
Some(hir_map::NodeItem(hir::Item {node: hir::ItemMod(_), ..})) |
1765+
None => {}
1766+
_ => {
1767+
cx.struct_span_lint(
1768+
UNTESTABLE_METHOD,
1769+
attr.span,
1770+
"cannot test inner function",
1771+
).emit();
1772+
}
1773+
}
1774+
break;
1775+
}
1776+
}
1777+
}
1778+
_ => return,
1779+
};
1780+
}
1781+
}

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+
UntestableMethod: UntestableMethod,
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 untestable_method
12+
13+
#[test]
14+
fn foo() {
15+
#[test] //~ ERROR cannot test inner function [untestable_method]
16+
fn bar() {}
17+
bar();
18+
}
19+
20+
mod x {
21+
#[test]
22+
fn foo() {
23+
#[test] //~ ERROR cannot test inner function [untestable_method]
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 [untestable_method]
5+
| ^^^^^^^
6+
|
7+
= note: requested on the command line with `-D untestable-method`
8+
9+
error: cannot test inner function
10+
--> $DIR/test-inner-fn.rs:23:9
11+
|
12+
LL | #[test] //~ ERROR cannot test inner function [untestable_method]
13+
| ^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)