diff --git a/clippy_lints/src/assign_ops.rs b/clippy_lints/src/assign_ops.rs index d42880f8b9f..122a600d7d3 100644 --- a/clippy_lints/src/assign_ops.rs +++ b/clippy_lints/src/assign_ops.rs @@ -126,7 +126,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps { return; // implements_trait does not work with generics } macro_rules! ops { - ($op:expr, $cx:expr, $ty:expr, $rty:expr, $($trait_name:ident:$full_trait_name:ident),+) => { + ($op:expr, + $cx:expr, + $ty:expr, + $rty:expr, + $($trait_name:ident:$full_trait_name:ident),+) => { match $op { $(hir::$full_trait_name => { let [krate, module] = ::utils::paths::OPS_MODULE; @@ -140,15 +144,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps { let parent_fn = cx.tcx.map.get_parent(e.id); let parent_impl = cx.tcx.map.get_parent(parent_fn); // the crate node is the only one that is not in the map - if parent_impl != ast::CRATE_NODE_ID { - if let hir::map::Node::NodeItem(item) = cx.tcx.map.get(parent_impl) { - if let hir::Item_::ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node { - if trait_ref.path.def.def_id() == trait_id { - return; - } - } - } - } + if_let_chain!{[ + parent_impl != ast::CRATE_NODE_ID, + let hir::map::Node::NodeItem(item) = cx.tcx.map.get(parent_impl), + let hir::Item_::ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node, + trait_ref.path.def.def_id() == trait_id + ], { return; }} implements_trait($cx, $ty, trait_id, vec![$rty]) },)* _ => false, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index c6852477a11..e1d7e2e9e2c 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -175,8 +175,10 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { let mut store = reg.sess.lint_store.borrow_mut(); store.register_removed("unstable_as_slice", "`Vec::as_slice` has been stabilized in 1.7"); store.register_removed("unstable_as_mut_slice", "`Vec::as_mut_slice` has been stabilized in 1.7"); - store.register_removed("str_to_string", "using `str::to_string` is common even today and specialization will likely happen soon"); - store.register_removed("string_to_string", "using `string::to_string` is common even today and specialization will likely happen soon"); + store.register_removed("str_to_string", + "using `str::to_string` is common even today and specialization will likely happen soon"); + store.register_removed("string_to_string", + "using `string::to_string` is common even today and specialization will likely happen soon"); // end deprecated lints, do not remove this comment, it’s used in `update_lints` reg.register_late_lint_pass(box serde::Serde); @@ -229,7 +231,9 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { reg.register_late_lint_pass(box map_clone::Pass); reg.register_late_lint_pass(box temporary_assignment::Pass); reg.register_late_lint_pass(box transmute::Transmute); - reg.register_late_lint_pass(box cyclomatic_complexity::CyclomaticComplexity::new(conf.cyclomatic_complexity_threshold)); + reg.register_late_lint_pass( + box cyclomatic_complexity::CyclomaticComplexity::new(conf.cyclomatic_complexity_threshold) + ); reg.register_late_lint_pass(box escape::Pass{too_large_for_stack: conf.too_large_for_stack}); reg.register_early_lint_pass(box misc_early::MiscEarly); reg.register_late_lint_pass(box array_indexing::ArrayIndexing); diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index bccc1164047..e6f044ae9b6 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -333,8 +333,16 @@ impl EarlyLintPass for MiscEarly { lit.span, "this is a decimal constant", |db| { - db.span_suggestion(lit.span, "if you mean to use a decimal constant, remove the `0` to remove confusion:", src[1..].to_string()); - db.span_suggestion(lit.span, "if you mean to use an octal constant, use `0o`:", format!("0o{}", &src[1..])); + db.span_suggestion( + lit.span, + "if you mean to use a decimal constant, remove the `0` to remove confusion:", + src[1..].to_string(), + ); + db.span_suggestion( + lit.span, + "if you mean to use an octal constant, use `0o`:", + format!("0o{}", &src[1..]), + ); }); } }} @@ -374,7 +382,12 @@ impl EarlyLintPass for MiscEarly { let ExprKind::Path(_, ref path) = closure.node ], { if sp_ident.node == (&path.segments[0]).identifier { - span_lint(cx, REDUNDANT_CLOSURE_CALL, second.span, "Closure called just once immediately after it was declared"); + span_lint( + cx, + REDUNDANT_CLOSURE_CALL, + second.span, + "Closure called just once immediately after it was declared", + ); } }} } diff --git a/clippy_lints/src/swap.rs b/clippy_lints/src/swap.rs index 7393ee3890e..8ed7e355879 100644 --- a/clippy_lints/src/swap.rs +++ b/clippy_lints/src/swap.rs @@ -81,7 +81,11 @@ fn check_manual_swap(cx: &LateContext, block: &Block) { SpanlessEq::new(cx).ignore_fn().eq_expr(tmp_init, lhs1), SpanlessEq::new(cx).ignore_fn().eq_expr(rhs1, lhs2) ], { - fn check_for_slice<'a>(cx: &LateContext, lhs1: &'a Expr, lhs2: &'a Expr) -> Option<(&'a Expr, &'a Expr, &'a Expr)> { + fn check_for_slice<'a>( + cx: &LateContext, + lhs1: &'a Expr, + lhs2: &'a Expr, + ) -> Option<(&'a Expr, &'a Expr, &'a Expr)> { if let ExprIndex(ref lhs1, ref idx1) = lhs1.node { if let ExprIndex(ref lhs2, ref idx2) = lhs2.node { if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, lhs2) { @@ -104,7 +108,10 @@ fn check_manual_swap(cx: &LateContext, block: &Block) { if let Some(slice) = Sugg::hir_opt(cx, slice) { (false, format!(" elements of `{}`", slice), - format!("{}.swap({}, {})", slice.maybe_par(), snippet(cx, idx1.span, ".."), snippet(cx, idx2.span, ".."))) + format!("{}.swap({}, {})", + slice.maybe_par(), + snippet(cx, idx1.span, ".."), + snippet(cx, idx2.span, ".."))) } else { (false, "".to_owned(), "".to_owned()) } @@ -148,7 +155,9 @@ fn check_suspicious_swap(cx: &LateContext, block: &Block) { SpanlessEq::new(cx).ignore_fn().eq_expr(lhs0, rhs1), SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, rhs0) ], { - let (what, lhs, rhs) = if let (Some(first), Some(second)) = (Sugg::hir_opt(cx, lhs0), Sugg::hir_opt(cx, rhs0)) { + let lhs0 = Sugg::hir_opt(cx, lhs0); + let rhs0 = Sugg::hir_opt(cx, rhs0); + let (what, lhs, rhs) = if let (Some(first), Some(second)) = (lhs0, rhs0) { (format!(" `{}` and `{}`", first, second), first.mut_addr().to_string(), second.mut_addr().to_string()) } else { ("".to_owned(), "".to_owned(), "".to_owned()) diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 7c83a7f69a5..d80fa17e29f 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -131,7 +131,12 @@ macro_rules! define_Conf { // how to read the value? (CONV i64, $value: expr) => { $value.as_integer() }; - (CONV u64, $value: expr) => { $value.as_integer().iter().filter_map(|&i| if i >= 0 { Some(i as u64) } else { None }).next() }; + (CONV u64, $value: expr) => { + $value.as_integer() + .iter() + .filter_map(|&i| if i >= 0 { Some(i as u64) } else { None }) + .next() + }; (CONV String, $value: expr) => { $value.as_str().map(Into::into) }; (CONV Vec, $value: expr) => {{ let slice = $value.as_slice(); @@ -139,12 +144,10 @@ macro_rules! define_Conf { if let Some(slice) = slice { if slice.iter().any(|v| v.as_str().is_none()) { None + } else { + Some(slice.iter().map(|v| v.as_str().expect("already checked").to_owned()).collect()) } - else { - Some(slice.iter().map(|v| v.as_str().unwrap_or_else(|| unreachable!()).to_owned()).collect()) - } - } - else { + } else { None } }}; @@ -160,7 +163,19 @@ define_Conf! { /// Lint: CYCLOMATIC_COMPLEXITY. The maximum cyclomatic complexity a function can have ("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25 => u64), /// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks - ("doc-valid-idents", doc_valid_idents, ["MiB", "GiB", "TiB", "PiB", "EiB", "DirectX", "GPLv2", "GPLv3", "GitHub", "IPv4", "IPv6", "JavaScript", "NaN", "OAuth", "OpenGL", "TrueType", "iOS", "macOS"] => Vec), + ("doc-valid-idents", doc_valid_idents, [ + "MiB", "GiB", "TiB", "PiB", "EiB", + "DirectX", + "GPLv2", "GPLv3", + "GitHub", + "IPv4", "IPv6", + "JavaScript", + "NaN", + "OAuth", + "OpenGL", + "TrueType", + "iOS", "macOS", + ] => Vec), /// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have ("too-many-arguments-threshold", too_many_arguments_threshold, 7 => u64), /// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have diff --git a/tests/compile-fail/absurd-extreme-comparisons.rs b/tests/compile-fail/absurd-extreme-comparisons.rs index 627cd888aac..81cb658df8d 100644 --- a/tests/compile-fail/absurd-extreme-comparisons.rs +++ b/tests/compile-fail/absurd-extreme-comparisons.rs @@ -9,7 +9,7 @@ fn main() { let u: u32 = 42; u <= 0; - //~^ ERROR this comparison involving the minimum or maximum element for this type contains a case that is always true or always false + //~^ ERROR this comparison involving the minimum or maximum element for this type contains a //~| HELP using u == 0 instead u <= Z; //~^ ERROR this comparison involving @@ -41,10 +41,10 @@ fn main() { //~| HELP because 1-1 is the minimum value for this type, this comparison is always false u >= !0; //~^ ERROR this comparison involving - //~| HELP because !0 is the maximum value for this type, the case where the two sides are not equal never occurs, consider using u == !0 instead + //~| HELP consider using u == !0 instead u <= 12 - 2*6; //~^ ERROR this comparison involving - //~| HELP because 12 - 2*6 is the minimum value for this type, the case where the two sides are not equal never occurs, consider using u == 12 - 2*6 instead + //~| HELP consider using u == 12 - 2*6 instead let i: i8 = 0; i < -127 - 1; diff --git a/tests/compile-fail/block_in_if_condition.rs b/tests/compile-fail/block_in_if_condition.rs index 43216754f75..090c39abc92 100644 --- a/tests/compile-fail/block_in_if_condition.rs +++ b/tests/compile-fail/block_in_if_condition.rs @@ -21,13 +21,13 @@ macro_rules! blocky_too { fn macro_if() { if blocky!() { } - + if blocky_too!() { } } fn condition_has_block() -> i32 { - if { //~ERROR in an 'if' condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a 'let' + if { //~ERROR in an 'if' condition, avoid complex blocks or closures with blocks; let x = 3; x == 3 } { @@ -55,12 +55,12 @@ fn pred_test() { // this is a sneaky case, where the block isn't directly in the condition, but is actually // inside a closure that the condition is using. same principle applies. add some extra // expressions to make sure linter isn't confused by them. - if v == 3 && sky == "blue" && predicate(|x| { let target = 3; x == target }, v) { //~ERROR in an 'if' condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a 'let' - + if v == 3 && sky == "blue" && predicate(|x| { let target = 3; x == target }, v) { + //~^ERROR in an 'if' condition, avoid complex blocks or closures with blocks; } - if predicate(|x| { let target = 3; x == target }, v) { //~ERROR in an 'if' condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a 'let' - + if predicate(|x| { let target = 3; x == target }, v) { + //~^ERROR in an 'if' condition, avoid complex blocks or closures with blocks; } }