@@ -46,7 +46,12 @@ use html::markdown;
4646
4747#[ derive( Clone , Default ) ]
4848pub struct TestOptions {
49+ /// Whether to disable the default `extern crate my_crate;` when creating doctests.
4950 pub no_crate_inject : bool ,
51+ /// Whether to emit compilation warnings when compiling doctests. Setting this will suppress
52+ /// the default `#![allow(unused)]`.
53+ pub display_warnings : bool ,
54+ /// Additional crate-level attributes to add to doctests.
5055 pub attrs : Vec < String > ,
5156}
5257
@@ -113,7 +118,8 @@ pub fn run(input_path: &Path,
113118 let crate_name = crate_name. unwrap_or_else ( || {
114119 :: rustc_trans_utils:: link:: find_crate_name ( None , & hir_forest. krate ( ) . attrs , & input)
115120 } ) ;
116- let opts = scrape_test_config ( hir_forest. krate ( ) ) ;
121+ let mut opts = scrape_test_config ( hir_forest. krate ( ) ) ;
122+ opts. display_warnings |= display_warnings;
117123 let mut collector = Collector :: new ( crate_name,
118124 cfgs,
119125 libs,
@@ -153,6 +159,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
153159
154160 let mut opts = TestOptions {
155161 no_crate_inject : false ,
162+ display_warnings : false ,
156163 attrs : Vec :: new ( ) ,
157164 } ;
158165
@@ -357,7 +364,7 @@ pub fn make_test(s: &str,
357364 let mut line_offset = 0 ;
358365 let mut prog = String :: new ( ) ;
359366
360- if opts. attrs . is_empty ( ) {
367+ if opts. attrs . is_empty ( ) && !opts . display_warnings {
361368 // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
362369 // lints that are commonly triggered in doctests. The crate-level test attributes are
363370 // commonly used to make tests fail in case they trigger warnings, so having this there in
@@ -787,6 +794,7 @@ assert_eq!(2+2, 4);
787794 //adding it anyway
788795 let opts = TestOptions {
789796 no_crate_inject : true ,
797+ display_warnings : false ,
790798 attrs : vec ! [ ] ,
791799 } ;
792800 let input =
@@ -957,4 +965,19 @@ assert_eq!(2+2, 4);".to_string();
957965 let output = make_test ( input, None , true , & opts) ;
958966 assert_eq ! ( output, ( expected. clone( ) , 1 ) ) ;
959967 }
968+
969+ #[ test]
970+ fn make_test_display_warnings ( ) {
971+ //if the user is asking to display doctest warnings, suppress the default allow(unused)
972+ let mut opts = TestOptions :: default ( ) ;
973+ opts. display_warnings = true ;
974+ let input =
975+ "assert_eq!(2+2, 4);" ;
976+ let expected =
977+ "fn main() {
978+ assert_eq!(2+2, 4);
979+ }" . to_string ( ) ;
980+ let output = make_test ( input, None , false , & opts) ;
981+ assert_eq ! ( output, ( expected. clone( ) , 1 ) ) ;
982+ }
960983}
0 commit comments