Skip to content

Commit cfa6d3a

Browse files
committed
Add more corrected code for doc
1 parent 86814c5 commit cfa6d3a

File tree

7 files changed

+94
-24
lines changed

7 files changed

+94
-24
lines changed

clippy_lints/src/methods/mod.rs

+50-15
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,12 @@ declare_clippy_lint! {
218218
/// **Example:**
219219
/// ```rust
220220
/// # let x = Ok::<_, ()>(());
221-
/// x.ok().expect("why did I do this again?")
221+
///
222+
/// // Bad
223+
/// x.ok().expect("why did I do this again?");
224+
///
225+
/// // Good
226+
/// x.expect("why did I do this again?");
222227
/// ```
223228
pub OK_EXPECT,
224229
style,
@@ -273,8 +278,12 @@ declare_clippy_lint! {
273278
/// **Example:**
274279
/// ```rust
275280
/// # let opt = Some(1);
276-
/// opt.map_or(None, |a| Some(a + 1))
277-
/// # ;
281+
///
282+
/// // Bad
283+
/// opt.map_or(None, |a| Some(a + 1));
284+
///
285+
/// // Good
286+
/// opt.and_then(|a| Some(a + 1));
278287
/// ```
279288
pub OPTION_MAP_OR_NONE,
280289
style,
@@ -390,14 +399,19 @@ declare_clippy_lint! {
390399
/// **What it does:** Checks for usage of `_.map(_).flatten(_)`,
391400
///
392401
/// **Why is this bad?** Readability, this can be written more concisely as a
393-
/// single method call.
402+
/// single method call using `_.flat_map(_)`
394403
///
395404
/// **Known problems:**
396405
///
397406
/// **Example:**
398407
/// ```rust
399408
/// let vec = vec![vec![1]];
409+
///
410+
/// // Bad
400411
/// vec.iter().map(|x| x.iter()).flatten();
412+
///
413+
/// // Good
414+
/// vec.iter().flat_map(|x| x.iter());
401415
/// ```
402416
pub MAP_FLATTEN,
403417
pedantic,
@@ -417,7 +431,12 @@ declare_clippy_lint! {
417431
/// **Example:**
418432
/// ```rust
419433
/// let vec = vec![1];
434+
///
435+
/// // Bad
420436
/// vec.iter().filter(|x| **x == 0).map(|x| *x * 2);
437+
///
438+
/// // Good
439+
/// vec.iter().filter_map(|x| Some(*x * 2));
421440
/// ```
422441
pub FILTER_MAP,
423442
pedantic,
@@ -634,7 +653,12 @@ declare_clippy_lint! {
634653
/// ```rust
635654
/// # use std::rc::Rc;
636655
/// let x = Rc::new(1);
656+
///
657+
/// // Bad
637658
/// x.clone();
659+
///
660+
/// // Good
661+
/// Rc::clone(&x);
638662
/// ```
639663
pub CLONE_ON_REF_PTR,
640664
restriction,
@@ -741,7 +765,12 @@ declare_clippy_lint! {
741765
/// **Known problems:** Does not catch multi-byte unicode characters.
742766
///
743767
/// **Example:**
744-
/// `_.split("x")` could be `_.split('x')`
768+
/// ```rust,ignore
769+
/// // Bad
770+
/// _.split("x");
771+
///
772+
/// // Good
773+
/// _.split('x');
745774
pub SINGLE_CHAR_PATTERN,
746775
perf,
747776
"using a single-character str where a char could be used, e.g., `_.split(\"x\")`"
@@ -964,8 +993,8 @@ declare_clippy_lint! {
964993
}
965994

966995
declare_clippy_lint! {
967-
/// **What it does:** Checks for usage of `.chars().last()` or
968-
/// `.chars().next_back()` on a `str` to check if it ends with a given char.
996+
/// **What it does:** Checks for usage of `_.chars().last()` or
997+
/// `_.chars().next_back()` on a `str` to check if it ends with a given char.
969998
///
970999
/// **Why is this bad?** Readability, this can be written more concisely as
9711000
/// `_.ends_with(_)`.
@@ -975,8 +1004,12 @@ declare_clippy_lint! {
9751004
/// **Example:**
9761005
/// ```rust
9771006
/// # let name = "_";
978-
/// name.chars().last() == Some('_') || name.chars().next_back() == Some('-')
979-
/// # ;
1007+
///
1008+
/// // Bad
1009+
/// name.chars().last() == Some('_') || name.chars().next_back() == Some('-');
1010+
///
1011+
/// // Good
1012+
/// name.ends_with('_') || name.ends_with('-');
9801013
/// ```
9811014
pub CHARS_LAST_CMP,
9821015
style,
@@ -1044,17 +1077,15 @@ declare_clippy_lint! {
10441077
/// **Example:**
10451078
/// ```rust
10461079
/// let _ = (0..3).filter_map(|x| if x > 2 { Some(x) } else { None });
1047-
/// ```
1048-
/// As there is no transformation of the argument this could be written as:
1049-
/// ```rust
1080+
///
1081+
/// // As there is no transformation of the argument this could be written as:
10501082
/// let _ = (0..3).filter(|&x| x > 2);
10511083
/// ```
10521084
///
10531085
/// ```rust
10541086
/// let _ = (0..4).filter_map(|x| Some(x + 1));
1055-
/// ```
1056-
/// As there is no conditional check on the argument this could be written as:
1057-
/// ```rust
1087+
///
1088+
/// // As there is no conditional check on the argument this could be written as:
10581089
/// let _ = (0..4).map(|x| x + 1);
10591090
/// ```
10601091
pub UNNECESSARY_FILTER_MAP,
@@ -1075,7 +1106,11 @@ declare_clippy_lint! {
10751106
/// **Example:**
10761107
///
10771108
/// ```rust
1109+
/// // Bad
10781110
/// let _ = (&vec![3, 4, 5]).into_iter();
1111+
///
1112+
/// // Good
1113+
/// let _ = (&vec![3, 4, 5]).iter();
10791114
/// ```
10801115
pub INTO_ITER_ON_REF,
10811116
style,

clippy_lints/src/misc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ declare_clippy_lint! {
9393
/// ```rust
9494
/// let x = 1.2331f64;
9595
/// let y = 1.2332f64;
96-
///
96+
///
9797
/// // Bad
9898
/// if y == 1.23f64 { }
9999
/// if y != x {} // where both are floats

clippy_lints/src/vec.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ declare_clippy_lint! {
1717
/// **Known problems:** None.
1818
///
1919
/// **Example:**
20-
/// ```rust,ignore
21-
/// foo(&vec![1, 2])
20+
/// ```rust
21+
/// # fn foo(my_vec: &[u8]) {}
22+
///
23+
/// // Bad
24+
/// foo(&vec![1, 2]);
25+
///
26+
/// // Good
27+
/// foo(&[1, 2]);
2228
/// ```
2329
pub USELESS_VEC,
2430
perf,

clippy_lints/src/verbose_file_reads.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare_clippy_lint! {
99
///
1010
/// **Why is this bad?** `fs::{read, read_to_string}` provide the same functionality when `buf` is empty with fewer imports and no intermediate values.
1111
/// 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)
12+
///
1213
/// **Known problems:** None.
1314
///
1415
/// **Example:**

clippy_lints/src/wildcard_imports.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ declare_clippy_lint! {
1919
/// still around.
2020
///
2121
/// **Example:**
22-
/// ```rust
22+
/// ```rust,ignore
23+
/// // Bad
2324
/// use std::cmp::Ordering::*;
25+
/// foo(Less);
26+
///
27+
/// // Good
28+
/// use std::cmp::Ordering;
29+
/// foo(Ordering::Less)
2430
/// ```
2531
pub ENUM_GLOB_USE,
2632
pedantic,
@@ -60,15 +66,15 @@ declare_clippy_lint! {
6066
///
6167
/// **Example:**
6268
///
63-
/// Bad:
6469
/// ```rust,ignore
70+
/// // Bad
6571
/// use crate1::*;
6672
///
6773
/// foo();
6874
/// ```
6975
///
70-
/// Good:
7176
/// ```rust,ignore
77+
/// // Good
7278
/// use crate1::foo;
7379
///
7480
/// foo();

clippy_lints/src/write.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ declare_clippy_lint! {
2323
///
2424
/// **Example:**
2525
/// ```rust
26+
/// // Bad
2627
/// println!("");
28+
///
29+
/// // Good
30+
/// println!();
2731
/// ```
2832
pub PRINTLN_EMPTY_STRING,
2933
style,
@@ -32,8 +36,7 @@ declare_clippy_lint! {
3236

3337
declare_clippy_lint! {
3438
/// **What it does:** This lint warns when you use `print!()` with a format
35-
/// string that
36-
/// ends in a newline.
39+
/// string that ends in a newline.
3740
///
3841
/// **Why is this bad?** You should use `println!()` instead, which appends the
3942
/// newline.
@@ -125,7 +128,12 @@ declare_clippy_lint! {
125128
/// ```rust
126129
/// # use std::fmt::Write;
127130
/// # let mut buf = String::new();
131+
///
132+
/// // Bad
128133
/// writeln!(buf, "");
134+
///
135+
/// // Good
136+
/// writeln!(buf);
129137
/// ```
130138
pub WRITELN_EMPTY_STRING,
131139
style,
@@ -147,7 +155,12 @@ declare_clippy_lint! {
147155
/// # use std::fmt::Write;
148156
/// # let mut buf = String::new();
149157
/// # let name = "World";
158+
///
159+
/// // Bad
150160
/// write!(buf, "Hello {}!\n", name);
161+
///
162+
/// // Good
163+
/// writeln!(buf, "Hello {}!", name);
151164
/// ```
152165
pub WRITE_WITH_NEWLINE,
153166
style,
@@ -168,7 +181,12 @@ declare_clippy_lint! {
168181
/// ```rust
169182
/// # use std::fmt::Write;
170183
/// # let mut buf = String::new();
184+
///
185+
/// // Bad
171186
/// writeln!(buf, "{}", "foo");
187+
///
188+
/// // Good
189+
/// writeln!(buf, "foo");
172190
/// ```
173191
pub WRITE_LITERAL,
174192
style,

clippy_lints/src/zero_div_zero.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ declare_clippy_lint! {
1414
///
1515
/// **Example:**
1616
/// ```rust
17-
/// 0.0f32 / 0.0;
17+
/// // Bad
18+
/// let nan = 0.0f32 / 0.0;
19+
///
20+
/// // Good
21+
/// let nan = f32::NAN;
1822
/// ```
1923
pub ZERO_DIVIDED_BY_ZERO,
2024
complexity,

0 commit comments

Comments
 (0)