From 9f68d62e0cbefe6d54f920d905f06147b4b15d8e Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Tue, 19 Sep 2017 22:58:33 -0700 Subject: [PATCH] don't let rustdoc get confused by text "fn main" in a line comment This is in the matter of #21299. --- src/librustdoc/test.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index f9cb4b84545dd..7fa1b38bdadfa 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -348,7 +348,21 @@ pub fn make_test(s: &str, } } } - if dont_insert_main || s.contains("fn main") { + + // FIXME (#21299): prefer libsyntax or some other actual parser over this + // best-effort ad hoc approach + let already_has_main = s.lines() + .map(|line| { + let comment = line.find("//"); + if let Some(comment_begins) = comment { + &line[0..comment_begins] + } else { + line + } + }) + .any(|code| code.contains("fn main")); + + if dont_insert_main || already_has_main { prog.push_str(&everything_else); } else { prog.push_str("fn main() {\n");