@@ -69,9 +69,7 @@ pub fn check(
6969 body : Some ( block) ,
7070 ..
7171 } ) if ident. name == sym:: main => {
72- if !ignore {
73- get_test_spans ( & item, * ident, & mut test_attr_spans) ;
74- }
72+ get_test_spans ( & item, * ident, & mut test_attr_spans) ;
7573 let is_async = matches ! ( sig. header. coroutine_kind, Some ( CoroutineKind :: Async { .. } ) ) ;
7674 let returns_nothing = match & sig. decl . output {
7775 FnRetTy :: Default ( ..) => true ,
@@ -86,13 +84,22 @@ pub fn check(
8684 // This main function should not be linted, we're done
8785 eligible = false ;
8886 }
87+ // Return early if we're in an ignore codeblock, as
88+ // test_attr_in_doctest
89+ // won't do anything useful in these cases and we already have our
90+ // problematic function.
8991 } ,
9092 // Another function was found; this case is ignored for needless_doctest_main
9193 ItemKind :: Fn ( fn_) => {
9294 eligible = false ;
93- if !ignore {
94- get_test_spans ( & item, fn_. ident , & mut test_attr_spans) ;
95+ if ignore {
96+ // If ignore is active invalidating one lint,
97+ // and we already found another function thus
98+ // invalidating the other one, we have no
99+ // business continuing.
100+ return ( false , test_attr_spans) ;
95101 }
102+ get_test_spans ( & item, fn_. ident , & mut test_attr_spans) ;
96103 } ,
97104 // Tests with one of these items are ignored
98105 ItemKind :: Static ( ..)
@@ -120,6 +127,17 @@ pub fn check(
120127
121128 let trailing_whitespace = text. len ( ) - text. trim_end ( ) . len ( ) ;
122129
130+ // We currently only test for "fn main". Checking for the real
131+ // entrypoint (with tcx.entry_fn(())) in each block would be unnecessarily
132+ // expensive, as those are probably intended and relevant. Same goes for
133+ // macros and other weird ways of declaring a main function.
134+ //
135+ // Also, as we only check for attribute names and don't do macro expansion,
136+ // we can check only for #[test]
137+ if !( text. contains ( "fn main" ) || text. contains ( "#[test]" ) ) {
138+ return ;
139+ }
140+
123141 // Because of the global session, we need to create a new session in a different thread with
124142 // the edition we need.
125143 let text = text. to_owned ( ) ;
0 commit comments