2019-11-29 20:47:26 +00:00
|
|
|
/// This is a test for needless `fn main()` in doctests.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// This should lint
|
|
|
|
/// ```
|
|
|
|
/// fn main() {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
|
|
|
//~| NOTE: `-D clippy::needless-doctest-main` implied by `-D warnings`
|
2019-11-29 20:47:26 +00:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-28 19:35:48 +00:00
|
|
|
///
|
2020-08-28 14:10:16 +00:00
|
|
|
/// With an explicit return type it should lint too
|
2020-12-20 16:19:49 +00:00
|
|
|
/// ```edition2015
|
2020-08-28 14:10:16 +00:00
|
|
|
/// fn main() -> () {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
2020-08-28 14:10:16 +00:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-28 19:35:48 +00:00
|
|
|
///
|
2020-08-28 14:10:16 +00:00
|
|
|
/// This should, too.
|
2020-03-10 19:19:37 +00:00
|
|
|
/// ```rust
|
|
|
|
/// fn main() {
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
2020-03-10 19:19:37 +00:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2023-07-28 19:35:48 +00:00
|
|
|
///
|
2020-03-10 19:19:37 +00:00
|
|
|
/// This one too.
|
|
|
|
/// ```no_run
|
2023-09-08 22:39:20 +00:00
|
|
|
/// // the fn is not always the first line
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: needless `fn main` in doctest
|
2023-09-08 22:39:20 +00:00
|
|
|
/// fn main() {
|
2020-03-10 19:19:37 +00:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
fn bad_doctests() {}
|
2019-11-29 20:47:26 +00:00
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// This shouldn't lint, because the `main` is empty:
|
|
|
|
/// ```
|
|
|
|
/// fn main(){}
|
|
|
|
/// ```
|
|
|
|
///
|
2020-08-28 14:10:16 +00:00
|
|
|
/// This shouldn't lint either, because main is async:
|
2020-12-20 16:19:49 +00:00
|
|
|
/// ```edition2018
|
2020-08-28 14:10:16 +00: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 20:47:26 +00:00
|
|
|
/// This shouldn't lint either, because there's a `static`:
|
|
|
|
/// ```
|
|
|
|
/// static ANSWER: i32 = 42;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// assert_eq!(42, ANSWER);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-12-23 12:11:20 +00:00
|
|
|
///
|
2020-08-28 14:10:16 +00:00
|
|
|
/// This shouldn't lint either, because there's a `const`:
|
|
|
|
/// ```
|
|
|
|
/// fn main() {
|
|
|
|
/// assert_eq!(42, ANSWER);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// const ANSWER: i32 = 42;
|
|
|
|
/// ```
|
|
|
|
///
|
2019-12-23 12:11:20 +00: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 19:19:37 +00:00
|
|
|
///
|
2020-08-28 14:10:16 +00: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 19:19:37 +00:00
|
|
|
///
|
2020-08-28 14:10:16 +00: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 19:19:37 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// fn main() {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Or even non-rust examples:
|
|
|
|
/// ```text
|
|
|
|
/// fn main() {
|
|
|
|
/// is what starts the program
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-11-29 20:47:26 +00:00
|
|
|
fn no_false_positives() {}
|
|
|
|
|
2020-12-20 16:19:49 +00:00
|
|
|
/// Yields a parse error when interpreted as rust code:
|
|
|
|
/// ```
|
|
|
|
/// r#"hi"
|
|
|
|
/// ```
|
|
|
|
fn issue_6022() {}
|
|
|
|
|
2019-11-29 20:47:26 +00:00
|
|
|
fn main() {
|
2020-03-10 19:19:37 +00:00
|
|
|
bad_doctests();
|
2019-11-29 20:47:26 +00:00
|
|
|
no_false_positives();
|
|
|
|
}
|