diff --git a/clippy_lints/src/default_trait_access.rs b/clippy_lints/src/default_trait_access.rs index bc5643a0bed..7719e35902b 100644 --- a/clippy_lints/src/default_trait_access.rs +++ b/clippy_lints/src/default_trait_access.rs @@ -49,44 +49,44 @@ impl LintPass for DefaultTraitAccess { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { if_chain! { - if let ExprKind::Call(ref path, ..) = expr.node; - if !any_parent_is_automatically_derived(cx.tcx, expr.id); - if let ExprKind::Path(ref qpath) = path.node; - if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id)); - if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD); - then { - match qpath { - QPath::Resolved(..) => { - if_chain! { - // Detect and ignore ::default() because these calls do - // explicitly name the type. - if let ExprKind::Call(ref method, ref _args) = expr.node; - if let ExprKind::Path(ref p) = method.node; - if let QPath::Resolved(Some(_ty), _path) = p; - then { - return; - } - } - - // TODO: Work out a way to put "whatever the imported way of referencing - // this type in this file" rather than a fully-qualified type. - let expr_ty = cx.tables.expr_ty(expr); - if let TyKind::Adt(..) = expr_ty.sty { - let replacement = format!("{}::default()", expr_ty); - span_lint_and_sugg( - cx, - DEFAULT_TRAIT_ACCESS, - expr.span, - &format!("Calling {} is more clear than this expression", replacement), - "try", - replacement, - Applicability::Unspecified, // First resolve the TODO above - ); + if let ExprKind::Call(ref path, ..) = expr.node; + if !any_parent_is_automatically_derived(cx.tcx, expr.id); + if let ExprKind::Path(ref qpath) = path.node; + if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id)); + if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD); + then { + match qpath { + QPath::Resolved(..) => { + if_chain! { + // Detect and ignore ::default() because these calls do + // explicitly name the type. + if let ExprKind::Call(ref method, ref _args) = expr.node; + if let ExprKind::Path(ref p) = method.node; + if let QPath::Resolved(Some(_ty), _path) = p; + then { + return; + } } - }, - QPath::TypeRelative(..) => {}, - } - } + + // TODO: Work out a way to put "whatever the imported way of referencing + // this type in this file" rather than a fully-qualified type. + let expr_ty = cx.tables.expr_ty(expr); + if let TyKind::Adt(..) = expr_ty.sty { + let replacement = format!("{}::default()", expr_ty); + span_lint_and_sugg( + cx, + DEFAULT_TRAIT_ACCESS, + expr.span, + &format!("Calling {} is more clear than this expression", replacement), + "try", + replacement, + Applicability::Unspecified, // First resolve the TODO above + ); + } + }, + QPath::TypeRelative(..) => {}, + } + } } } } diff --git a/clippy_lints/src/explicit_write.rs b/clippy_lints/src/explicit_write.rs index c1f007fea0b..94bd0ab209c 100644 --- a/clippy_lints/src/explicit_write.rs +++ b/clippy_lints/src/explicit_write.rs @@ -27,10 +27,9 @@ use if_chain::if_chain; /// writeln!(&mut io::stderr(), "foo: {:?}", bar).unwrap(); /// ``` declare_clippy_lint! { -pub EXPLICIT_WRITE, -complexity, -"using the `write!()` family of functions instead of the `print!()` family \ - of functions, when using the latter would work" + pub EXPLICIT_WRITE, + complexity, + "using the `write!()` family of functions instead of the `print!()` family of functions, when using the latter would work" } #[derive(Copy, Clone, Debug)] diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 01b526cc630..08ae3a55a2c 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -249,10 +249,9 @@ declare_clippy_lint! { /// vec.iter().map(|x| /* some operation returning () */).collect::>(); /// ``` declare_clippy_lint! { -pub UNUSED_COLLECT, -perf, -"`collect()`ing an iterator without using the result; this is usually better \ - written as a for loop" + pub UNUSED_COLLECT, + perf, + "`collect()`ing an iterator without using the result; this is usually better written as a for loop" } /// **What it does:** Checks for functions collecting an iterator when collect diff --git a/clippy_lints/src/map_clone.rs b/clippy_lints/src/map_clone.rs index 4ea02db1465..af85a279ca3 100644 --- a/clippy_lints/src/map_clone.rs +++ b/clippy_lints/src/map_clone.rs @@ -81,7 +81,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, name, None) => { match closure_expr.node { hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner) => { - if !cx.tables.expr_ty(inner).is_box() => { + if !cx.tables.expr_ty(inner).is_box() { lint(cx, e.span, args[0].span, name, inner); } }, diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 1821a35cf59..dedf9a16b35 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -40,10 +40,9 @@ use std::collections::Bound; /// } /// ``` declare_clippy_lint! { -pub SINGLE_MATCH, -style, -"a match statement with a single nontrivial arm (i.e. where the other arm \ - is `_ => {}`) instead of `if let`" + pub SINGLE_MATCH, + style, + "a match statement with a single nontrivial arm (i.e. where the other arm is `_ => {}`) instead of `if let`" } /// **What it does:** Checks for matches with a two arms where an `if let` will @@ -61,10 +60,9 @@ style, /// } /// ``` declare_clippy_lint! { -pub SINGLE_MATCH_ELSE, -pedantic, -"a match statement with a two arms where the second arm's pattern is a wildcard \ - instead of `if let`" + pub SINGLE_MATCH_ELSE, + pedantic, + "a match statement with a two arms where the second arm's pattern is a wildcard instead of `if let`" } /// **What it does:** Checks for matches where all arms match a reference, diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 9e56213931a..0df166a0796 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -131,10 +131,9 @@ declare_clippy_lint! { /// } /// ``` declare_clippy_lint! { -pub WRONG_SELF_CONVENTION, -style, -"defining a method named with an established prefix (like \"into_\") that takes \ - `self` with the wrong convention" + pub WRONG_SELF_CONVENTION, + style, + "defining a method named with an established prefix (like \"into_\") that takes `self` with the wrong convention" } /// **What it does:** This is the same as @@ -155,10 +154,9 @@ style, /// } /// ``` declare_clippy_lint! { -pub WRONG_PUB_SELF_CONVENTION, -restriction, -"defining a public method named with an established prefix (like \"into_\") that takes \ - `self` with the wrong convention" + pub WRONG_PUB_SELF_CONVENTION, + restriction, + "defining a public method named with an established prefix (like \"into_\") that takes `self` with the wrong convention" } /// **What it does:** Checks for usage of `ok().expect(..)`. @@ -173,10 +171,9 @@ restriction, /// x.ok().expect("why did I do this again?") /// ``` declare_clippy_lint! { -pub OK_EXPECT, -style, -"using `ok().expect()`, which gives worse error messages than \ - calling `expect` directly on the Result" + pub OK_EXPECT, + style, + "using `ok().expect()`, which gives worse error messages than calling `expect` directly on the Result" } /// **What it does:** Checks for usage of `_.map(_).unwrap_or(_)`. @@ -209,10 +206,9 @@ pedantic, /// x.map(|a| a + 1).unwrap_or_else(some_function) /// ``` declare_clippy_lint! { -pub OPTION_MAP_UNWRAP_OR_ELSE, -pedantic, -"using `Option.map(f).unwrap_or_else(g)`, which is more succinctly expressed as \ - `map_or_else(g, f)`" + pub OPTION_MAP_UNWRAP_OR_ELSE, + pedantic, + "using `Option.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `map_or_else(g, f)`" } /// **What it does:** Checks for usage of `result.map(_).unwrap_or_else(_)`. @@ -227,10 +223,9 @@ pedantic, /// x.map(|a| a + 1).unwrap_or_else(some_function) /// ``` declare_clippy_lint! { -pub RESULT_MAP_UNWRAP_OR_ELSE, -pedantic, -"using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as \ - `.ok().map_or_else(g, f)`" + pub RESULT_MAP_UNWRAP_OR_ELSE, + pedantic, + "using `Result.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `.ok().map_or_else(g, f)`" } /// **What it does:** Checks for usage of `_.map_or(None, _)`. @@ -245,10 +240,9 @@ pedantic, /// opt.map_or(None, |a| a + 1) /// ``` declare_clippy_lint! { -pub OPTION_MAP_OR_NONE, -style, -"using `Option.map_or(None, f)`, which is more succinctly expressed as \ - `and_then(f)`" + pub OPTION_MAP_OR_NONE, + style, + "using `Option.map_or(None, f)`, which is more succinctly expressed as `and_then(f)`" } /// **What it does:** Checks for usage of `_.filter(_).next()`. @@ -280,10 +274,9 @@ declare_clippy_lint! { /// iter.map(|x| x.iter()).flatten() /// ``` declare_clippy_lint! { -pub MAP_FLATTEN, -pedantic, -"using combinations of `flatten` and `map` which can usually be written as a \ - single method call" + pub MAP_FLATTEN, + pedantic, + "using combinations of `flatten` and `map` which can usually be written as a single method call" } /// **What it does:** Checks for usage of `_.filter(_).map(_)`, @@ -300,10 +293,9 @@ pedantic, /// iter.filter(|x| x == 0).map(|x| x * 2) /// ``` declare_clippy_lint! { -pub FILTER_MAP, -pedantic, -"using combinations of `filter`, `map`, `filter_map` and `flat_map` which can \ - usually be written as a single method call" + pub FILTER_MAP, + pedantic, + "using combinations of `filter`, `map`, `filter_map` and `flat_map` which can usually be written as a single method call" } /// **What it does:** Checks for an iterator search (such as `find()`, @@ -319,10 +311,9 @@ pedantic, /// iter.find(|x| x == 0).is_some() /// ``` declare_clippy_lint! { -pub SEARCH_IS_SOME, -complexity, -"using an iterator search followed by `is_some()`, which is more succinctly \ - expressed as a call to `any()`" + pub SEARCH_IS_SOME, + complexity, + "using an iterator search followed by `is_some()`, which is more succinctly expressed as a call to `any()`" } /// **What it does:** Checks for usage of `.chars().next()` on a `str` to check @@ -485,10 +476,9 @@ declare_clippy_lint! { /// **Example:** /// `_.split("x")` could be `_.split('x')` declare_clippy_lint! { -pub SINGLE_CHAR_PATTERN, -perf, -"using a single-character str where a char could be used, e.g. \ - `_.split(\"x\")`" + pub SINGLE_CHAR_PATTERN, + perf, + "using a single-character str where a char could be used, e.g. `_.split(\"x\")`" } /// **What it does:** Checks for getting the inner pointer of a temporary diff --git a/clippy_lints/src/mut_reference.rs b/clippy_lints/src/mut_reference.rs index 6729b2030a7..b92b3358cea 100644 --- a/clippy_lints/src/mut_reference.rs +++ b/clippy_lints/src/mut_reference.rs @@ -27,10 +27,9 @@ use crate::utils::span_lint; /// my_vec.push(&mut value) /// ``` declare_clippy_lint! { -pub UNNECESSARY_MUT_PASSED, -style, -"an argument passed as a mutable reference although the callee only demands an \ - immutable reference" + pub UNNECESSARY_MUT_PASSED, + style, + "an argument passed as a mutable reference although the callee only demands an immutable reference" } #[derive(Copy, Clone)] diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs index fec8e2490da..e3db1f79ed7 100644 --- a/clippy_lints/src/needless_bool.rs +++ b/clippy_lints/src/needless_bool.rs @@ -40,10 +40,9 @@ use crate::utils::{in_macro, snippet_with_applicability, span_lint, span_lint_an /// } /// ``` declare_clippy_lint! { -pub NEEDLESS_BOOL, -complexity, -"if-statements with plain booleans in the then- and else-clause, e.g. \ - `if p { true } else { false }`" + pub NEEDLESS_BOOL, + complexity, + "if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`" } /// **What it does:** Checks for expressions of the form `x == true` (or vice diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 139b87dffe7..82e69889395 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -54,10 +54,9 @@ use std::borrow::Cow; /// fn foo(&Vec) { .. } /// ``` declare_clippy_lint! { -pub PTR_ARG, -style, -"fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` \ - instead, respectively" + pub PTR_ARG, + style, + "fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively" } /// **What it does:** This lint checks for equality comparisons with `ptr::null` diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 892f9e57752..c7dc6e1cde7 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -65,10 +65,9 @@ declare_clippy_lint! { /// } /// ``` declare_clippy_lint! { -pub LET_AND_RETURN, -style, -"creating a let-binding and then immediately returning it like `let x = expr; x` at \ - the end of a block" + pub LET_AND_RETURN, + style, + "creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block" } /// **What it does:** Checks for unit (`()`) expressions that can be removed. diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index caeccf0cba2..472596beaf7 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -56,10 +56,9 @@ declare_clippy_lint! { /// let y = x + 1; /// ``` declare_clippy_lint! { -pub SHADOW_REUSE, -restriction, -"rebinding a name to an expression that re-uses the original value, e.g. \ - `let x = x + 1`" + pub SHADOW_REUSE, + restriction, + "rebinding a name to an expression that re-uses the original value, e.g. `let x = x + 1`" } /// **What it does:** Checks for bindings that shadow other bindings already in diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index 65d8e33ce7b..ec6439aef95 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -81,7 +81,8 @@ declare_clippy_lint! { /// **Example:** /// ```rust /// let _: &T = std::mem::transmute(p); // where p: *const T -/// // can be written: +/// +/// // can be written: /// let _: &T = &*p; /// ``` declare_clippy_lint! { @@ -108,7 +109,8 @@ declare_clippy_lint! { /// **Example:** /// ```rust /// let _: char = std::mem::transmute(x); // where x: u32 -/// // should be: +/// +/// // should be: /// let _ = std::char::from_u32(x).unwrap(); /// ``` declare_clippy_lint! { @@ -135,7 +137,8 @@ declare_clippy_lint! { /// **Example:** /// ```rust /// let _: &str = std::mem::transmute(b); // where b: &[u8] -/// // should be: +/// +/// // should be: /// let _ = std::str::from_utf8(b).unwrap(); /// ``` declare_clippy_lint! { @@ -153,7 +156,8 @@ declare_clippy_lint! { /// **Example:** /// ```rust /// let _: bool = std::mem::transmute(x); // where x: u8 -/// // should be: +/// +/// // should be: /// let _: bool = x != 0; /// ``` declare_clippy_lint! { @@ -171,7 +175,8 @@ declare_clippy_lint! { /// **Example:** /// ```rust /// let _: f32 = std::mem::transmute(x); // where x: u32 -/// // should be: +/// +/// // should be: /// let _: f32 = f32::from_bits(x); /// ``` declare_clippy_lint! { diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 6d5dbfe0713..1b98a89f868 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -117,10 +117,9 @@ declare_clippy_lint! { /// let x = LinkedList::new(); /// ``` declare_clippy_lint! { -pub LINKEDLIST, -pedantic, -"usage of LinkedList, usually a vector is faster, or a more specialized data \ - structure like a VecDeque" + pub LINKEDLIST, + pedantic, + "usage of LinkedList, usually a vector is faster, or a more specialized data structure like a VecDeque" } /// **What it does:** Checks for use of `&Box` anywhere in the code. @@ -668,10 +667,9 @@ declare_clippy_lint! { /// } /// ``` declare_clippy_lint! { -pub CAST_POSSIBLE_TRUNCATION, -pedantic, -"casts that may cause truncation of the value, e.g. `x as u8` where `x: u32`, \ - or `x as i32` where `x: f32`" + pub CAST_POSSIBLE_TRUNCATION, + pedantic, + "casts that may cause truncation of the value, e.g. `x as u8` where `x: u32`, or `x as i32` where `x: f32`" } /// **What it does:** Checks for casts from an unsigned type to a signed type of @@ -692,10 +690,9 @@ pedantic, /// u32::MAX as i32 // will yield a value of `-1` /// ``` declare_clippy_lint! { -pub CAST_POSSIBLE_WRAP, -pedantic, -"casts that may cause wrapping around the value, e.g. `x as i32` where `x: u32` \ - and `x > i32::MAX`" + pub CAST_POSSIBLE_WRAP, + pedantic, + "casts that may cause wrapping around the value, e.g. `x as i32` where `x: u32` and `x > i32::MAX`" } /// **What it does:** Checks for on casts between numerical types that may diff --git a/clippy_lints/src/unicode.rs b/clippy_lints/src/unicode.rs index bb384891926..723565b3a9b 100644 --- a/clippy_lints/src/unicode.rs +++ b/clippy_lints/src/unicode.rs @@ -45,10 +45,9 @@ declare_clippy_lint! { /// let x = "Hä?" /// ``` declare_clippy_lint! { -pub NON_ASCII_LITERAL, -pedantic, -"using any literal non-ASCII chars in a string literal instead of \ - using the `\\u` escape" + pub NON_ASCII_LITERAL, + pedantic, + "using any literal non-ASCII chars in a string literal instead of using the `\\u` escape" } /// **What it does:** Checks for string literals that contain Unicode in a form @@ -63,10 +62,9 @@ pedantic, /// **Example:** You may not see it, but “à” and “à” aren't the same string. The /// former when escaped is actually `"a\u{300}"` while the latter is `"\u{e0}"`. declare_clippy_lint! { -pub UNICODE_NOT_NFC, -pedantic, -"using a unicode literal not in NFC normal form (see \ - [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)" + pub UNICODE_NOT_NFC, + pedantic, + "using a unicode literal not in NFC normal form (see [unicode tr15](http://www.unicode.org/reports/tr15/) for further information)" } #[derive(Copy, Clone)]