diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs
index daf10e5b88a83..d71098ad89da0 100644
--- a/src/librustdoc/doctest.rs
+++ b/src/librustdoc/doctest.rs
@@ -679,6 +679,10 @@ pub(crate) fn make_test(
             // parse the source, but only has false positives, not false
             // negatives.
             if s.contains(crate_name) {
+                // rustdoc implicitly inserts an `extern crate` item for the own crate
+                // which may be unused, so we need to allow the lint.
+                prog.push_str(&format!("#[allow(unused_extern_crates)]\n"));
+
                 prog.push_str(&format!("extern crate r#{crate_name};\n"));
                 line_offset += 1;
             }
diff --git a/src/librustdoc/doctest/tests.rs b/src/librustdoc/doctest/tests.rs
index 360d2259ea3d2..a30fe28f94f99 100644
--- a/src/librustdoc/doctest/tests.rs
+++ b/src/librustdoc/doctest/tests.rs
@@ -38,6 +38,7 @@ fn make_test_crate_name() {
     let input = "use asdf::qwop;
 assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
+#[allow(unused_extern_crates)]
 extern crate r#asdf;
 fn main() {
 use asdf::qwop;
@@ -128,6 +129,7 @@ fn make_test_opts_attrs() {
     let input = "use asdf::qwop;
 assert_eq!(2+2, 4);";
     let expected = "#![feature(sick_rad)]
+#[allow(unused_extern_crates)]
 extern crate r#asdf;
 fn main() {
 use asdf::qwop;
@@ -141,6 +143,7 @@ assert_eq!(2+2, 4);
     opts.attrs.push("feature(hella_dope)".to_string());
     let expected = "#![feature(sick_rad)]
 #![feature(hella_dope)]
+#[allow(unused_extern_crates)]
 extern crate r#asdf;
 fn main() {
 use asdf::qwop;
@@ -236,6 +239,7 @@ assert_eq!(asdf::foo, 4);";
 
     let expected = "#![allow(unused)]
 extern crate hella_qwop;
+#[allow(unused_extern_crates)]
 extern crate r#asdf;
 fn main() {
 assert_eq!(asdf::foo, 4);
diff --git a/tests/rustdoc/playground-arg.rs b/tests/rustdoc/playground-arg.rs
index f3811fe0b0ad1..2542ed657c1a7 100644
--- a/tests/rustdoc/playground-arg.rs
+++ b/tests/rustdoc/playground-arg.rs
@@ -10,4 +10,4 @@
 pub fn dummy() {}
 
 // ensure that `extern crate foo;` was inserted into code snips automatically:
-// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0Aextern+crate+r%23foo;%0Afn+main()+%7B%0Ause+foo::dummy;%0Adummy();%0A%7D&edition=2015"]' "Run"
+// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0A%23%5Ballow(unused_extern_crates)%5D%0Aextern+crate+r%23foo;%0Afn+main()+%7B%0Ause+foo::dummy;%0Adummy();%0A%7D&edition=2015"]' "Run"