From 88d693918ff1874b6cfb7fbc9e2cd9bff389b96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Sun, 12 Aug 2018 00:16:03 +0200 Subject: [PATCH] docs: add more suggestions on how to fix clippy findings to the online lint list. --- clippy_lints/src/len_zero.rs | 6 ++++++ clippy_lints/src/loops.rs | 8 ++++++++ clippy_lints/src/matches.rs | 9 +++++++++ clippy_lints/src/methods.rs | 4 ++-- clippy_lints/src/redundant_field_names.rs | 4 ++++ clippy_lints/src/returns.rs | 15 ++++++++++++++- clippy_lints/src/shadow.rs | 4 ++++ clippy_lints/src/write.rs | 8 ++++++++ 8 files changed, 55 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/len_zero.rs b/clippy_lints/src/len_zero.rs index b73f912fad5..081450516bb 100644 --- a/clippy_lints/src/len_zero.rs +++ b/clippy_lints/src/len_zero.rs @@ -22,6 +22,12 @@ use crate::utils::{get_item_name, in_macro, snippet, span_lint, span_lint_and_su /// **Example:** /// ```rust /// if x.len() == 0 { .. } +/// if y.len() != 0 { .. } +/// ``` +/// instead use +/// ```rust +/// if x.len().is_empty() { .. } +/// if !y.len().is_empty() { .. } /// ``` declare_clippy_lint! { pub LEN_ZERO, diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 61c35dc33b0..6d3134d2206 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -80,6 +80,10 @@ declare_clippy_lint! { /// // with `y` a `Vec` or slice: /// for x in y.iter() { .. } /// ``` +/// can be rewritten to +/// ```rust +/// for x in &y { .. } +/// ``` declare_clippy_lint! { pub EXPLICIT_ITER_LOOP, style, @@ -98,6 +102,10 @@ declare_clippy_lint! { /// // with `y` a `Vec` or slice: /// for x in y.into_iter() { .. } /// ``` +/// can be rewritten to +/// ```rust +/// for x in y { .. } +/// ``` declare_clippy_lint! { pub EXPLICIT_INTO_ITER_LOOP, style, diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index c12389a0a1f..d42355f89f4 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -93,6 +93,15 @@ declare_clippy_lint! { /// false => bar(), /// } /// ``` +/// Use if/else instead: +/// ```rust +/// let condition: bool = true; +/// if condition { +/// foo(); +/// } else { +/// bar(); +/// } +/// ``` declare_clippy_lint! { pub MATCH_BOOL, style, diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index 25a8e73dd03..69df7f2a1c5 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -448,7 +448,7 @@ declare_clippy_lint! { /// **Known problems:** Does not catch multi-byte unicode characters. /// /// **Example:** -/// `_.split("x")` could be `_.split('x') +/// `_.split("x")` could be `_.split('x')` declare_clippy_lint! { pub SINGLE_CHAR_PATTERN, perf, @@ -468,7 +468,7 @@ declare_clippy_lint! { /// ```rust,ignore /// let c_str = CString::new("foo").unwrap().as_ptr(); /// unsafe { -/// call_some_ffi_func(c_str); +/// call_some_ffi_func(c_str); /// } /// ``` /// Here `c_str` point to a freed address. The correct use would be: diff --git a/clippy_lints/src/redundant_field_names.rs b/clippy_lints/src/redundant_field_names.rs index c5cf35edabe..ba8b11e55df 100644 --- a/clippy_lints/src/redundant_field_names.rs +++ b/clippy_lints/src/redundant_field_names.rs @@ -21,6 +21,10 @@ use crate::utils::{span_lint_and_sugg}; /// /// let foo = Foo{ bar: bar } /// ``` +/// the last line can be simplified to +/// ```rust +/// let foo = Foo{ bar } +/// ``` declare_clippy_lint! { pub REDUNDANT_FIELD_NAMES, style, diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 0ede1bc9727..b2202fb1eff 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -19,6 +19,10 @@ use crate::utils::{in_macro, match_path_ast, snippet_opt, span_lint_and_then, sp /// ```rust /// fn foo(x: usize) { return x; } /// ``` +/// simplify to +/// ```rust +/// fn foo(x: usize) { x } +/// ``` declare_clippy_lint! { pub NEEDLESS_RETURN, style, @@ -35,7 +39,16 @@ declare_clippy_lint! { /// /// **Example:** /// ```rust -/// { let x = ..; x } +/// fn foo() -> String { +/// let x = String::new(); +/// x +///} +/// ``` +/// instead, use +/// ``` +/// fn foo() -> String { +/// String::new() +///} /// ``` declare_clippy_lint! { pub LET_AND_RETURN, diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index aab578d6344..a6645e19f02 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -42,6 +42,10 @@ declare_clippy_lint! { /// ```rust /// let x = x + 1; /// ``` +/// use different variable name: +/// ```rust +/// let y = x + 1; +/// ``` declare_clippy_lint! { pub SHADOW_REUSE, restriction, diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index d8bbf0098d2..915ba832833 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -35,6 +35,10 @@ declare_clippy_lint! { /// ```rust /// print!("Hello {}!\n", name); /// ``` +/// use println!() instead +/// ```rust +/// println!("Hello {}!", name); +/// ``` declare_clippy_lint! { pub PRINT_WITH_NEWLINE, style, @@ -88,6 +92,10 @@ declare_clippy_lint! { /// ```rust /// println!("{}", "foo"); /// ``` +/// use the literal without formatting: +/// ```rust +/// println!("foo"); +/// ``` declare_clippy_lint! { pub PRINT_LITERAL, style,