Skip to content

Commit bd1c637

Browse files
committed
Initial draft of untestable-function lint
1 parent 09f4844 commit bd1c637

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

text/0000-lint-test-inner-function.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
- Feature Name: lint-test-inner-function
2+
- Start Date: 2018-06-10
3+
- RFC PR: (leave this empty)
4+
- Rust Issue: (leave this empty)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Add a lint that warns when marking an inner function as `#[test]`.
10+
11+
# Motivation
12+
[motivation]: #motivation
13+
14+
`#[test]` is used to mark funcitons to be run as part of a test suite. The
15+
functions being marked need to be addressable for this to work. Currently,
16+
marking an inner function as `#[test]` will not raise any errors or warnings,
17+
but the test will silently not be run. By adding a lint that identifies these
18+
cases, users are less likely to fail to notice.
19+
20+
# Guide-level explanation
21+
[guide-level-explanation]: #guide-level-explanation
22+
23+
This is a lint that triggers when a `#[test]` annotation is found in a non
24+
addressable function, warning that that function cannot be tested.
25+
26+
For example, in the following code, `bar` will never be called as part of a
27+
test run:
28+
29+
```rust
30+
fn foo() {
31+
#[test]
32+
fn bar() {
33+
assert!(true);
34+
}
35+
}
36+
```
37+
38+
The output should resemble the following:
39+
40+
```
41+
error: cannot test inner function
42+
--> $DIR/test-inner-fn.rs:15:5
43+
|
44+
LL | #[test] //~ ERROR cannot test inner function [untestable_method]
45+
| ^^^^^^^
46+
|
47+
= note: requested on the command line with `-D untestable-method`
48+
```
49+
50+
# Reference-level explanation
51+
[reference-level-explanation]: #reference-level-explanation
52+
53+
This is a new lint that shouldn't interact with others. Due to the interaction
54+
with `cfg` attributes, the lint might only warn when run as part of a `--test`
55+
compilation. This would be acceptable.
56+
57+
# Drawbacks
58+
[drawbacks]: #drawbacks
59+
60+
Can't think of any reason not to do this.
61+
62+
# Rationale and alternatives
63+
[alternatives]: #alternatives
64+
65+
Adding as a lint allows users to silence the error if they so wish.
66+
67+
Not addressing this issue will let this problem continue happening without
68+
warning to end users.
69+
70+
# Prior art
71+
[prior-art]: #prior-art
72+
73+
This would act in the same way as other lints warning for potentially
74+
problematic valid code.
75+
76+
# Unresolved questions
77+
[unresolved]: #unresolved-questions
78+
79+
None.

0 commit comments

Comments
 (0)