Skip to content

Commit 5c1e30a

Browse files
committed
Auto merge of #4327 - phansch:doctests_perf, r=flip1995
Doctests: Enable running doc tests for perf lints changelog: none This should be possible to merge independently of #4325 cc #4319
2 parents 3dc9183 + c0cdfd2 commit 5c1e30a

10 files changed

+65
-32
lines changed

clippy_lints/src/bytecount.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ declare_clippy_lint! {
2424
/// **Example:**
2525
///
2626
/// ```rust
27-
/// &my_data.filter(|&x| x == 0u8).count() // use bytecount::count instead
27+
/// # let vec = vec![1_u8];
28+
/// &vec.iter().filter(|x| **x == 0u8).count(); // use bytecount::count instead
2829
/// ```
2930
pub NAIVE_BYTECOUNT,
3031
perf,

clippy_lints/src/entry.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,32 @@ declare_clippy_lint! {
1616
///
1717
/// **Known problems:** Some false negatives, eg.:
1818
/// ```rust
19-
/// let k = &key;
20-
/// if !m.contains_key(k) {
21-
/// m.insert(k.clone(), v);
19+
/// # use std::collections::HashMap;
20+
/// # let mut map = HashMap::new();
21+
/// # let v = 1;
22+
/// # let k = 1;
23+
/// if !map.contains_key(&k) {
24+
/// map.insert(k.clone(), v);
2225
/// }
2326
/// ```
2427
///
2528
/// **Example:**
2629
/// ```rust
27-
/// if !m.contains_key(&k) {
28-
/// m.insert(k, v)
30+
/// # use std::collections::HashMap;
31+
/// # let mut map = HashMap::new();
32+
/// # let k = 1;
33+
/// # let v = 1;
34+
/// if !map.contains_key(&k) {
35+
/// map.insert(k, v);
2936
/// }
3037
/// ```
31-
/// can be rewritten as:
38+
/// can both be rewritten as:
3239
/// ```rust
33-
/// m.entry(k).or_insert(v);
40+
/// # use std::collections::HashMap;
41+
/// # let mut map = HashMap::new();
42+
/// # let k = 1;
43+
/// # let v = 1;
44+
/// map.entry(k).or_insert(v);
3445
/// ```
3546
pub MAP_ENTRY,
3647
perf,

clippy_lints/src/escape.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ declare_clippy_lint! {
2828
///
2929
/// **Example:**
3030
/// ```rust
31-
/// fn main() {
32-
/// let x = Box::new(1);
33-
/// foo(*x);
34-
/// println!("{}", *x);
35-
/// }
31+
/// # fn foo(bar: usize) {}
32+
/// let x = Box::new(1);
33+
/// foo(*x);
34+
/// println!("{}", *x);
3635
/// ```
3736
pub BOXED_LOCAL,
3837
perf,

clippy_lints/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ macro_rules! declare_clippy_lint {
105105
};
106106
{ $(#[$attr:meta])* pub $name:tt, perf, $description:tt } => {
107107
declare_tool_lint! {
108-
pub clippy::$name, Warn, $description, report_in_external_macro: true
108+
$(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true
109109
}
110110
};
111111
{ $(#[$attr:meta])* pub $name:tt, pedantic, $description:tt } => {
@@ -120,7 +120,7 @@ macro_rules! declare_clippy_lint {
120120
};
121121
{ $(#[$attr:meta])* pub $name:tt, cargo, $description:tt } => {
122122
declare_tool_lint! {
123-
pub clippy::$name, Allow, $description, report_in_external_macro: true
123+
$(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true
124124
}
125125
};
126126
{ $(#[$attr:meta])* pub $name:tt, nursery, $description:tt } => {

clippy_lints/src/loops.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ declare_clippy_lint! {
4141
/// **Known problems:** None.
4242
///
4343
/// **Example:**
44-
/// ```ignore
44+
/// ```rust
45+
/// # let src = vec![1];
46+
/// # let mut dst = vec![0; 65];
4547
/// for i in 0..src.len() {
4648
/// dst[i + 64] = src[i];
4749
/// }
@@ -265,8 +267,9 @@ declare_clippy_lint! {
265267
/// None
266268
///
267269
/// **Example:**
268-
/// ```ignore
269-
/// let len = iterator.collect::<Vec<_>>().len();
270+
/// ```rust
271+
/// # let iterator = vec![1].into_iter();
272+
/// let len = iterator.clone().collect::<Vec<_>>().len();
270273
/// // should be
271274
/// let len = iterator.count();
272275
/// ```

clippy_lints/src/methods/mod.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,18 @@ declare_clippy_lint! {
386386
///
387387
/// **Example:**
388388
/// ```rust
389-
/// foo.unwrap_or(String::new())
389+
/// # let foo = Some(String::new());
390+
/// foo.unwrap_or(String::new());
390391
/// ```
391392
/// this can instead be written:
392393
/// ```rust
393-
/// foo.unwrap_or_else(String::new)
394+
/// # let foo = Some(String::new());
395+
/// foo.unwrap_or_else(String::new);
394396
/// ```
395397
/// or
396398
/// ```rust
397-
/// foo.unwrap_or_default()
399+
/// # let foo = Some(String::new());
400+
/// foo.unwrap_or_default();
398401
/// ```
399402
pub OR_FUN_CALL,
400403
perf,
@@ -412,15 +415,24 @@ declare_clippy_lint! {
412415
///
413416
/// **Example:**
414417
/// ```rust
415-
/// foo.expect(&format!("Err {}: {}", err_code, err_msg))
418+
/// # let foo = Some(String::new());
419+
/// # let err_code = "418";
420+
/// # let err_msg = "I'm a teapot";
421+
/// foo.expect(&format!("Err {}: {}", err_code, err_msg));
416422
/// ```
417423
/// or
418424
/// ```rust
419-
/// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str())
425+
/// # let foo = Some(String::new());
426+
/// # let err_code = "418";
427+
/// # let err_msg = "I'm a teapot";
428+
/// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str());
420429
/// ```
421430
/// this can instead be written:
422431
/// ```rust
423-
/// foo.unwrap_or_else(|_| panic!("Err {}: {}", err_code, err_msg))
432+
/// # let foo = Some(String::new());
433+
/// # let err_code = "418";
434+
/// # let err_msg = "I'm a teapot";
435+
/// foo.unwrap_or_else(|| panic!("Err {}: {}", err_code, err_msg));
424436
/// ```
425437
pub EXPECT_FUN_CALL,
426438
perf,

clippy_lints/src/misc.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ declare_clippy_lint! {
101101
///
102102
/// **Example:**
103103
/// ```rust
104-
/// x.to_owned() == y
104+
/// # let x = "foo";
105+
/// # let y = String::from("foo");
106+
/// if x.to_owned() == y {}
105107
/// ```
106108
pub CMP_OWNED,
107109
perf,

clippy_lints/src/mutex_atomic.rs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ declare_clippy_lint! {
2222
///
2323
/// **Example:**
2424
/// ```rust
25+
/// # use std::sync::Mutex;
26+
/// # let y = 1;
2527
/// let x = Mutex::new(&y);
2628
/// ```
2729
pub MUTEX_ATOMIC,

clippy_lints/src/slow_vector_initialization.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ declare_clippy_lint! {
1919
///
2020
/// **Example:**
2121
/// ```rust
22+
/// # use core::iter::repeat;
23+
/// # let len = 4;
2224
/// let mut vec1 = Vec::with_capacity(len);
2325
/// vec1.resize(len, 0);
2426
///

clippy_lints/src/trivially_copy_pass_by_ref.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ declare_clippy_lint! {
3939
/// each other.
4040
///
4141
/// **Example:**
42+
///
43+
/// ```rust
44+
/// // Bad
45+
/// fn foo(v: &u32) {}
46+
/// ```
47+
///
4248
/// ```rust
43-
/// fn foo(v: &u32) {
44-
/// assert_eq!(v, 42);
45-
/// }
46-
/// // should be
47-
/// fn foo(v: u32) {
48-
/// assert_eq!(v, 42);
49-
/// }
49+
/// // Better
50+
/// fn foo(v: u32) {}
5051
/// ```
5152
pub TRIVIALLY_COPY_PASS_BY_REF,
5253
perf,

0 commit comments

Comments
 (0)