Skip to content

Commit c0cdfd2

Browse files
committed
Doctests: Enable running doc tests for perf lints
This should be possible to merge independently of #4325 cc #4319
1 parent 18a7dce commit c0cdfd2

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
/// }
@@ -264,8 +266,9 @@ declare_clippy_lint! {
264266
/// None
265267
///
266268
/// **Example:**
267-
/// ```ignore
268-
/// let len = iterator.collect::<Vec<_>>().len();
269+
/// ```rust
270+
/// # let iterator = vec![1].into_iter();
271+
/// let len = iterator.clone().collect::<Vec<_>>().len();
269272
/// // should be
270273
/// let len = iterator.count();
271274
/// ```

clippy_lints/src/methods/mod.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,18 @@ declare_clippy_lint! {
383383
///
384384
/// **Example:**
385385
/// ```rust
386-
/// foo.unwrap_or(String::new())
386+
/// # let foo = Some(String::new());
387+
/// foo.unwrap_or(String::new());
387388
/// ```
388389
/// this can instead be written:
389390
/// ```rust
390-
/// foo.unwrap_or_else(String::new)
391+
/// # let foo = Some(String::new());
392+
/// foo.unwrap_or_else(String::new);
391393
/// ```
392394
/// or
393395
/// ```rust
394-
/// foo.unwrap_or_default()
396+
/// # let foo = Some(String::new());
397+
/// foo.unwrap_or_default();
395398
/// ```
396399
pub OR_FUN_CALL,
397400
perf,
@@ -409,15 +412,24 @@ declare_clippy_lint! {
409412
///
410413
/// **Example:**
411414
/// ```rust
412-
/// foo.expect(&format!("Err {}: {}", err_code, err_msg))
415+
/// # let foo = Some(String::new());
416+
/// # let err_code = "418";
417+
/// # let err_msg = "I'm a teapot";
418+
/// foo.expect(&format!("Err {}: {}", err_code, err_msg));
413419
/// ```
414420
/// or
415421
/// ```rust
416-
/// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str())
422+
/// # let foo = Some(String::new());
423+
/// # let err_code = "418";
424+
/// # let err_msg = "I'm a teapot";
425+
/// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str());
417426
/// ```
418427
/// this can instead be written:
419428
/// ```rust
420-
/// foo.unwrap_or_else(|_| panic!("Err {}: {}", err_code, err_msg))
429+
/// # let foo = Some(String::new());
430+
/// # let err_code = "418";
431+
/// # let err_msg = "I'm a teapot";
432+
/// foo.unwrap_or_else(|| panic!("Err {}: {}", err_code, err_msg));
421433
/// ```
422434
pub EXPECT_FUN_CALL,
423435
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)