|
| 1 | +- Feature Name: `lint_test_inner_function` |
| 2 | +- Start Date: 2018-06-10 |
| 3 | +- RFC PR: [rust-lang/rfcs#2471](https://github.com/rust-lang/rfcs/pull/2471) |
| 4 | +- Rust Issue: [rust-lang/rust#53911](https://github.com/rust-lang/rust/issues/53911) |
| 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