Skip to content

Commit

Permalink
Rework SESSION_GLOBALS API to prevent overwriting it
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jul 8, 2021
1 parent 7de1efa commit 3fc3445
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions clippy_lints/src/doc.rs
Expand Up @@ -26,6 +26,7 @@ use rustc_span::source_map::{BytePos, FilePathMapping, MultiSpan, SourceMap, Spa
use rustc_span::{sym, FileName, Pos};
use std::io;
use std::ops::Range;
use std::thread;
use url::Url;

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

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

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

if has_needless_main(text, edition) {
// 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();
if thread::spawn(move || has_needless_main(text, edition)).join().expect("thread::spawn failed") {
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
}
}
Expand Down

0 comments on commit 3fc3445

Please sign in to comment.