diff --git a/clippy_lints/src/bytecount.rs b/clippy_lints/src/bytecount.rs index 984858382614..ab9cf9512617 100644 --- a/clippy_lints/src/bytecount.rs +++ b/clippy_lints/src/bytecount.rs @@ -24,7 +24,8 @@ declare_clippy_lint! { /// **Example:** /// /// ```rust - /// &my_data.filter(|&x| x == 0u8).count() // use bytecount::count instead + /// # let vec = vec![1_u8]; + /// &vec.iter().filter(|x| **x == 0u8).count(); // use bytecount::count instead /// ``` pub NAIVE_BYTECOUNT, perf, diff --git a/clippy_lints/src/entry.rs b/clippy_lints/src/entry.rs index 83f78b2d72d6..e6b09f89b353 100644 --- a/clippy_lints/src/entry.rs +++ b/clippy_lints/src/entry.rs @@ -16,21 +16,32 @@ declare_clippy_lint! { /// /// **Known problems:** Some false negatives, eg.: /// ```rust - /// let k = &key; - /// if !m.contains_key(k) { - /// m.insert(k.clone(), v); + /// # use std::collections::HashMap; + /// # let mut map = HashMap::new(); + /// # let v = 1; + /// # let k = 1; + /// if !map.contains_key(&k) { + /// map.insert(k.clone(), v); /// } /// ``` /// /// **Example:** /// ```rust - /// if !m.contains_key(&k) { - /// m.insert(k, v) + /// # use std::collections::HashMap; + /// # let mut map = HashMap::new(); + /// # let k = 1; + /// # let v = 1; + /// if !map.contains_key(&k) { + /// map.insert(k, v); /// } /// ``` - /// can be rewritten as: + /// can both be rewritten as: /// ```rust - /// m.entry(k).or_insert(v); + /// # use std::collections::HashMap; + /// # let mut map = HashMap::new(); + /// # let k = 1; + /// # let v = 1; + /// map.entry(k).or_insert(v); /// ``` pub MAP_ENTRY, perf, diff --git a/clippy_lints/src/escape.rs b/clippy_lints/src/escape.rs index f276014f43ad..c37efbd6e49d 100644 --- a/clippy_lints/src/escape.rs +++ b/clippy_lints/src/escape.rs @@ -28,11 +28,10 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// fn main() { - /// let x = Box::new(1); - /// foo(*x); - /// println!("{}", *x); - /// } + /// # fn foo(bar: usize) {} + /// let x = Box::new(1); + /// foo(*x); + /// println!("{}", *x); /// ``` pub BOXED_LOCAL, perf, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 39e55a9558bb..86a32a69e6e3 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -105,7 +105,7 @@ macro_rules! declare_clippy_lint { }; { $(#[$attr:meta])* pub $name:tt, perf, $description:tt } => { declare_tool_lint! { - pub clippy::$name, Warn, $description, report_in_external_macro: true + $(#[$attr])* pub clippy::$name, Warn, $description, report_in_external_macro: true } }; { $(#[$attr:meta])* pub $name:tt, pedantic, $description:tt } => { @@ -120,7 +120,7 @@ macro_rules! declare_clippy_lint { }; { $(#[$attr:meta])* pub $name:tt, cargo, $description:tt } => { declare_tool_lint! { - pub clippy::$name, Allow, $description, report_in_external_macro: true + $(#[$attr])* pub clippy::$name, Allow, $description, report_in_external_macro: true } }; { $(#[$attr:meta])* pub $name:tt, nursery, $description:tt } => { diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 3c66c74324ea..e1a949c04c1a 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -41,7 +41,9 @@ declare_clippy_lint! { /// **Known problems:** None. /// /// **Example:** - /// ```ignore + /// ```rust + /// # let src = vec![1]; + /// # let mut dst = vec![0; 65]; /// for i in 0..src.len() { /// dst[i + 64] = src[i]; /// } @@ -265,8 +267,9 @@ declare_clippy_lint! { /// None /// /// **Example:** - /// ```ignore - /// let len = iterator.collect::>().len(); + /// ```rust + /// # let iterator = vec![1].into_iter(); + /// let len = iterator.clone().collect::>().len(); /// // should be /// let len = iterator.count(); /// ``` diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 473f3892f22f..2a52ad58df83 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -386,15 +386,18 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// foo.unwrap_or(String::new()) + /// # let foo = Some(String::new()); + /// foo.unwrap_or(String::new()); /// ``` /// this can instead be written: /// ```rust - /// foo.unwrap_or_else(String::new) + /// # let foo = Some(String::new()); + /// foo.unwrap_or_else(String::new); /// ``` /// or /// ```rust - /// foo.unwrap_or_default() + /// # let foo = Some(String::new()); + /// foo.unwrap_or_default(); /// ``` pub OR_FUN_CALL, perf, @@ -412,15 +415,24 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// foo.expect(&format!("Err {}: {}", err_code, err_msg)) + /// # let foo = Some(String::new()); + /// # let err_code = "418"; + /// # let err_msg = "I'm a teapot"; + /// foo.expect(&format!("Err {}: {}", err_code, err_msg)); /// ``` /// or /// ```rust - /// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str()) + /// # let foo = Some(String::new()); + /// # let err_code = "418"; + /// # let err_msg = "I'm a teapot"; + /// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str()); /// ``` /// this can instead be written: /// ```rust - /// foo.unwrap_or_else(|_| panic!("Err {}: {}", err_code, err_msg)) + /// # let foo = Some(String::new()); + /// # let err_code = "418"; + /// # let err_msg = "I'm a teapot"; + /// foo.unwrap_or_else(|| panic!("Err {}: {}", err_code, err_msg)); /// ``` pub EXPECT_FUN_CALL, perf, diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index 6d24aa008fb9..dec3cd0fb42d 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -101,7 +101,9 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// x.to_owned() == y + /// # let x = "foo"; + /// # let y = String::from("foo"); + /// if x.to_owned() == y {} /// ``` pub CMP_OWNED, perf, diff --git a/clippy_lints/src/mutex_atomic.rs b/clippy_lints/src/mutex_atomic.rs index 3b9641b354f2..bf76e0ff369a 100644 --- a/clippy_lints/src/mutex_atomic.rs +++ b/clippy_lints/src/mutex_atomic.rs @@ -22,6 +22,8 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// # use std::sync::Mutex; + /// # let y = 1; /// let x = Mutex::new(&y); /// ``` pub MUTEX_ATOMIC, diff --git a/clippy_lints/src/slow_vector_initialization.rs b/clippy_lints/src/slow_vector_initialization.rs index dde9412c52e8..7e543bfe60f7 100644 --- a/clippy_lints/src/slow_vector_initialization.rs +++ b/clippy_lints/src/slow_vector_initialization.rs @@ -19,6 +19,8 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// # use core::iter::repeat; + /// # let len = 4; /// let mut vec1 = Vec::with_capacity(len); /// vec1.resize(len, 0); /// diff --git a/clippy_lints/src/trivially_copy_pass_by_ref.rs b/clippy_lints/src/trivially_copy_pass_by_ref.rs index 4e07a07ccc27..be79fc2fb81f 100644 --- a/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -39,14 +39,15 @@ declare_clippy_lint! { /// each other. /// /// **Example:** + /// + /// ```rust + /// // Bad + /// fn foo(v: &u32) {} + /// ``` + /// /// ```rust - /// fn foo(v: &u32) { - /// assert_eq!(v, 42); - /// } - /// // should be - /// fn foo(v: u32) { - /// assert_eq!(v, 42); - /// } + /// // Better + /// fn foo(v: u32) {} /// ``` pub TRIVIALLY_COPY_PASS_BY_REF, perf,