Skip to content

Commit

Permalink
Add more corrected code for doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibsG committed May 31, 2020
1 parent 86814c5 commit 0f0f89d
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 25 deletions.
65 changes: 50 additions & 15 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # let x = Ok::<_, ()>(());
/// x.ok().expect("why did I do this again?")
///
/// // Bad
/// x.ok().expect("why did I do this again?");
///
/// // Good
/// x.expect("why did I do this again?");
/// ```
pub OK_EXPECT,
style,
Expand Down Expand Up @@ -273,8 +278,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # let opt = Some(1);
/// opt.map_or(None, |a| Some(a + 1))
/// # ;
///
/// // Bad
/// opt.map_or(None, |a| Some(a + 1));
///
/// // Good
/// opt.and_then(|a| Some(a + 1));
/// ```
pub OPTION_MAP_OR_NONE,
style,
Expand Down Expand Up @@ -390,14 +399,19 @@ declare_clippy_lint! {
/// **What it does:** Checks for usage of `_.map(_).flatten(_)`,
///
/// **Why is this bad?** Readability, this can be written more concisely as a
/// single method call.
/// single method call using `_.flat_map(_)`
///
/// **Known problems:**
///
/// **Example:**
/// ```rust
/// let vec = vec![vec![1]];
///
/// // Bad
/// vec.iter().map(|x| x.iter()).flatten();
///
/// // Good
/// vec.iter().flat_map(|x| x.iter());
/// ```
pub MAP_FLATTEN,
pedantic,
Expand All @@ -417,7 +431,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let vec = vec![1];
///
/// // Bad
/// vec.iter().filter(|x| **x == 0).map(|x| *x * 2);
///
/// // Good
/// vec.iter().filter_map(|x| Some(*x * 2));
/// ```
pub FILTER_MAP,
pedantic,
Expand Down Expand Up @@ -634,7 +653,12 @@ declare_clippy_lint! {
/// ```rust
/// # use std::rc::Rc;
/// let x = Rc::new(1);
///
/// // Bad
/// x.clone();
///
/// // Good
/// Rc::clone(&x);
/// ```
pub CLONE_ON_REF_PTR,
restriction,
Expand Down Expand Up @@ -741,7 +765,12 @@ declare_clippy_lint! {
/// **Known problems:** Does not catch multi-byte unicode characters.
///
/// **Example:**
/// `_.split("x")` could be `_.split('x')`
/// ```rust,ignore
/// // Bad
/// _.split("x");
///
/// // Good
/// _.split('x');
pub SINGLE_CHAR_PATTERN,
perf,
"using a single-character str where a char could be used, e.g., `_.split(\"x\")`"
Expand Down Expand Up @@ -964,8 +993,8 @@ declare_clippy_lint! {
}

declare_clippy_lint! {
/// **What it does:** Checks for usage of `.chars().last()` or
/// `.chars().next_back()` on a `str` to check if it ends with a given char.
/// **What it does:** Checks for usage of `_.chars().last()` or
/// `_.chars().next_back()` on a `str` to check if it ends with a given char.
///
/// **Why is this bad?** Readability, this can be written more concisely as
/// `_.ends_with(_)`.
Expand All @@ -975,8 +1004,12 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # let name = "_";
/// name.chars().last() == Some('_') || name.chars().next_back() == Some('-')
/// # ;
///
/// // Bad
/// name.chars().last() == Some('_') || name.chars().next_back() == Some('-');
///
/// // Good
/// name.ends_with('_') || name.ends_with('-');
/// ```
pub CHARS_LAST_CMP,
style,
Expand Down Expand Up @@ -1044,17 +1077,15 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let _ = (0..3).filter_map(|x| if x > 2 { Some(x) } else { None });
/// ```
/// As there is no transformation of the argument this could be written as:
/// ```rust
///
/// // As there is no transformation of the argument this could be written as:
/// let _ = (0..3).filter(|&x| x > 2);
/// ```
///
/// ```rust
/// let _ = (0..4).filter_map(|x| Some(x + 1));
/// ```
/// As there is no conditional check on the argument this could be written as:
/// ```rust
///
/// // As there is no conditional check on the argument this could be written as:
/// let _ = (0..4).map(|x| x + 1);
/// ```
pub UNNECESSARY_FILTER_MAP,
Expand All @@ -1075,7 +1106,11 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
/// // Bad
/// let _ = (&vec![3, 4, 5]).into_iter();
///
/// // Good
/// let _ = (&vec![3, 4, 5]).iter();
/// ```
pub INTO_ITER_ON_REF,
style,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ declare_clippy_lint! {
/// ```rust
/// let x = 1.2331f64;
/// let y = 1.2332f64;
///
///
/// // Bad
/// if y == 1.23f64 { }
/// if y != x {} // where both are floats
Expand Down
10 changes: 8 additions & 2 deletions clippy_lints/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ```rust,ignore
/// foo(&vec![1, 2])
/// ```rust
/// # fn foo(my_vec: &[u8]) {}
///
/// // Bad
/// foo(&vec![1, 2]);
///
/// // Good
/// foo(&[1, 2]);
/// ```
pub USELESS_VEC,
perf,
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/verbose_file_reads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare_clippy_lint! {
///
/// **Why is this bad?** `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
/// See also: [fs::read docs](https://doc.rust-lang.org/std/fs/fn.read.html), [fs::read_to_string docs](https://doc.rust-lang.org/std/fs/fn.read_to_string.html)
///
/// **Known problems:** None.
///
/// **Example:**
Expand Down
12 changes: 9 additions & 3 deletions clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ declare_clippy_lint! {
/// still around.
///
/// **Example:**
/// ```rust
/// ```rust,ignore
/// // Bad
/// use std::cmp::Ordering::*;
/// foo(Less);
///
/// // Good
/// use std::cmp::Ordering;
/// foo(Ordering::Less)
/// ```
pub ENUM_GLOB_USE,
pedantic,
Expand Down Expand Up @@ -60,15 +66,15 @@ declare_clippy_lint! {
///
/// **Example:**
///
/// Bad:
/// ```rust,ignore
/// // Bad
/// use crate1::*;
///
/// foo();
/// ```
///
/// Good:
/// ```rust,ignore
/// // Good
/// use crate1::foo;
///
/// foo();
Expand Down
22 changes: 20 additions & 2 deletions clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// println!("");
///
/// // Good
/// println!();
/// ```
pub PRINTLN_EMPTY_STRING,
style,
Expand All @@ -32,8 +36,7 @@ declare_clippy_lint! {

declare_clippy_lint! {
/// **What it does:** This lint warns when you use `print!()` with a format
/// string that
/// ends in a newline.
/// string that ends in a newline.
///
/// **Why is this bad?** You should use `println!()` instead, which appends the
/// newline.
Expand Down Expand Up @@ -125,7 +128,12 @@ declare_clippy_lint! {
/// ```rust
/// # use std::fmt::Write;
/// # let mut buf = String::new();
///
/// // Bad
/// writeln!(buf, "");
///
/// // Good
/// writeln!(buf);
/// ```
pub WRITELN_EMPTY_STRING,
style,
Expand All @@ -147,7 +155,12 @@ declare_clippy_lint! {
/// # use std::fmt::Write;
/// # let mut buf = String::new();
/// # let name = "World";
///
/// // Bad
/// write!(buf, "Hello {}!\n", name);
///
/// // Good
/// writeln!(buf, "Hello {}!", name);
/// ```
pub WRITE_WITH_NEWLINE,
style,
Expand All @@ -168,7 +181,12 @@ declare_clippy_lint! {
/// ```rust
/// # use std::fmt::Write;
/// # let mut buf = String::new();
///
/// // Bad
/// writeln!(buf, "{}", "foo");
///
/// // Good
/// writeln!(buf, "foo");
/// ```
pub WRITE_LITERAL,
style,
Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/zero_div_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// 0.0f32 / 0.0;
/// // Bad
/// let nan = 0.0f32 / 0.0;
///
/// // Good
/// let nan = f32::NAN;
/// ```
pub ZERO_DIVIDED_BY_ZERO,
complexity,
Expand Down
2 changes: 1 addition & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
Lint {
name: "pub_enum_variant_names",
group: "pedantic",
desc: "enums where all variants share a prefix/postfix",
desc: "public enums where all variants share a prefix/postfix",
deprecation: None,
module: "enum_variants",
},
Expand Down

0 comments on commit 0f0f89d

Please sign in to comment.