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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 23 additions & 5 deletions clippy_lints/src/doc/needless_doctest_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ pub fn check(
body: Some(block),
..
}) if ident.name == sym::main => {
if !ignore {
get_test_spans(&item, *ident, &mut test_attr_spans);
}
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 @@ -86,13 +84,22 @@ pub fn check(
// This main function should not be linted, we're done
eligible = false;
}
// Return early if we're in an ignore codeblock, as
// test_attr_in_doctest
// won't do anything useful in these cases and we already have our
// problematic function.
},
// 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 +127,17 @@ 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("fn main") || text.contains("#[test]")) {
Copy link
Member

Choose a reason for hiding this comment

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

hm, if someone writes fn main() instead of fn main() will this cause false negatives?

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
67 changes: 65 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,69 @@
/// ```
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;
/// }
/// ```
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() {}

fn main() {}
26 changes: 26 additions & 0 deletions tests/ui/doc/needless_doctest_main.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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: aborting due to 2 previous errors