Skip to content

Commit 9f68d62

Browse files
committed
don't let rustdoc get confused by text "fn main" in a line comment
This is in the matter of #21299.
1 parent 930d3b1 commit 9f68d62

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/librustdoc/test.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,21 @@ pub fn make_test(s: &str,
348348
}
349349
}
350350
}
351-
if dont_insert_main || s.contains("fn main") {
351+
352+
// FIXME (#21299): prefer libsyntax or some other actual parser over this
353+
// best-effort ad hoc approach
354+
let already_has_main = s.lines()
355+
.map(|line| {
356+
let comment = line.find("//");
357+
if let Some(comment_begins) = comment {
358+
&line[0..comment_begins]
359+
} else {
360+
line
361+
}
362+
})
363+
.any(|code| code.contains("fn main"));
364+
365+
if dont_insert_main || already_has_main {
352366
prog.push_str(&everything_else);
353367
} else {
354368
prog.push_str("fn main() {\n");

0 commit comments

Comments
 (0)