2019-11-29 14:47:26 -06:00
|
|
|
/// This is a test for needless `fn main()` in doctests.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// This should lint
|
|
|
|
/// ```
|
|
|
|
/// fn main() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
|
|
|
//~| NOTE: `-D clippy::needless-doctest-main` implied by `-D warnings`
|
2019-11-29 14:47:26 -06:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-28 14:35:48 -05:00
|
|
|
///
|
2020-08-28 09:10:16 -05:00
|
|
|
/// With an explicit return type it should lint too
|
2020-12-20 10:19:49 -06:00
|
|
|
/// ```edition2015
|
2020-08-28 09:10:16 -05:00
|
|
|
/// fn main() -> () {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
2020-08-28 09:10:16 -05:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-28 14:35:48 -05:00
|
|
|
///
|
2020-08-28 09:10:16 -05:00
|
|
|
/// This should, too.
|
2020-03-10 14:19:37 -05:00
|
|
|
/// ```rust
|
|
|
|
/// fn main() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
2020-03-10 14:19:37 -05:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-28 14:35:48 -05:00
|
|
|
///
|
2020-03-10 14:19:37 -05:00
|
|
|
/// This one too.
|
|
|
|
/// ```no_run
|
2023-09-08 17:39:20 -05:00
|
|
|
/// // the fn is not always the first line
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
2023-09-08 17:39:20 -05:00
|
|
|
/// fn main() {
|
2020-03-10 14:19:37 -05:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
fn bad_doctests() {}
|
2019-11-29 14:47:26 -06:00
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// This shouldn't lint, because the `main` is empty:
|
|
|
|
/// ```
|
|
|
|
/// fn main(){}
|
|
|
|
/// ```
|
|
|
|
///
|
2020-08-28 09:10:16 -05:00
|
|
|
/// This shouldn't lint either, because main is async:
|
2020-12-20 10:19:49 -06:00
|
|
|
/// ```edition2018
|
2020-08-28 09:10:16 -05:00
|
|
|
/// async fn main() {
|
|
|
|
/// assert_eq!(42, ANSWER);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Same here, because the return type is not the unit type:
|
|
|
|
/// ```
|
|
|
|
/// fn main() -> Result<()> {
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2019-11-29 14:47:26 -06:00
|
|
|
/// This shouldn't lint either, because there's a `static`:
|
|
|
|
/// ```
|
|
|
|
/// static ANSWER: i32 = 42;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// assert_eq!(42, ANSWER);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-12-23 06:11:20 -06:00
|
|
|
///
|
2020-08-28 09:10:16 -05:00
|
|
|
/// This shouldn't lint either, because there's a `const`:
|
|
|
|
/// ```
|
|
|
|
/// fn main() {
|
|
|
|
/// assert_eq!(42, ANSWER);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// const ANSWER: i32 = 42;
|
|
|
|
/// ```
|
|
|
|
///
|
2019-12-23 06:11:20 -06:00
|
|
|
/// Neither should this lint because of `extern crate`:
|
|
|
|
/// ```
|
|
|
|
/// #![feature(test)]
|
|
|
|
/// extern crate test;
|
|
|
|
/// fn main() {
|
|
|
|
/// assert_eq(1u8, test::black_box(1));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-03-10 14:19:37 -05:00
|
|
|
///
|
2020-08-28 09:10:16 -05:00
|
|
|
/// Neither should this lint because it has an extern block:
|
|
|
|
/// ```
|
|
|
|
/// extern {}
|
|
|
|
/// fn main() {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// This should not lint because there is another function defined:
|
|
|
|
/// ```
|
|
|
|
/// fn fun() {}
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-03-10 14:19:37 -05:00
|
|
|
///
|
2020-08-28 09:10:16 -05:00
|
|
|
/// We should not lint inside raw strings ...
|
|
|
|
/// ```
|
|
|
|
/// let string = r#"
|
|
|
|
/// fn main() {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// "#;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ... or comments
|
|
|
|
/// ```
|
|
|
|
/// // fn main() {
|
|
|
|
/// // let _inception = 42;
|
|
|
|
/// // }
|
|
|
|
/// let _inception = 42;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// We should not lint ignored examples:
|
2020-03-10 14:19:37 -05:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// fn main() {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Or even non-rust examples:
|
|
|
|
/// ```text
|
|
|
|
/// fn main() {
|
|
|
|
/// is what starts the program
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-11-29 14:47:26 -06:00
|
|
|
fn no_false_positives() {}
|
|
|
|
|
2020-12-20 10:19:49 -06:00
|
|
|
/// Yields a parse error when interpreted as rust code:
|
|
|
|
/// ```
|
|
|
|
/// r#"hi"
|
|
|
|
/// ```
|
|
|
|
fn issue_6022() {}
|
|
|
|
|
2019-11-29 14:47:26 -06:00
|
|
|
fn main() {
|
2020-03-10 14:19:37 -05:00
|
|
|
bad_doctests();
|
2019-11-29 14:47:26 -06:00
|
|
|
no_false_positives();
|
|
|
|
}
|