Skip to content

Commit 8c9c951

Browse files
committed
Auto merge of #29544 - Ryman:reduce_doc_warnings, r=steveklabnik
Did this alphabetically, so I didn't see [how `std` was doing things](https://dxr.mozilla.org/rust/source/src/libstd/lib.rs#215) till I was nearly finished. If you prefer to add crate-level-whitelists like std instead of test-level, I can rebase with that strategy. A number of these commits can probably be dropped as the crates don't have much to test, and are deprecated. Let me know which if any to drop! (can also squash after review if desired) r? @steveklabnik
2 parents 0bd7084 + 5ae1937 commit 8c9c951

File tree

47 files changed

+168
-69
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+168
-69
lines changed

mk/tests.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ $(eval $(call RUST_CRATE,collectionstest))
2525
TEST_TARGET_CRATES = $(filter-out core rustc_unicode alloc_system libc \
2626
alloc_jemalloc,$(TARGET_CRATES)) \
2727
collectionstest coretest
28-
TEST_DOC_CRATES = $(DOC_CRATES)
28+
TEST_DOC_CRATES = $(DOC_CRATES) arena flate fmt_macros getopts graphviz \
29+
log rand rbml serialize syntax term test
2930
TEST_HOST_CRATES = $(filter-out rustc_typeck rustc_borrowck rustc_resolve \
3031
rustc_trans rustc_lint,\
3132
$(HOST_CRATES))

src/doc/trpl/documentation.md

+12
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,18 @@ You can control a few aspects of the HTML that `rustdoc` generates through the
620620

621621
This sets a few different options, with a logo, favicon, and a root URL.
622622

623+
### Configuring documentation tests
624+
625+
You can also configure the way that `rustdoc` tests your documentation examples
626+
through the `#![doc(test(..))]` attribute.
627+
628+
```rust
629+
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
630+
```
631+
632+
This allows unused variables within the examples, but will fail the test for any
633+
other lint warning thrown.
634+
623635
## Generation options
624636

625637
`rustdoc` also contains a few other options on the command line, for further customization:

src/liballoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
7171
html_root_url = "https://doc.rust-lang.org/nightly/",
7272
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
73-
test(no_crate_inject))]
73+
test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
7474
#![no_std]
7575
#![cfg_attr(not(stage0), needs_allocator)]
7676

src/libarena/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
#![crate_type = "dylib"]
2929
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
3030
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
31-
html_root_url = "https://doc.rust-lang.org/nightly/")]
31+
html_root_url = "https://doc.rust-lang.org/nightly/",
32+
test(no_crate_inject, attr(deny(warnings))))]
3233

3334
#![feature(alloc)]
3435
#![feature(box_syntax)]

src/libcollections/binary_heap.rs

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ impl<T: Ord> BinaryHeap<T> {
233233
///
234234
/// ```
235235
/// #![feature(binary_heap_extras)]
236+
/// # #![allow(deprecated)]
236237
///
237238
/// use std::collections::BinaryHeap;
238239
/// let heap = BinaryHeap::from_vec(vec![9, 1, 2, 7, 3, 2]);

src/libcollections/borrow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ impl<T> ToOwned for T where T: Clone {
7272
/// ```
7373
/// use std::borrow::Cow;
7474
///
75+
/// # #[allow(dead_code)]
7576
/// fn abs_all(input: &mut Cow<[i32]>) {
7677
/// for i in 0..input.len() {
7778
/// let v = input[i];

src/libcollections/btree/set.rs

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ impl<T: Ord> BTreeSet<T> {
8989
/// # Examples
9090
///
9191
/// ```
92+
/// # #![allow(unused_mut)]
9293
/// use std::collections::BTreeSet;
9394
///
9495
/// let mut set: BTreeSet<i32> = BTreeSet::new();

src/libcollections/fmt.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
//! implement a method of the signature:
151151
//!
152152
//! ```
153+
//! # #![allow(dead_code)]
153154
//! # use std::fmt;
154155
//! # struct Foo; // our custom type
155156
//! # impl fmt::Display for Foo {
@@ -174,7 +175,6 @@
174175
//! like:
175176
//!
176177
//! ```
177-
//! #![feature(fmt_flags)]
178178
//! use std::fmt;
179179
//!
180180
//! #[derive(Debug)]
@@ -288,6 +288,7 @@
288288
//! off, some example usage is:
289289
//!
290290
//! ```
291+
//! # #![allow(unused_must_use)]
291292
//! use std::fmt;
292293
//! use std::io::{self, Write};
293294
//!

src/libcollections/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
html_root_url = "https://doc.rust-lang.org/nightly/",
2828
html_playground_url = "https://play.rust-lang.org/",
2929
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
30-
test(no_crate_inject))]
30+
test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
3131

3232
#![allow(trivial_casts)]
3333
#![cfg_attr(test, allow(deprecated))] // rand

src/libcollections/slice.rs

+1
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ pub trait SliceConcatExt<T: ?Sized> {
852852
/// # Examples
853853
///
854854
/// ```
855+
/// # #![allow(deprecated)]
855856
/// assert_eq!(["hello", "world"].connect(" "), "hello world");
856857
/// ```
857858
#[stable(feature = "rust1", since = "1.0.0")]

src/libcollections/str.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl str {
298298
/// done by `.chars()` or `.char_indices()`.
299299
///
300300
/// ```
301-
/// #![feature(str_char, core)]
301+
/// #![feature(str_char)]
302302
///
303303
/// use std::str::CharRange;
304304
///
@@ -358,7 +358,7 @@ impl str {
358358
/// done by `.chars().rev()` or `.char_indices()`.
359359
///
360360
/// ```
361-
/// #![feature(str_char, core)]
361+
/// #![feature(str_char)]
362362
///
363363
/// use std::str::CharRange;
364364
///
@@ -634,6 +634,7 @@ impl str {
634634
/// # Examples
635635
///
636636
/// ```
637+
/// # #![allow(deprecated)]
637638
/// let four_lines = "foo\r\nbar\n\r\nbaz";
638639
/// let v: Vec<&str> = four_lines.lines_any().collect();
639640
///
@@ -643,6 +644,7 @@ impl str {
643644
/// Leaving off the trailing character:
644645
///
645646
/// ```
647+
/// # #![allow(deprecated)]
646648
/// let four_lines = "foo\r\nbar\n\r\nbaz\n";
647649
/// let v: Vec<&str> = four_lines.lines_any().collect();
648650
///
@@ -1179,8 +1181,6 @@ impl str {
11791181
/// # Examples
11801182
///
11811183
/// ```
1182-
/// #![feature(str_match_indices)]
1183-
///
11841184
/// let v: Vec<_> = "abcXXXabcYYYabc".match_indices("abc").collect();
11851185
/// assert_eq!(v, [(0, "abc"), (6, "abc"), (12, "abc")]);
11861186
///
@@ -1216,8 +1216,6 @@ impl str {
12161216
/// # Examples
12171217
///
12181218
/// ```
1219-
/// #![feature(str_match_indices)]
1220-
///
12211219
/// let v: Vec<_> = "abcXXXabcYYYabc".rmatch_indices("abc").collect();
12221220
/// assert_eq!(v, [(12, "abc"), (6, "abc"), (0, "abc")]);
12231221
///

src/libcollections/string.rs

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl String {
5555
/// # Examples
5656
///
5757
/// ```
58+
/// # #![allow(unused_mut)]
5859
/// let mut s = String::new();
5960
/// ```
6061
#[inline]
@@ -73,6 +74,20 @@ impl String {
7374
///
7475
/// ```
7576
/// let mut s = String::with_capacity(10);
77+
///
78+
/// // The String contains no chars, even though it has capacity for more
79+
/// assert_eq!(s.len(), 0);
80+
///
81+
/// // These are all done without reallocating...
82+
/// let cap = s.capacity();
83+
/// for i in 0..10 {
84+
/// s.push('a');
85+
/// }
86+
///
87+
/// assert_eq!(s.capacity(), cap);
88+
///
89+
/// // ...but this may make the vector reallocate
90+
/// s.push('a');
7691
/// ```
7792
#[inline]
7893
#[stable(feature = "rust1", since = "1.0.0")]

src/libcollections/vec.rs

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ impl<T> Vec<T> {
242242
/// # Examples
243243
///
244244
/// ```
245+
/// # #![allow(unused_mut)]
245246
/// let mut vec: Vec<i32> = Vec::new();
246247
/// ```
247248
#[inline]

src/libcore/cell.rs

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
//! a trait method that was originally defined to take `&self`.
7777
//!
7878
//! ```
79+
//! # #![allow(dead_code)]
7980
//! use std::cell::RefCell;
8081
//!
8182
//! struct Graph {
@@ -125,6 +126,7 @@
125126
//! }
126127
//!
127128
//! struct RcBox<T> {
129+
//! # #[allow(dead_code)]
128130
//! value: T,
129131
//! refcount: Cell<usize>
130132
//! }
@@ -776,6 +778,7 @@ impl<'b, T: ?Sized> DerefMut for RefMut<'b, T> {
776778
/// use std::cell::UnsafeCell;
777779
/// use std::marker::Sync;
778780
///
781+
/// # #[allow(dead_code)]
779782
/// struct NotThreadSafe<T> {
780783
/// value: UnsafeCell<T>,
781784
/// }

src/libcore/cmp.rs

-4
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ impl Ordering {
140140
/// This method can be used to reverse a comparison:
141141
///
142142
/// ```
143-
/// use std::cmp::Ordering;
144-
///
145143
/// let mut data: &mut [_] = &mut [2, 10, 5, 8];
146144
///
147145
/// // sort the array from largest to smallest.
@@ -263,8 +261,6 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
263261
/// # Examples
264262
///
265263
/// ```
266-
/// use std::cmp::Ordering;
267-
///
268264
/// let result = 1.0 < 2.0;
269265
/// assert_eq!(result, true);
270266
///

src/libcore/default.rs

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! that define a set of options:
1616
//!
1717
//! ```
18+
//! # #[allow(dead_code)]
1819
//! struct SomeOptions {
1920
//! foo: i32,
2021
//! bar: f32,
@@ -24,6 +25,7 @@
2425
//! How can we define some default values? You can use `Default`:
2526
//!
2627
//! ```
28+
//! # #[allow(dead_code)]
2729
//! #[derive(Default)]
2830
//! struct SomeOptions {
2931
//! foo: i32,
@@ -40,6 +42,7 @@
4042
//! If you have your own type, you need to implement `Default` yourself:
4143
//!
4244
//! ```
45+
//! # #![allow(dead_code)]
4346
//! enum Kind {
4447
//! A,
4548
//! B,
@@ -66,6 +69,7 @@
6669
//! If you want to override a particular option, but still retain the other defaults:
6770
//!
6871
//! ```
72+
//! # #[allow(dead_code)]
6973
//! # #[derive(Default)]
7074
//! # struct SomeOptions {
7175
//! # foo: i32,
@@ -88,6 +92,7 @@ use marker::Sized;
8892
/// # Examples
8993
///
9094
/// ```
95+
/// # #[allow(dead_code)]
9196
/// #[derive(Default)]
9297
/// struct SomeOptions {
9398
/// foo: i32,
@@ -114,6 +119,7 @@ pub trait Default: Sized {
114119
/// Making your own:
115120
///
116121
/// ```
122+
/// # #[allow(dead_code)]
117123
/// enum Kind {
118124
/// A,
119125
/// B,

src/libcore/hash/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
//!
4646
//! struct Person {
4747
//! id: u32,
48+
//! # #[allow(dead_code)]
4849
//! name: String,
4950
//! phone: u64,
5051
//! }

src/libcore/intrinsics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ extern "rust-intrinsic" {
334334
/// use std::mem;
335335
/// use std::ptr;
336336
///
337+
/// # #[allow(dead_code)]
337338
/// fn swap<T>(x: &mut T, y: &mut T) {
338339
/// unsafe {
339340
/// // Give ourselves some scratch space to work with
@@ -372,6 +373,7 @@ extern "rust-intrinsic" {
372373
/// ```
373374
/// use std::ptr;
374375
///
376+
/// # #[allow(dead_code)]
375377
/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: usize) -> Vec<T> {
376378
/// let mut dst = Vec::with_capacity(elts);
377379
/// dst.set_len(elts);

src/libcore/iter.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
//! method calls a closure on each element it iterates over:
242242
//!
243243
//! ```
244+
//! # #![allow(unused_must_use)]
244245
//! let v = vec![1, 2, 3, 4, 5];
245246
//! v.iter().map(|x| println!("{}", x));
246247
//! ```
@@ -419,7 +420,7 @@ pub trait Iterator {
419420
///
420421
/// ```
421422
/// // an infinite iterator has no upper bound
422-
/// let iter = (0..);
423+
/// let iter = 0..;
423424
///
424425
/// assert_eq!((0, None), iter.size_hint());
425426
/// ```
@@ -709,6 +710,7 @@ pub trait Iterator {
709710
/// If you're doing some sort of side effect, prefer [`for`] to `map()`:
710711
///
711712
/// ```
713+
/// # #![allow(unused_must_use)]
712714
/// // don't do this:
713715
/// (0..5).map(|x| println!("{}", x));
714716
///
@@ -2695,7 +2697,7 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
26952697
///
26962698
/// ```
26972699
/// // a finite range knows exactly how many times it will iterate
2698-
/// let five = (0..5);
2700+
/// let five = 0..5;
26992701
///
27002702
/// assert_eq!(5, five.len());
27012703
/// ```
@@ -2761,7 +2763,7 @@ pub trait ExactSizeIterator: Iterator {
27612763
///
27622764
/// ```
27632765
/// // a finite range knows exactly how many times it will iterate
2764-
/// let five = (0..5);
2766+
/// let five = 0..5;
27652767
///
27662768
/// assert_eq!(5, five.len());
27672769
/// ```

src/libcore/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
html_root_url = "https://doc.rust-lang.org/nightly/",
6161
html_playground_url = "https://play.rust-lang.org/",
6262
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
63-
#![doc(test(no_crate_inject))]
63+
#![doc(test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
6464

6565
#![no_core]
6666
#![allow(raw_pointer_derive)]

src/libcore/macros.rs

+2
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ macro_rules! writeln {
247247
/// Match arms:
248248
///
249249
/// ```
250+
/// # #[allow(dead_code)]
250251
/// fn foo(x: Option<i32>) {
251252
/// match x {
252253
/// Some(n) if n >= 0 => println!("Some(Non-negative)"),
@@ -260,6 +261,7 @@ macro_rules! writeln {
260261
/// Iterators:
261262
///
262263
/// ```
264+
/// # #[allow(dead_code)]
263265
/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
264266
/// for i in 0.. {
265267
/// if 3*i < i { panic!("u32 overflow"); }

0 commit comments

Comments
 (0)