Skip to content

Commit 3fc3445

Browse files
Rework SESSION_GLOBALS API to prevent overwriting it
1 parent 7de1efa commit 3fc3445

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

clippy_lints/src/doc.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_span::source_map::{BytePos, FilePathMapping, MultiSpan, SourceMap, Spa
2626
use rustc_span::{sym, FileName, Pos};
2727
use std::io;
2828
use std::ops::Range;
29+
use std::thread;
2930
use url::Url;
3031

3132
declare_clippy_lint! {
@@ -584,10 +585,10 @@ fn get_current_span(spans: &[(usize, Span)], idx: usize) -> (usize, Span) {
584585
}
585586

586587
fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
587-
fn has_needless_main(code: &str, edition: Edition) -> bool {
588+
fn has_needless_main(code: String, edition: Edition) -> bool {
588589
rustc_driver::catch_fatal_errors(|| {
589-
rustc_span::with_session_globals(edition, || {
590-
let filename = FileName::anon_source_code(code);
590+
rustc_span::create_session_globals_then(edition, || {
591+
let filename = FileName::anon_source_code(&code);
591592

592593
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
593594
let emitter = EmitterWriter::new(box io::sink(), None, false, false, false, None, false);
@@ -649,7 +650,10 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
649650
.unwrap_or_default()
650651
}
651652

652-
if has_needless_main(text, edition) {
653+
// Because of the global session, we need to create a new session in a different thread with
654+
// the edition we need.
655+
let text = text.to_owned();
656+
if thread::spawn(move || has_needless_main(text, edition)).join().expect("thread::spawn failed") {
653657
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
654658
}
655659
}

0 commit comments

Comments
 (0)