Skip to content

Commit 1dc9a50

Browse files
committed
Doctests: Enable running doc tests for pedantic lints
1 parent 5c1e30a commit 1dc9a50

16 files changed

+73
-27
lines changed

clippy_lints/src/checked_conversions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ declare_clippy_lint! {
2727
/// Could be written:
2828
///
2929
/// ```rust
30+
/// # use std::convert::TryFrom;
31+
/// # let foo = 1;
3032
/// # let _ =
3133
/// i32::try_from(foo).is_ok()
3234
/// # ;

clippy_lints/src/copy_iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare_clippy_lint! {
1313
/// **Known problems:** None.
1414
///
1515
/// **Example:**
16-
/// ```rust
16+
/// ```rust,ignore
1717
/// #[derive(Copy, Clone)]
1818
/// struct Countdown(u8);
1919
///

clippy_lints/src/derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ declare_clippy_lint! {
4949
/// **Known problems:** Bounds of generic types are sometimes wrong: https://github.com/rust-lang/rust/issues/26925
5050
///
5151
/// **Example:**
52-
/// ```rust
52+
/// ```rust,ignore
5353
/// #[derive(Copy)]
5454
/// struct Foo;
5555
///
5656
/// impl Clone for Foo {
57-
/// ..
57+
/// // ..
5858
/// }
5959
/// ```
6060
pub EXPL_IMPL_CLONE_ON_COPY,

clippy_lints/src/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare_clippy_lint! {
2727
/// /// Do something with the foo_bar parameter. See also
2828
/// /// that::other::module::foo.
2929
/// // ^ `foo_bar` and `that::other::module::foo` should be ticked.
30-
/// fn doit(foo_bar) { .. }
30+
/// fn doit(foo_bar: usize) {}
3131
/// ```
3232
pub DOC_MARKDOWN,
3333
pedantic,

clippy_lints/src/if_not_else.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ declare_clippy_lint! {
1717
///
1818
/// **Example:**
1919
/// ```rust
20+
/// # let v: Vec<usize> = vec![];
21+
/// # fn a() {}
22+
/// # fn b() {}
2023
/// if !v.is_empty() {
2124
/// a()
2225
/// } else {
@@ -27,6 +30,9 @@ declare_clippy_lint! {
2730
/// Could be written:
2831
///
2932
/// ```rust
33+
/// # let v: Vec<usize> = vec![];
34+
/// # fn a() {}
35+
/// # fn b() {}
3036
/// if v.is_empty() {
3137
/// b()
3238
/// } else {

clippy_lints/src/infinite_iter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ declare_clippy_lint! {
3434
///
3535
/// **Example:**
3636
/// ```rust
37-
/// [0..].iter().zip(infinite_iter.take_while(|x| x > 5))
37+
/// let infinite_iter = 0..;
38+
/// [0..].iter().zip(infinite_iter.take_while(|x| *x > 5));
3839
/// ```
3940
pub MAYBE_INFINITE_ITER,
4041
pedantic,

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ macro_rules! declare_clippy_lint {
110110
};
111111
{ $(#[$attr:meta])* pub $name:tt, pedantic, $description:tt } => {
112112
declare_tool_lint! {
113-
pub clippy::$name, Allow, $description, report_in_external_macro: true
113+
$(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
114114
}
115115
};
116116
{ $(#[$attr:meta])* pub $name:tt, restriction, $description:tt } => {

clippy_lints/src/loops.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,18 @@ declare_clippy_lint! {
9191
/// types.
9292
///
9393
/// **Example:**
94-
/// ```ignore
94+
/// ```rust
9595
/// // with `y` a `Vec` or slice:
96+
/// # let y = vec![1];
9697
/// for x in y.iter() {
97-
/// ..
98+
/// // ..
9899
/// }
99100
/// ```
100101
/// can be rewritten to
101102
/// ```rust
103+
/// # let y = vec![1];
102104
/// for x in &y {
103-
/// ..
105+
/// // ..
104106
/// }
105107
/// ```
106108
pub EXPLICIT_ITER_LOOP,
@@ -117,16 +119,18 @@ declare_clippy_lint! {
117119
/// **Known problems:** None
118120
///
119121
/// **Example:**
120-
/// ```ignore
122+
/// ```rust
123+
/// # let y = vec![1];
121124
/// // with `y` a `Vec` or slice:
122125
/// for x in y.into_iter() {
123-
/// ..
126+
/// // ..
124127
/// }
125128
/// ```
126129
/// can be rewritten to
127-
/// ```ignore
130+
/// ```rust
131+
/// # let y = vec![1];
128132
/// for x in y {
129-
/// ..
133+
/// // ..
130134
/// }
131135
/// ```
132136
pub EXPLICIT_INTO_ITER_LOOP,

clippy_lints/src/matches.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,25 @@ declare_clippy_lint! {
5353
/// Using `match`:
5454
///
5555
/// ```rust
56+
/// # fn bar(foo: &usize) {}
57+
/// # let other_ref: usize = 1;
58+
/// # let x: Option<&usize> = Some(&1);
5659
/// match x {
5760
/// Some(ref foo) => bar(foo),
58-
/// _ => bar(other_ref),
61+
/// _ => bar(&other_ref),
5962
/// }
6063
/// ```
6164
///
6265
/// Using `if let` with `else`:
6366
///
6467
/// ```rust
68+
/// # fn bar(foo: &usize) {}
69+
/// # let other_ref: usize = 1;
70+
/// # let x: Option<&usize> = Some(&1);
6571
/// if let Some(ref foo) = x {
6672
/// bar(foo);
6773
/// } else {
68-
/// bar(other_ref);
74+
/// bar(&other_ref);
6975
/// }
7076
/// ```
7177
pub SINGLE_MATCH_ELSE,

clippy_lints/src/methods/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ declare_clippy_lint! {
179179
///
180180
/// **Example:**
181181
/// ```rust
182-
/// x.map(|a| a + 1).unwrap_or(0)
182+
/// # let x = Some(1);
183+
/// x.map(|a| a + 1).unwrap_or(0);
183184
/// ```
184185
pub OPTION_MAP_UNWRAP_OR,
185186
pedantic,
@@ -196,7 +197,9 @@ declare_clippy_lint! {
196197
///
197198
/// **Example:**
198199
/// ```rust
199-
/// x.map(|a| a + 1).unwrap_or_else(some_function)
200+
/// # let x = Some(1);
201+
/// # fn some_function() -> usize { 1 }
202+
/// x.map(|a| a + 1).unwrap_or_else(some_function);
200203
/// ```
201204
pub OPTION_MAP_UNWRAP_OR_ELSE,
202205
pedantic,
@@ -213,7 +216,9 @@ declare_clippy_lint! {
213216
///
214217
/// **Example:**
215218
/// ```rust
216-
/// x.map(|a| a + 1).unwrap_or_else(some_function)
219+
/// # let x: Result<usize, ()> = Ok(1);
220+
/// # fn some_function(foo: ()) -> usize { 1 }
221+
/// x.map(|a| a + 1).unwrap_or_else(some_function);
217222
/// ```
218223
pub RESULT_MAP_UNWRAP_OR_ELSE,
219224
pedantic,
@@ -265,7 +270,8 @@ declare_clippy_lint! {
265270
///
266271
/// **Example:**
267272
/// ```rust
268-
/// iter.map(|x| x.iter()).flatten()
273+
/// let vec = vec![vec![1]];
274+
/// vec.iter().map(|x| x.iter()).flatten();
269275
/// ```
270276
pub MAP_FLATTEN,
271277
pedantic,
@@ -284,7 +290,8 @@ declare_clippy_lint! {
284290
///
285291
/// **Example:**
286292
/// ```rust
287-
/// iter.filter(|x| x == 0).map(|x| x * 2)
293+
/// let vec = vec![1];
294+
/// vec.iter().filter(|x| **x == 0).map(|x| *x * 2);
288295
/// ```
289296
pub FILTER_MAP,
290297
pedantic,
@@ -324,7 +331,7 @@ declare_clippy_lint! {
324331
///
325332
/// **Example:**
326333
/// ```rust
327-
/// (0..3).find(|x| x == 2).map(|x| x * 2);
334+
/// (0..3).find(|x| *x == 2).map(|x| x * 2);
328335
/// ```
329336
/// Can be written as
330337
/// ```rust

clippy_lints/src/mut_mut.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ declare_clippy_lint! {
1616
///
1717
/// **Example:**
1818
/// ```rust
19+
/// # let mut y = 1;
1920
/// let x = &mut &mut y;
2021
/// ```
2122
pub MUT_MUT,

clippy_lints/src/needless_continue.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ declare_clippy_lint! {
5757
///
5858
/// **Example:**
5959
/// ```rust
60+
/// # fn condition() -> bool { false }
61+
/// # fn update_condition() {}
62+
/// # let x = false;
6063
/// while condition() {
6164
/// update_condition();
6265
/// if x {
@@ -71,6 +74,9 @@ declare_clippy_lint! {
7174
/// Could be rewritten as
7275
///
7376
/// ```rust
77+
/// # fn condition() -> bool { false }
78+
/// # fn update_condition() {}
79+
/// # let x = false;
7480
/// while condition() {
7581
/// update_condition();
7682
/// if x {
@@ -83,22 +89,26 @@ declare_clippy_lint! {
8389
/// As another example, the following code
8490
///
8591
/// ```rust
92+
/// # fn waiting() -> bool { false }
8693
/// loop {
8794
/// if waiting() {
8895
/// continue;
8996
/// } else {
9097
/// // Do something useful
9198
/// }
99+
/// # break;
92100
/// }
93101
/// ```
94102
/// Could be rewritten as
95103
///
96104
/// ```rust
105+
/// # fn waiting() -> bool { false }
97106
/// loop {
98107
/// if waiting() {
99108
/// continue;
100109
/// }
101110
/// // Do something useful
111+
/// # break;
102112
/// }
103113
/// ```
104114
pub NEEDLESS_CONTINUE,

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ declare_clippy_lint! {
4040
/// fn foo(v: Vec<i32>) {
4141
/// assert_eq!(v.len(), 42);
4242
/// }
43+
/// ```
44+
///
45+
/// ```rust
4346
/// // should be
4447
/// fn foo(v: &[i32]) {
4548
/// assert_eq!(v.len(), 42);

clippy_lints/src/replace_consts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ declare_clippy_lint! {
1616
///
1717
/// **Example:**
1818
/// ```rust
19+
/// # use core::sync::atomic::{ATOMIC_ISIZE_INIT, AtomicIsize};
1920
/// static FOO: AtomicIsize = ATOMIC_ISIZE_INIT;
2021
/// ```
2122
///
2223
/// Could be written:
2324
///
2425
/// ```rust
26+
/// # use core::sync::atomic::AtomicIsize;
2527
/// static FOO: AtomicIsize = AtomicIsize::new(0);
2628
/// ```
2729
pub REPLACE_CONSTS,

clippy_lints/src/shadow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ declare_clippy_lint! {
6767
///
6868
/// **Example:**
6969
/// ```rust
70+
/// # let y = 1;
71+
/// # let z = 2;
7072
/// let x = y;
7173
/// let x = z; // shadows the earlier binding
7274
/// ```

clippy_lints/src/types.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ declare_clippy_lint! {
133133
///
134134
/// **Example:**
135135
/// ```rust
136-
/// let x = LinkedList::new();
136+
/// # use std::collections::LinkedList;
137+
/// let x: LinkedList<usize> = LinkedList::new();
137138
/// ```
138139
pub LINKEDLIST,
139140
pedantic,
@@ -660,8 +661,8 @@ declare_clippy_lint! {
660661
///
661662
/// **Example:**
662663
/// ```rust
663-
/// let x = u64::MAX;
664-
/// x as f64
664+
/// let x = std::u64::MAX;
665+
/// x as f64;
665666
/// ```
666667
pub CAST_PRECISION_LOSS,
667668
pedantic,
@@ -682,7 +683,7 @@ declare_clippy_lint! {
682683
/// **Example:**
683684
/// ```rust
684685
/// let y: i8 = -1;
685-
/// y as u128 // will return 18446744073709551615
686+
/// y as u128; // will return 18446744073709551615
686687
/// ```
687688
pub CAST_SIGN_LOSS,
688689
pedantic,
@@ -727,7 +728,7 @@ declare_clippy_lint! {
727728
///
728729
/// **Example:**
729730
/// ```rust
730-
/// u32::MAX as i32 // will yield a value of `-1`
731+
/// std::u32::MAX as i32; // will yield a value of `-1`
731732
/// ```
732733
pub CAST_POSSIBLE_WRAP,
733734
pedantic,
@@ -1689,7 +1690,8 @@ declare_clippy_lint! {
16891690
///
16901691
/// **Example:**
16911692
/// ```rust
1692-
/// let x : u8 = ...; (x as u32) > 300
1693+
/// let x: u8 = 1;
1694+
/// (x as u32) > 300;
16931695
/// ```
16941696
pub INVALID_UPCAST_COMPARISONS,
16951697
pedantic,

0 commit comments

Comments
 (0)