Fix more code examples

This commit is contained in:
ThibsG 2020-05-29 18:15:42 +02:00
parent 61587c9a29
commit 262c9dc025
8 changed files with 51 additions and 21 deletions

View File

@ -18,13 +18,24 @@
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// struct Foo(i32);
///
/// // Bad
/// impl From<String> for Foo {
/// fn from(s: String) -> Self {
/// Foo(s.parse().unwrap())
/// }
/// }
///
/// // Good
/// use std::convert::TryFrom;
/// impl TryFrom<String> for Foo {
/// type Error = ();
/// fn try_from(s: String) -> Result<Self, Self::Error> {
/// s.parse()
/// }
/// }
/// ```
pub FALLIBLE_IMPL_FROM,
nursery,

View File

@ -28,7 +28,6 @@
/// **Example:**
///
/// ```rust
///
/// let a = 3f32;
/// let _ = a.powf(1.0 / 3.0);
/// let _ = (1.0 + a).ln();
@ -38,7 +37,6 @@
/// is better expressed as
///
/// ```rust
///
/// let a = 3f32;
/// let _ = a.cbrt();
/// let _ = a.ln_1p();

View File

@ -25,9 +25,13 @@
///
/// **Examples:**
/// ```rust
///
/// // Bad
/// # let foo = "foo";
/// format!("foo");
/// format!("{}", foo);
///
/// // Good
/// format!("foo");
/// ```
pub USELESS_FORMAT,
complexity,

View File

@ -49,7 +49,7 @@
/// **Known problems:** None.
///
/// **Example:**
/// ``` rust
/// ```rust
/// fn im_too_long() {
/// println!("");
/// // ... 100 more LoC
@ -79,10 +79,16 @@
/// `some_argument.get_raw_ptr()`).
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// // Bad
/// pub fn foo(x: *const u8) {
/// println!("{}", unsafe { *x });
/// }
///
/// // Good
/// pub unsafe fn foo(x: *const u8) {
/// println!("{}", unsafe { *x });
/// }
/// ```
pub NOT_UNSAFE_PTR_ARG_DEREF,
correctness,

View File

@ -25,13 +25,6 @@
/// if i != 0 {
/// i -= 1;
/// }
/// ```
/// Use instead:
/// ```rust
/// let end: u32 = 10;
/// let start: u32 = 5;
///
/// let mut i: u32 = end - start;
///
/// // Good
/// i = i.saturating_sub(1);

View File

@ -10,7 +10,6 @@
declare_clippy_lint! {
/// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
///
///
/// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
///
/// **Known problems:** None.

View File

@ -15,10 +15,13 @@
///
/// **Example:**
/// ```rust
/// fn main() {
/// // Bad
/// let x = 3 / 2;
/// println!("{}", x);
/// }
///
/// // Good
/// let x = 3f32 / 2f32;
/// println!("{}", x);
/// ```
pub INTEGER_DIVISION,
restriction,

View File

@ -16,6 +16,7 @@
///
/// **Example:**
/// ```rust
/// // Bad
/// fn foo() {
/// println!("cake");
/// }
@ -28,6 +29,21 @@
/// foo(); // prints "foo"
/// }
/// ```
///
/// ```rust
/// // Good
/// fn foo() {
/// println!("cake");
/// }
///
/// fn main() {
/// fn foo() {
/// println!("foo");
/// }
/// foo(); // prints "foo"
/// foo(); // prints "foo"
/// }
/// ```
pub ITEMS_AFTER_STATEMENTS,
pedantic,
"blocks where an item comes after a statement"