@@ -45,7 +45,12 @@ use html::markdown;
4545
4646#[ derive( Clone , Default ) ]
4747pub struct TestOptions {
48+ /// Whether to disable the default `extern crate my_crate;` when creating doctests.
4849 pub no_crate_inject : bool ,
50+ /// Whether to emit compilation warnings when compiling doctests. Setting this will suppress
51+ /// the default `#![allow(unused)]`.
52+ pub display_warnings : bool ,
53+ /// Additional crate-level attributes to add to doctests.
4954 pub attrs : Vec < String > ,
5055}
5156
@@ -107,7 +112,8 @@ pub fn run(input_path: &Path,
107112 let crate_name = crate_name. unwrap_or_else ( || {
108113 :: rustc_trans_utils:: link:: find_crate_name ( None , & hir_forest. krate ( ) . attrs , & input)
109114 } ) ;
110- let opts = scrape_test_config ( hir_forest. krate ( ) ) ;
115+ let mut opts = scrape_test_config ( hir_forest. krate ( ) ) ;
116+ opts. display_warnings |= display_warnings;
111117 let mut collector = Collector :: new ( crate_name,
112118 cfgs,
113119 libs,
@@ -146,6 +152,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
146152
147153 let mut opts = TestOptions {
148154 no_crate_inject : false ,
155+ display_warnings : false ,
149156 attrs : Vec :: new ( ) ,
150157 } ;
151158
@@ -347,7 +354,7 @@ pub fn make_test(s: &str,
347354 let mut line_offset = 0 ;
348355 let mut prog = String :: new ( ) ;
349356
350- if opts. attrs . is_empty ( ) {
357+ if opts. attrs . is_empty ( ) && !opts . display_warnings {
351358 // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
352359 // lints that are commonly triggered in doctests. The crate-level test attributes are
353360 // commonly used to make tests fail in case they trigger warnings, so having this there in
@@ -772,6 +779,7 @@ assert_eq!(2+2, 4);
772779 //adding it anyway
773780 let opts = TestOptions {
774781 no_crate_inject : true ,
782+ display_warnings : false ,
775783 attrs : vec ! [ ] ,
776784 } ;
777785 let input =
@@ -924,4 +932,19 @@ assert_eq!(2+2, 4);".to_string();
924932 let output = make_test ( input, None , true , & opts) ;
925933 assert_eq ! ( output, ( expected. clone( ) , 1 ) ) ;
926934 }
935+
936+ #[ test]
937+ fn make_test_display_warnings ( ) {
938+ //if the user is asking to display doctest warnings, suppress the default allow(unused)
939+ let mut opts = TestOptions :: default ( ) ;
940+ opts. display_warnings = true ;
941+ let input =
942+ "assert_eq!(2+2, 4);" ;
943+ let expected =
944+ "fn main() {
945+ assert_eq!(2+2, 4);
946+ }" . to_string ( ) ;
947+ let output = make_test ( input, None , false , & opts) ;
948+ assert_eq ! ( output, ( expected. clone( ) , 2 ) ) ;
949+ }
927950}
0 commit comments