Skip to content
/ rustc Public
forked from rust-lang/rust

Commit

Permalink
Auto merge of rust-lang#5664 - ThibsG:GiveCorrectedCode, r=flip1995
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jun 2, 2020
2 parents f760d77 + 137a3b4 commit 5cb9ef3
Show file tree
Hide file tree
Showing 42 changed files with 433 additions and 90 deletions.
4 changes: 4 additions & 0 deletions clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ declare_clippy_lint! {
/// let mut a = 5;
/// let b = 0;
/// // ...
/// // Bad
/// a = a + b;
///
/// // Good
/// a += b;
/// ```
pub ASSIGN_OP_PATTERN,
style,
Expand Down
18 changes: 16 additions & 2 deletions clippy_lints/src/double_parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// fn simple_double_parens() -> i32 {
/// ((0))
/// }
///
/// // Good
/// fn simple_no_parens() -> i32 {
/// 0
/// }
///
/// // or
///
/// # fn foo(bar: usize) {}
/// ((0));
/// // Bad
/// foo((0));
/// ((1, 2));
///
/// // Good
/// foo(0);
/// ```
pub DOUBLE_PARENS,
complexity,
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/drop_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ declare_clippy_lint! {
/// ```rust
/// fn foo<T: Drop>() {}
/// ```
/// Could be written as:
/// ```rust
/// fn foo<T>() {}
/// ```
pub DROP_BOUNDS,
correctness,
"Bounds of the form `T: Drop` are useless"
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/duration_subsec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ declare_clippy_lint! {
/// ```rust
/// # use std::time::Duration;
/// let dur = Duration::new(5, 0);
///
/// // Bad
/// let _micros = dur.subsec_nanos() / 1_000;
/// let _millis = dur.subsec_nanos() / 1_000_000;
///
/// // Good
/// let _micros = dur.subsec_micros();
/// let _millis = dur.subsec_millis();
/// ```
pub DURATION_SUBSEC,
complexity,
Expand Down
32 changes: 27 additions & 5 deletions clippy_lints/src/enum_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,47 @@ declare_clippy_lint! {
/// BattenbergCake,
/// }
/// ```
/// Could be written as:
/// ```rust
/// enum Cake {
/// BlackForest,
/// Hummingbird,
/// Battenberg,
/// }
/// ```
pub ENUM_VARIANT_NAMES,
style,
"enums where all variants share a prefix/postfix"
}

declare_clippy_lint! {
/// **What it does:** Detects enumeration variants that are prefixed or suffixed
/// by the same characters.
/// **What it does:** Detects public enumeration variants that are
/// prefixed or suffixed by the same characters.
///
/// **Why is this bad?** Enumeration variant names should specify their variant,
/// **Why is this bad?** Public enumeration variant names should specify their variant,
/// not repeat the enumeration name.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// enum Cake {
/// pub enum Cake {
/// BlackForestCake,
/// HummingbirdCake,
/// BattenbergCake,
/// }
/// ```
/// Could be written as:
/// ```rust
/// pub enum Cake {
/// BlackForest,
/// Hummingbird,
/// Battenberg,
/// }
/// ```
pub PUB_ENUM_VARIANT_NAMES,
pedantic,
"enums where all variants share a prefix/postfix"
"public enums where all variants share a prefix/postfix"
}

declare_clippy_lint! {
Expand All @@ -66,6 +82,12 @@ declare_clippy_lint! {
/// struct BlackForestCake;
/// }
/// ```
/// Could be written as:
/// ```rust
/// mod cake {
/// struct BlackForest;
/// }
/// ```
pub MODULE_NAME_REPETITIONS,
pedantic,
"type names prefixed/postfixed with their containing module's name"
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/eq_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```ignore
/// // Bad
/// &x == y
///
/// // Good
/// x == *y
/// ```
pub OP_REF,
style,
Expand Down
7 changes: 7 additions & 0 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// # fn foo(bar: usize) {}
///
/// // Bad
/// let x = Box::new(1);
/// foo(*x);
/// println!("{}", *x);
///
/// // Good
/// let x = 1;
/// foo(x);
/// println!("{}", x);
/// ```
pub BOXED_LOCAL,
perf,
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/eta_reduction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust,ignore
/// // Bad
/// xs.map(|x| foo(x))
///
/// // Good
/// xs.map(foo)
/// ```
/// where `foo(_)` is a plain function that takes the exact argument type of
/// `x`.
Expand Down
9 changes: 9 additions & 0 deletions clippy_lints/src/eval_order_dependence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// let mut x = 0;
///
/// // Bad
/// let a = {
/// x = 1;
/// 1
/// } + x;
/// // Unclear whether a is 1 or 2.
///
/// // Good
/// let tmp = {
/// x = 1;
/// 1
/// };
/// let a = tmp + x;
/// ```
pub EVAL_ORDER_DEPENDENCE,
complexity,
Expand Down
21 changes: 20 additions & 1 deletion clippy_lints/src/fallible_impl_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,31 @@ declare_clippy_lint! {
/// **Example:**
/// ```rust
/// struct Foo(i32);
///
/// // Bad
/// impl From<String> for Foo {
/// fn from(s: String) -> Self {
/// Foo(s.parse().unwrap())
/// }
/// }
/// ```
///
/// ```rust
/// // Good
/// struct Foo(i32);
///
/// use std::convert::TryFrom;
/// impl TryFrom<String> for Foo {
/// type Error = ();
/// fn try_from(s: String) -> Result<Self, Self::Error> {
/// if let Ok(parsed) = s.parse() {
/// Ok(Foo(parsed))
/// } else {
/// Err(())
/// }
/// }
/// }
/// ```
pub FALLIBLE_IMPL_FROM,
nursery,
"Warn on impls of `From<..>` that contain `panic!()` or `unwrap()`"
Expand Down Expand Up @@ -120,7 +139,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
move |diag| {
diag.help(
"`From` is intended for infallible conversions only. \
Use `TryFrom` if there's a possibility for the conversion to fail.");
Use `TryFrom` if there's a possibility for the conversion to fail.");
diag.span_note(fpu.result, "potential failure(s)");
});
}
Expand Down
2 changes: 0 additions & 2 deletions clippy_lints/src/floating_point_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ declare_clippy_lint! {
/// **Example:**
///
/// ```rust
///
/// let a = 3f32;
/// let _ = a.powf(1.0 / 3.0);
/// let _ = (1.0 + a).ln();
Expand All @@ -38,7 +37,6 @@ declare_clippy_lint! {
/// is better expressed as
///
/// ```rust
///
/// let a = 3f32;
/// let _ = a.cbrt();
/// let _ = a.ln_1p();
Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ declare_clippy_lint! {
///
/// **Examples:**
/// ```rust
///
/// // Bad
/// # let foo = "foo";
/// format!("foo");
/// format!("{}", foo);
///
/// // Good
/// format!("foo");
/// ```
pub USELESS_FORMAT,
complexity,
Expand Down
16 changes: 11 additions & 5 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
/// ``` rust
/// ```rust
/// fn im_too_long() {
/// println!("");
/// // ... 100 more LoC
/// println!("");
/// println!("");
/// // ... 100 more LoC
/// println!("");
/// }
/// ```
pub TOO_MANY_LINES,
Expand All @@ -79,10 +79,16 @@ declare_clippy_lint! {
/// `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,
Expand Down
7 changes: 0 additions & 7 deletions clippy_lints/src/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ declare_clippy_lint! {
/// 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);
Expand Down
1 change: 0 additions & 1 deletion clippy_lints/src/int_plus_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::utils::{snippet_opt, span_lint_and_sugg};
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.
Expand Down
11 changes: 7 additions & 4 deletions clippy_lints/src/integer_division.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// fn main() {
/// let x = 3 / 2;
/// println!("{}", x);
/// }
/// // Bad
/// let x = 3 / 2;
/// println!("{}", x);
///
/// // Good
/// let x = 3f32 / 2f32;
/// println!("{}", x);
/// ```
pub INTEGER_DIVISION,
restriction,
Expand Down
16 changes: 16 additions & 0 deletions clippy_lints/src/items_after_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// // Bad
/// fn foo() {
/// println!("cake");
/// }
Expand All @@ -28,6 +29,21 @@ declare_clippy_lint! {
/// 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"
Expand Down
Loading

0 comments on commit 5cb9ef3

Please sign in to comment.