@@ -46,7 +46,12 @@ use html::markdown;
46
46
47
47
#[ derive( Clone , Default ) ]
48
48
pub struct TestOptions {
49
+ /// Whether to disable the default `extern crate my_crate;` when creating doctests.
49
50
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.
50
55
pub attrs : Vec < String > ,
51
56
}
52
57
@@ -113,7 +118,8 @@ pub fn run(input_path: &Path,
113
118
let crate_name = crate_name. unwrap_or_else ( || {
114
119
:: rustc_trans_utils:: link:: find_crate_name ( None , & hir_forest. krate ( ) . attrs , & input)
115
120
} ) ;
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;
117
123
let mut collector = Collector :: new ( crate_name,
118
124
cfgs,
119
125
libs,
@@ -153,6 +159,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
153
159
154
160
let mut opts = TestOptions {
155
161
no_crate_inject : false ,
162
+ display_warnings : false ,
156
163
attrs : Vec :: new ( ) ,
157
164
} ;
158
165
@@ -357,7 +364,7 @@ pub fn make_test(s: &str,
357
364
let mut line_offset = 0 ;
358
365
let mut prog = String :: new ( ) ;
359
366
360
- if opts. attrs . is_empty ( ) {
367
+ if opts. attrs . is_empty ( ) && !opts . display_warnings {
361
368
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
362
369
// lints that are commonly triggered in doctests. The crate-level test attributes are
363
370
// 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);
787
794
//adding it anyway
788
795
let opts = TestOptions {
789
796
no_crate_inject : true ,
797
+ display_warnings : false ,
790
798
attrs : vec ! [ ] ,
791
799
} ;
792
800
let input =
@@ -957,4 +965,19 @@ assert_eq!(2+2, 4);".to_string();
957
965
let output = make_test ( input, None , true , & opts) ;
958
966
assert_eq ! ( output, ( expected. clone( ) , 1 ) ) ;
959
967
}
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
+ }
960
983
}
0 commit comments