From b608e02e1cedb99ee8a069e3647dde5316b41968 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sat, 3 Aug 2019 21:24:50 +0200 Subject: [PATCH] Doctests: Enable running doc tests for restriction lints --- clippy_lints/src/arithmetic.rs | 6 ++-- clippy_lints/src/else_if_without_else.rs | 6 ++++ clippy_lints/src/indexing_slicing.rs | 2 +- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/matches.rs | 2 ++ clippy_lints/src/mem_forget.rs | 2 ++ clippy_lints/src/methods/mod.rs | 36 ++++++++++++++++++++---- clippy_lints/src/misc.rs | 5 ++-- clippy_lints/src/missing_inline.rs | 6 ++-- clippy_lints/src/shadow.rs | 3 ++ clippy_lints/src/strings.rs | 2 +- clippy_lints/src/write.rs | 1 + 12 files changed, 57 insertions(+), 16 deletions(-) diff --git a/clippy_lints/src/arithmetic.rs b/clippy_lints/src/arithmetic.rs index d94cd668b..9e0d11456 100644 --- a/clippy_lints/src/arithmetic.rs +++ b/clippy_lints/src/arithmetic.rs @@ -16,7 +16,8 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// a + 1 + /// # let a = 0; + /// a + 1; /// ``` pub INTEGER_ARITHMETIC, restriction, @@ -33,7 +34,8 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// a + 1.0 + /// # let a = 0.0; + /// a + 1.0; /// ``` pub FLOAT_ARITHMETIC, restriction, diff --git a/clippy_lints/src/else_if_without_else.rs b/clippy_lints/src/else_if_without_else.rs index 25a1cde6e..6daf204a5 100644 --- a/clippy_lints/src/else_if_without_else.rs +++ b/clippy_lints/src/else_if_without_else.rs @@ -16,6 +16,9 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// # fn a() {} + /// # fn b() {} + /// # let x: i32 = 1; /// if x.is_positive() { /// a(); /// } else if x.is_negative() { @@ -26,6 +29,9 @@ declare_clippy_lint! { /// Could be written: /// /// ```rust + /// # fn a() {} + /// # fn b() {} + /// # let x: i32 = 1; /// if x.is_positive() { /// a(); /// } else if x.is_negative() { diff --git a/clippy_lints/src/indexing_slicing.rs b/clippy_lints/src/indexing_slicing.rs index d90efd2ea..06e5458c1 100644 --- a/clippy_lints/src/indexing_slicing.rs +++ b/clippy_lints/src/indexing_slicing.rs @@ -47,7 +47,7 @@ declare_clippy_lint! { /// **Known problems:** Hopefully none. /// /// **Example:** - /// ```rust + /// ```rust,no_run /// // Vector /// let x = vec![0; 5]; /// diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 86a32a69e..ac983b5a2 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -115,7 +115,7 @@ macro_rules! declare_clippy_lint { }; { $(#[$attr:meta])* pub $name:tt, restriction, $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, cargo, $description:tt } => { diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 566082c9b..b0a4d2691 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -205,6 +205,8 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// # enum Foo { A(usize), B(usize) } + /// # let x = Foo::B(1); /// match x { /// A => {}, /// _ => {}, diff --git a/clippy_lints/src/mem_forget.rs b/clippy_lints/src/mem_forget.rs index 55673c430..54b0a6193 100644 --- a/clippy_lints/src/mem_forget.rs +++ b/clippy_lints/src/mem_forget.rs @@ -14,6 +14,8 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// # use std::mem; + /// # use std::rc::Rc; /// mem::forget(Rc::new(55)) /// ``` pub MEM_FORGET, diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 2a52ad58d..476bf3b56 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -40,8 +40,19 @@ declare_clippy_lint! { /// **Known problems:** None. /// /// **Example:** + /// + /// Using unwrap on an `Option`: + /// /// ```rust - /// x.unwrap() + /// let opt = Some(1); + /// opt.unwrap(); + /// ``` + /// + /// Better: + /// + /// ```rust + /// let opt = Some(1); + /// opt.expect("more helpful message"); /// ``` pub OPTION_UNWRAP_USED, restriction, @@ -62,8 +73,18 @@ declare_clippy_lint! { /// **Known problems:** None. /// /// **Example:** + /// Using unwrap on an `Option`: + /// /// ```rust - /// x.unwrap() + /// let res: Result = Ok(1); + /// res.unwrap(); + /// ``` + /// + /// Better: + /// + /// ```rust + /// let res: Result = Ok(1); + /// res.expect("more helpful message"); /// ``` pub RESULT_UNWRAP_USED, restriction, @@ -141,9 +162,10 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// impl X { - /// pub fn as_str(self) -> &str { - /// .. + /// # struct X; + /// impl<'a> X { + /// pub fn as_str(self) -> &'a str { + /// "foo" /// } /// } /// ``` @@ -467,7 +489,9 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// x.clone() + /// # use std::rc::Rc; + /// let x = Rc::new(1); + /// x.clone(); /// ``` pub CLONE_ON_REF_PTR, restriction, diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index dec3cd0fb..f8a249829 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -226,8 +226,9 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust - /// const ONE = 1.00f64; - /// x == ONE // where both are floats + /// let x: f64 = 1.0; + /// const ONE: f64 = 1.00; + /// x == ONE; // where both are floats /// ``` pub FLOAT_CMP_CONST, restriction, diff --git a/clippy_lints/src/missing_inline.rs b/clippy_lints/src/missing_inline.rs index c467f2fc3..115bde344 100644 --- a/clippy_lints/src/missing_inline.rs +++ b/clippy_lints/src/missing_inline.rs @@ -33,7 +33,7 @@ declare_clippy_lint! { /// /// struct Baz; /// impl Baz { - /// fn priv() {} // ok + /// fn private() {} // ok /// } /// /// impl Bar for Baz { @@ -42,8 +42,8 @@ declare_clippy_lint! { /// /// pub struct PubBaz; /// impl PubBaz { - /// fn priv() {} // ok - /// pub not_ptriv() {} // missing #[inline] + /// fn private() {} // ok + /// pub fn not_ptrivate() {} // missing #[inline] /// } /// /// impl Bar for PubBaz { diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index c75e33e84..b5413b388 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -20,6 +20,7 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// # let x = 1; /// let x = &x; /// ``` pub SHADOW_SAME, @@ -41,10 +42,12 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// let x = 2; /// let x = x + 1; /// ``` /// use different variable name: /// ```rust + /// let x = 2; /// let y = x + 1; /// ``` pub SHADOW_REUSE, diff --git a/clippy_lints/src/strings.rs b/clippy_lints/src/strings.rs index 57f63a600..0de63b5ff 100644 --- a/clippy_lints/src/strings.rs +++ b/clippy_lints/src/strings.rs @@ -48,7 +48,7 @@ declare_clippy_lint! { /// /// ```rust /// let x = "Hello".to_owned(); - /// x + ", World" + /// x + ", World"; /// ``` pub STRING_ADD, restriction, diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 9b30708b2..9e26b3e5b 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -77,6 +77,7 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust + /// # let foo = "bar"; /// println!("{:?}", foo); /// ``` pub USE_DEBUG,