#![allow(clippy::needless_doctest_main)] per doctest

#![allow(clippy::needless_doctest_main)] or #[allow(clippy::needless_doctest_main)] don't have effect when in the doctest itself. But #![allow(clippy::needless_doctest_main)] does apply when in src/lib.rs - which is strange, because (I've thought that) doctests are build in a separate crate (and they don't have access to non-public items).

Is that by design, or a likely error? I wish to apply ``#[allow(clippy::needless_doctest_main)]` granularly only where needed (rather than wild allow).

cargo +stable clippy
...
warning: needless `fn main` in doctest
   --> src/linted_with_tests.rs:235:5
    |
235 |   /// #![allow(clippy::needless_doctest_main)]
    |  _____^
236 | | /// ::prudent::load!(any: "linted.rs");
237 | | /// use self::prudent::*;
238 | | /// #[allow(clippy::needless_doctest_main)]
...   |
274 | | /// }
275 | | /// }
    | |_____^

Clippy doesn't check the doctests themselves, at least today, so if the lint applied only inside doctest-created crates, it wouldn't work at all. But you can control it for a single item:

#![warn(clippy::needless_doctest_main)]


#[allow(clippy::needless_doctest_main)]
/// ```
/// fn main() {
/// println!("hi");
/// }
/// ```
pub fn foo() {}
2 Likes

Wow, thank you.
For anyone else needing this: Yes, the above is right. #[allow(clippy::needless_doctest_main)] goes outside (before) the affected rustdoc comment.

1 Like