Skip to content

[PERF] Don't spawn so many compilers (3/2) (19m -> 250k) #15030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions clippy_lints/src/doc/needless_doctest_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub fn check(
if !ignore {
get_test_spans(&item, *ident, &mut test_attr_spans);
}

let is_async = matches!(sig.header.coroutine_kind, Some(CoroutineKind::Async { .. }));
let returns_nothing = match &sig.decl.output {
FnRetTy::Default(..) => true,
Expand All @@ -90,9 +91,14 @@ pub fn check(
// Another function was found; this case is ignored for needless_doctest_main
ItemKind::Fn(fn_) => {
eligible = false;
if !ignore {
get_test_spans(&item, fn_.ident, &mut test_attr_spans);
if ignore {
// If ignore is active invalidating one lint,
// and we already found another function thus
// invalidating the other one, we have no
// business continuing.
return (false, test_attr_spans);
}
get_test_spans(&item, fn_.ident, &mut test_attr_spans);
},
// Tests with one of these items are ignored
ItemKind::Static(..)
Expand Down Expand Up @@ -120,6 +126,18 @@ pub fn check(

let trailing_whitespace = text.len() - text.trim_end().len();

// We currently only test for "fn main". Checking for the real
// entrypoint (with tcx.entry_fn(())) in each block would be unnecessarily
// expensive, as those are probably intended and relevant. Same goes for
// macros and other weird ways of declaring a main function.
//
// Also, as we only check for attribute names and don't do macro expansion,
// we can check only for #[test]

if !((text.contains("main") && text.contains("fn")) || text.contains("#[test]")) {
return;
}

// Because of the global session, we need to create a new session in a different thread with
// the edition we need.
let text = text.to_owned();
Expand Down
96 changes: 94 additions & 2 deletions tests/ui/doc/needless_doctest_main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@ check-pass

#![warn(clippy::needless_doctest_main)]
//! issue 10491:
//! ```rust,no_test
Expand All @@ -19,4 +17,98 @@
/// ```
fn foo() {}

#[rustfmt::skip]
/// Description
/// ```rust
/// fn main() {
//~^ error: needless `fn main` in doctest
/// let a = 0;
/// }
/// ```
fn mulpipulpi() {}

#[rustfmt::skip]
/// With a `#[no_main]`
/// ```rust
/// #[no_main]
/// fn a() {
/// let _ = 0;
/// }
/// ```
fn pulpimulpi() {}

// Without a `#[no_main]` attribute
/// ```rust
/// fn a() {
/// let _ = 0;
/// }
/// ```
fn plumilupi() {}

#[rustfmt::skip]
/// Additional function, shouldn't trigger
/// ```rust
/// fn additional_function() {
/// let _ = 0;
/// // Thus `fn main` is actually relevant!
/// }
/// fn main() {
/// let _ = 0;
/// }
/// ```
Comment on lines +49 to +58
Copy link
Member

@y21 y21 Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Non blocking question) Out of curiosity, do you know why we don't want to lint this case (when there is another function)? Nested functions are allowed and so the function would instead just be part of the implicit main fn, so it could still be removed as far as I can tell?

Copy link
Member Author

@blyxyas blyxyas Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, I suppose it's just a sensible thing to do. When having more than one function omitting fn main makes it look like some pseudo-language for scripting or something.

fn a() -> bool {
    return true
}

let _ = 0;
let _ = a();
// ^^^^ What's the scope for this??

fn mlupipupi() {}

#[rustfmt::skip]
/// Additional function AFTER main, shouldn't trigger
/// ```rust
/// fn main() {
/// let _ = 0;
/// }
/// fn additional_function() {
/// let _ = 0;
/// // Thus `fn main` is actually relevant!
/// }
/// ```
fn lumpimupli() {}

#[rustfmt::skip]
/// Ignore code block, should not lint at all
/// ```rust, ignore
/// fn main() {
//~^ error: needless `fn main` in doctest
/// // Hi!
/// let _ = 0;
/// }
/// ```
fn mpulpilumi() {}

#[rustfmt::skip]
/// Spaces in weird positions (including an \u{A0} after `main`)
/// ```rust
/// fn main (){
//~^ error: needless `fn main` in doctest
/// let _ = 0;
/// }
/// ```
fn plumpiplupi() {}

/// 4 Functions, this should not lint because there are several function
///
/// ```rust
/// fn a() {let _ = 0; }
/// fn b() {let _ = 0; }
/// fn main() { let _ = 0; }
/// fn d() { let _ = 0; }
/// ```
fn pulmipulmip() {}

/// 3 Functions but main is first, should also not lint
///
///```rust
/// fn main() { let _ = 0; }
/// fn b() { let _ = 0; }
/// fn c() { let _ = 0; }
/// ```
fn pmuplimulip() {}

fn main() {}
36 changes: 36 additions & 0 deletions tests/ui/doc/needless_doctest_main.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error: needless `fn main` in doctest
--> tests/ui/doc/needless_doctest_main.rs:23:5
|
LL | /// fn main() {
| _____^
LL | |
LL | | /// let a = 0;
LL | | /// }
| |_____^
|
= note: `-D clippy::needless-doctest-main` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::needless_doctest_main)]`

error: needless `fn main` in doctest
--> tests/ui/doc/needless_doctest_main.rs:77:5
|
LL | /// fn main() {
| _____^
LL | |
LL | | /// // Hi!
LL | | /// let _ = 0;
LL | | /// }
| |_____^

error: needless `fn main` in doctest
--> tests/ui/doc/needless_doctest_main.rs:88:5
|
LL | /// fn main (){
| _____^
LL | |
LL | | /// let _ = 0;
LL | | /// }
| |_____^

error: aborting due to 3 previous errors