@@ -45,7 +45,12 @@ use html::markdown;
45
45
46
46
#[ derive( Clone , Default ) ]
47
47
pub struct TestOptions {
48
+ /// Whether to disable the default `extern crate my_crate;` when creating doctests.
48
49
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.
49
54
pub attrs : Vec < String > ,
50
55
}
51
56
@@ -107,7 +112,8 @@ pub fn run(input_path: &Path,
107
112
let crate_name = crate_name. unwrap_or_else ( || {
108
113
:: rustc_trans_utils:: link:: find_crate_name ( None , & hir_forest. krate ( ) . attrs , & input)
109
114
} ) ;
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;
111
117
let mut collector = Collector :: new ( crate_name,
112
118
cfgs,
113
119
libs,
@@ -146,6 +152,7 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
146
152
147
153
let mut opts = TestOptions {
148
154
no_crate_inject : false ,
155
+ display_warnings : false ,
149
156
attrs : Vec :: new ( ) ,
150
157
} ;
151
158
@@ -347,7 +354,7 @@ pub fn make_test(s: &str,
347
354
let mut line_offset = 0 ;
348
355
let mut prog = String :: new ( ) ;
349
356
350
- if opts. attrs . is_empty ( ) {
357
+ if opts. attrs . is_empty ( ) && !opts . display_warnings {
351
358
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
352
359
// lints that are commonly triggered in doctests. The crate-level test attributes are
353
360
// 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);
772
779
//adding it anyway
773
780
let opts = TestOptions {
774
781
no_crate_inject : true ,
782
+ display_warnings : false ,
775
783
attrs : vec ! [ ] ,
776
784
} ;
777
785
let input =
@@ -924,4 +932,19 @@ assert_eq!(2+2, 4);".to_string();
924
932
let output = make_test ( input, None , true , & opts) ;
925
933
assert_eq ! ( output, ( expected. clone( ) , 1 ) ) ;
926
934
}
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
+ }
927
950
}
0 commit comments