Skip to content

Commit 5cb9ef3

Browse files
committed
Auto merge of rust-lang#5664 - ThibsG:GiveCorrectedCode, r=flip1995
Give corrected code This PR adds corrected code for doc examples. I did this in several commits to facilitate review. Don't hesitate to tell me if I forgot some. Also, sometimes I felt it was not necessary to give corrected code, but I maybe wrong. fixes: rust-lang#4829 changelog: Improve documentation examples across multiple lints.
2 parents f760d77 + 137a3b4 commit 5cb9ef3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+433
-90
lines changed

clippy_lints/src/assign_ops.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ declare_clippy_lint! {
2424
/// let mut a = 5;
2525
/// let b = 0;
2626
/// // ...
27+
/// // Bad
2728
/// a = a + b;
29+
///
30+
/// // Good
31+
/// a += b;
2832
/// ```
2933
pub ASSIGN_OP_PATTERN,
3034
style,

clippy_lints/src/double_parens.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,24 @@ declare_clippy_lint! {
1313
///
1414
/// **Example:**
1515
/// ```rust
16+
/// // Bad
17+
/// fn simple_double_parens() -> i32 {
18+
/// ((0))
19+
/// }
20+
///
21+
/// // Good
22+
/// fn simple_no_parens() -> i32 {
23+
/// 0
24+
/// }
25+
///
26+
/// // or
27+
///
1628
/// # fn foo(bar: usize) {}
17-
/// ((0));
29+
/// // Bad
1830
/// foo((0));
19-
/// ((1, 2));
31+
///
32+
/// // Good
33+
/// foo(0);
2034
/// ```
2135
pub DOUBLE_PARENS,
2236
complexity,

clippy_lints/src/drop_bounds.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ declare_clippy_lint! {
2727
/// ```rust
2828
/// fn foo<T: Drop>() {}
2929
/// ```
30+
/// Could be written as:
31+
/// ```rust
32+
/// fn foo<T>() {}
33+
/// ```
3034
pub DROP_BOUNDS,
3135
correctness,
3236
"Bounds of the form `T: Drop` are useless"

clippy_lints/src/duration_subsec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,14 @@ declare_clippy_lint! {
2222
/// ```rust
2323
/// # use std::time::Duration;
2424
/// let dur = Duration::new(5, 0);
25+
///
26+
/// // Bad
2527
/// let _micros = dur.subsec_nanos() / 1_000;
2628
/// let _millis = dur.subsec_nanos() / 1_000_000;
29+
///
30+
/// // Good
31+
/// let _micros = dur.subsec_micros();
32+
/// let _millis = dur.subsec_millis();
2733
/// ```
2834
pub DURATION_SUBSEC,
2935
complexity,

clippy_lints/src/enum_variants.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,47 @@ declare_clippy_lint! {
2525
/// BattenbergCake,
2626
/// }
2727
/// ```
28+
/// Could be written as:
29+
/// ```rust
30+
/// enum Cake {
31+
/// BlackForest,
32+
/// Hummingbird,
33+
/// Battenberg,
34+
/// }
35+
/// ```
2836
pub ENUM_VARIANT_NAMES,
2937
style,
3038
"enums where all variants share a prefix/postfix"
3139
}
3240

3341
declare_clippy_lint! {
34-
/// **What it does:** Detects enumeration variants that are prefixed or suffixed
35-
/// by the same characters.
42+
/// **What it does:** Detects public enumeration variants that are
43+
/// prefixed or suffixed by the same characters.
3644
///
37-
/// **Why is this bad?** Enumeration variant names should specify their variant,
45+
/// **Why is this bad?** Public enumeration variant names should specify their variant,
3846
/// not repeat the enumeration name.
3947
///
4048
/// **Known problems:** None.
4149
///
4250
/// **Example:**
4351
/// ```rust
44-
/// enum Cake {
52+
/// pub enum Cake {
4553
/// BlackForestCake,
4654
/// HummingbirdCake,
4755
/// BattenbergCake,
4856
/// }
4957
/// ```
58+
/// Could be written as:
59+
/// ```rust
60+
/// pub enum Cake {
61+
/// BlackForest,
62+
/// Hummingbird,
63+
/// Battenberg,
64+
/// }
65+
/// ```
5066
pub PUB_ENUM_VARIANT_NAMES,
5167
pedantic,
52-
"enums where all variants share a prefix/postfix"
68+
"public enums where all variants share a prefix/postfix"
5369
}
5470

5571
declare_clippy_lint! {
@@ -66,6 +82,12 @@ declare_clippy_lint! {
6682
/// struct BlackForestCake;
6783
/// }
6884
/// ```
85+
/// Could be written as:
86+
/// ```rust
87+
/// mod cake {
88+
/// struct BlackForest;
89+
/// }
90+
/// ```
6991
pub MODULE_NAME_REPETITIONS,
7092
pedantic,
7193
"type names prefixed/postfixed with their containing module's name"

clippy_lints/src/eq_op.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ declare_clippy_lint! {
3939
///
4040
/// **Example:**
4141
/// ```ignore
42+
/// // Bad
4243
/// &x == y
44+
///
45+
/// // Good
46+
/// x == *y
4347
/// ```
4448
pub OP_REF,
4549
style,

clippy_lints/src/escape.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ declare_clippy_lint! {
2828
/// **Example:**
2929
/// ```rust
3030
/// # fn foo(bar: usize) {}
31+
///
32+
/// // Bad
3133
/// let x = Box::new(1);
3234
/// foo(*x);
3335
/// println!("{}", *x);
36+
///
37+
/// // Good
38+
/// let x = 1;
39+
/// foo(x);
40+
/// println!("{}", x);
3441
/// ```
3542
pub BOXED_LOCAL,
3643
perf,

clippy_lints/src/eta_reduction.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ declare_clippy_lint! {
2626
///
2727
/// **Example:**
2828
/// ```rust,ignore
29+
/// // Bad
2930
/// xs.map(|x| foo(x))
31+
///
32+
/// // Good
33+
/// xs.map(foo)
3034
/// ```
3135
/// where `foo(_)` is a plain function that takes the exact argument type of
3236
/// `x`.

clippy_lints/src/eval_order_dependence.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@ declare_clippy_lint! {
2121
/// **Example:**
2222
/// ```rust
2323
/// let mut x = 0;
24+
///
25+
/// // Bad
2426
/// let a = {
2527
/// x = 1;
2628
/// 1
2729
/// } + x;
2830
/// // Unclear whether a is 1 or 2.
31+
///
32+
/// // Good
33+
/// let tmp = {
34+
/// x = 1;
35+
/// 1
36+
/// };
37+
/// let a = tmp + x;
2938
/// ```
3039
pub EVAL_ORDER_DEPENDENCE,
3140
complexity,

clippy_lints/src/fallible_impl_from.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,31 @@ declare_clippy_lint! {
2020
/// **Example:**
2121
/// ```rust
2222
/// struct Foo(i32);
23+
///
24+
/// // Bad
2325
/// impl From<String> for Foo {
2426
/// fn from(s: String) -> Self {
2527
/// Foo(s.parse().unwrap())
2628
/// }
2729
/// }
2830
/// ```
31+
///
32+
/// ```rust
33+
/// // Good
34+
/// struct Foo(i32);
35+
///
36+
/// use std::convert::TryFrom;
37+
/// impl TryFrom<String> for Foo {
38+
/// type Error = ();
39+
/// fn try_from(s: String) -> Result<Self, Self::Error> {
40+
/// if let Ok(parsed) = s.parse() {
41+
/// Ok(Foo(parsed))
42+
/// } else {
43+
/// Err(())
44+
/// }
45+
/// }
46+
/// }
47+
/// ```
2948
pub FALLIBLE_IMPL_FROM,
3049
nursery,
3150
"Warn on impls of `From<..>` that contain `panic!()` or `unwrap()`"
@@ -120,7 +139,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
120139
move |diag| {
121140
diag.help(
122141
"`From` is intended for infallible conversions only. \
123-
Use `TryFrom` if there's a possibility for the conversion to fail.");
142+
Use `TryFrom` if there's a possibility for the conversion to fail.");
124143
diag.span_note(fpu.result, "potential failure(s)");
125144
});
126145
}

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ declare_clippy_lint! {
2828
/// **Example:**
2929
///
3030
/// ```rust
31-
///
3231
/// let a = 3f32;
3332
/// let _ = a.powf(1.0 / 3.0);
3433
/// let _ = (1.0 + a).ln();
@@ -38,7 +37,6 @@ declare_clippy_lint! {
3837
/// is better expressed as
3938
///
4039
/// ```rust
41-
///
4240
/// let a = 3f32;
4341
/// let _ = a.cbrt();
4442
/// let _ = a.ln_1p();

clippy_lints/src/format.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ declare_clippy_lint! {
2525
///
2626
/// **Examples:**
2727
/// ```rust
28+
///
29+
/// // Bad
2830
/// # let foo = "foo";
29-
/// format!("foo");
3031
/// format!("{}", foo);
32+
///
33+
/// // Good
34+
/// format!("foo");
3135
/// ```
3236
pub USELESS_FORMAT,
3337
complexity,

clippy_lints/src/functions.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ declare_clippy_lint! {
4949
/// **Known problems:** None.
5050
///
5151
/// **Example:**
52-
/// ``` rust
52+
/// ```rust
5353
/// fn im_too_long() {
54-
/// println!("");
55-
/// // ... 100 more LoC
56-
/// println!("");
54+
/// println!("");
55+
/// // ... 100 more LoC
56+
/// println!("");
5757
/// }
5858
/// ```
5959
pub TOO_MANY_LINES,
@@ -79,10 +79,16 @@ declare_clippy_lint! {
7979
/// `some_argument.get_raw_ptr()`).
8080
///
8181
/// **Example:**
82-
/// ```rust
82+
/// ```rust,ignore
83+
/// // Bad
8384
/// pub fn foo(x: *const u8) {
8485
/// println!("{}", unsafe { *x });
8586
/// }
87+
///
88+
/// // Good
89+
/// pub unsafe fn foo(x: *const u8) {
90+
/// println!("{}", unsafe { *x });
91+
/// }
8692
/// ```
8793
pub NOT_UNSAFE_PTR_ARG_DEREF,
8894
correctness,

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ declare_clippy_lint! {
2525
/// if i != 0 {
2626
/// i -= 1;
2727
/// }
28-
/// ```
29-
/// Use instead:
30-
/// ```rust
31-
/// let end: u32 = 10;
32-
/// let start: u32 = 5;
33-
///
34-
/// let mut i: u32 = end - start;
3528
///
3629
/// // Good
3730
/// i = i.saturating_sub(1);

clippy_lints/src/int_plus_one.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::utils::{snippet_opt, span_lint_and_sugg};
1010
declare_clippy_lint! {
1111
/// **What it does:** Checks for usage of `x >= y + 1` or `x - 1 >= y` (and `<=`) in a block
1212
///
13-
///
1413
/// **Why is this bad?** Readability -- better to use `> y` instead of `>= y + 1`.
1514
///
1615
/// **Known problems:** None.

clippy_lints/src/integer_division.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ declare_clippy_lint! {
1515
///
1616
/// **Example:**
1717
/// ```rust
18-
/// fn main() {
19-
/// let x = 3 / 2;
20-
/// println!("{}", x);
21-
/// }
18+
/// // Bad
19+
/// let x = 3 / 2;
20+
/// println!("{}", x);
21+
///
22+
/// // Good
23+
/// let x = 3f32 / 2f32;
24+
/// println!("{}", x);
2225
/// ```
2326
pub INTEGER_DIVISION,
2427
restriction,

clippy_lints/src/items_after_statements.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ declare_clippy_lint! {
1616
///
1717
/// **Example:**
1818
/// ```rust
19+
/// // Bad
1920
/// fn foo() {
2021
/// println!("cake");
2122
/// }
@@ -28,6 +29,21 @@ declare_clippy_lint! {
2829
/// foo(); // prints "foo"
2930
/// }
3031
/// ```
32+
///
33+
/// ```rust
34+
/// // Good
35+
/// fn foo() {
36+
/// println!("cake");
37+
/// }
38+
///
39+
/// fn main() {
40+
/// fn foo() {
41+
/// println!("foo");
42+
/// }
43+
/// foo(); // prints "foo"
44+
/// foo(); // prints "foo"
45+
/// }
46+
/// ```
3147
pub ITEMS_AFTER_STATEMENTS,
3248
pedantic,
3349
"blocks where an item comes after a statement"

0 commit comments

Comments
 (0)