From 5650a599a8727ecab31537fc78e9d1ddec0f6d56 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Fri, 10 Feb 2017 19:39:03 +0100 Subject: [PATCH 01/13] New mut_from_ref lint This fixes #1507. --- CHANGELOG.md | 3 +++ README.md | 3 ++- clippy_lints/src/lib.rs | 1 + clippy_lints/src/ptr.rs | 41 +++++++++++++++++++++++++++++++++++- tests/ui/mut_from_ref.rs | 40 +++++++++++++++++++++++++++++++++++ tests/ui/mut_from_ref.stderr | 26 +++++++++++++++++++++++ 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 tests/ui/mut_from_ref.rs create mode 100644 tests/ui/mut_from_ref.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index b72387f0d71..671450a120d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Change Log All notable changes to this project will be documented in this file. +* New [`mut_from_ref`] lint + ## 0.0.114 — 2017-02-08 * Rustup to rustc 1.17.0-nightly (c49d10207 2017-02-07) * Tests are now ui tests (testing the exact output of rustc) @@ -369,6 +371,7 @@ All notable changes to this project will be documented in this file. [`mixed_case_hex_literals`]: https://github.com/Manishearth/rust-clippy/wiki#mixed_case_hex_literals [`module_inception`]: https://github.com/Manishearth/rust-clippy/wiki#module_inception [`modulo_one`]: https://github.com/Manishearth/rust-clippy/wiki#modulo_one +[`mut_from_ref`]: https://github.com/Manishearth/rust-clippy/wiki#mut_from_ref [`mut_mut`]: https://github.com/Manishearth/rust-clippy/wiki#mut_mut [`mutex_atomic`]: https://github.com/Manishearth/rust-clippy/wiki#mutex_atomic [`mutex_integer`]: https://github.com/Manishearth/rust-clippy/wiki#mutex_integer diff --git a/README.md b/README.md index 3f3ed135e0c..17282a7fc36 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ transparently: ## Lints -There are 186 lints included in this crate: +There are 187 lints included in this crate: name | default | triggers on -----------------------------------------------------------------------------------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------- @@ -278,6 +278,7 @@ name [mixed_case_hex_literals](https://github.com/Manishearth/rust-clippy/wiki#mixed_case_hex_literals) | warn | hex literals whose letter digits are not consistently upper- or lowercased [module_inception](https://github.com/Manishearth/rust-clippy/wiki#module_inception) | warn | modules that have the same name as their parent module [modulo_one](https://github.com/Manishearth/rust-clippy/wiki#modulo_one) | warn | taking a number modulo 1, which always returns 0 +[mut_from_ref](https://github.com/Manishearth/rust-clippy/wiki#mut_from_ref) | warn | fns that create mutable refs from immutable ref args [mut_mut](https://github.com/Manishearth/rust-clippy/wiki#mut_mut) | allow | usage of double-mut refs, e.g. `&mut &mut ...` [mutex_atomic](https://github.com/Manishearth/rust-clippy/wiki#mutex_atomic) | warn | using a mutex where an atomic value could be used instead [mutex_integer](https://github.com/Manishearth/rust-clippy/wiki#mutex_integer) | allow | using a mutex for an integer type diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 1099738d799..da8b04bdf6f 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -464,6 +464,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { precedence::PRECEDENCE, print::PRINT_WITH_NEWLINE, ptr::CMP_NULL, + ptr::MUT_FROM_REF, ptr::PTR_ARG, ranges::RANGE_STEP_BY_ZERO, ranges::RANGE_ZIP_WITH_LEN, diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 590e3d587d4..7a7631d9769 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -44,13 +44,30 @@ declare_lint! { "comparing a pointer to a null pointer, suggesting to use `.is_null()` instead." } +/// **What it does:** This lint checks for functions that take immutable refs and return +/// mutable ones. +/// +/// **Why is this bad?** This is trivially unsound, as one can create two mutable refs +/// from the same source. +/// +/// **Known problems:** This lint will overlook functions where input and output lifetimes differ +/// +/// **Example:** +/// ```rust +/// fn foo(&Foo) -> &mut Bar { .. } +/// ``` +declare_lint! { + pub MUT_FROM_REF, + Warn, + "fns that create mutable refs from immutable ref args" +} #[derive(Copy,Clone)] pub struct PointerPass; impl LintPass for PointerPass { fn get_lints(&self) -> LintArray { - lint_array!(PTR_ARG, CMP_NULL) + lint_array!(PTR_ARG, CMP_NULL, MUT_FROM_REF) } } @@ -111,6 +128,28 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) { } } } + + if let FunctionRetTy::Return(ref ty) = decl.output { + if let Some((out, MutMutable)) = get_rptr_lm(ty) { + if let Some(MutImmutable) = decl.inputs.iter() + .filter_map(|ty| get_rptr_lm(ty)) + .filter(|&(lt, _)| lt.name == out.name) + .fold(None, |x, (_, m)| match (x, m) { + (Some(MutMutable), _) | + (_, MutMutable) => Some(MutMutable), + (_, m) => Some(m), + }) { + span_lint(cx, + MUT_FROM_REF, + ty.span, + "this function takes an immutable ref to return a mutable one:"); + } + } + } +} + +fn get_rptr_lm(ty: &Ty) -> Option<(&Lifetime, Mutability)> { + if let Ty_::TyRptr(ref lt, ref m) = ty.node { Some((lt, m.mutbl)) } else { None } } fn is_null_path(expr: &Expr) -> bool { diff --git a/tests/ui/mut_from_ref.rs b/tests/ui/mut_from_ref.rs new file mode 100644 index 00000000000..09cc8a6061c --- /dev/null +++ b/tests/ui/mut_from_ref.rs @@ -0,0 +1,40 @@ +#![feature(plugin)] +#![plugin(clippy)] +#![allow(unused)] +#![deny(mut_from_ref)] + +struct Foo; + +impl Foo { + fn this_wont_hurt_a_bit(&self) -> &mut Foo { + unimplemented!() + } +} + +trait Ouch { + fn ouch(x: &Foo) -> &mut Foo; +} + +impl Ouch for Foo { + fn ouch(x: &Foo) -> &mut Foo { + unimplemented!() + } +} + +fn fail(x: &u32) -> &mut u16 { + unimplemented!() +} + +// this is OK, because the result borrows y +fn works<'a>(x: &u32, y: &'a mut u32) -> &'a mut u32 { + unimplemented!() +} + +// this is also OK, because the result could borrow y +fn also_works<'a>(x: &'a u32, y: &'a mut u32) -> &'a mut u32 { + unimplemented!() +} + +fn main() { + //TODO +} diff --git a/tests/ui/mut_from_ref.stderr b/tests/ui/mut_from_ref.stderr new file mode 100644 index 00000000000..23fc20d9a4b --- /dev/null +++ b/tests/ui/mut_from_ref.stderr @@ -0,0 +1,26 @@ +error: this function takes an immutable ref to return a mutable one: + --> $DIR/mut_from_ref.rs:9:39 + | +9 | fn this_wont_hurt_a_bit(&self) -> &mut Foo { + | ^^^^^^^^ + | +note: lint level defined here + --> $DIR/mut_from_ref.rs:4:9 + | +4 | #![deny(mut_from_ref)] + | ^^^^^^^^^^^^ + +error: this function takes an immutable ref to return a mutable one: + --> $DIR/mut_from_ref.rs:15:25 + | +15 | fn ouch(x: &Foo) -> &mut Foo; + | ^^^^^^^^ + +error: this function takes an immutable ref to return a mutable one: + --> $DIR/mut_from_ref.rs:24:21 + | +24 | fn fail(x: &u32) -> &mut u16 { + | ^^^^^^^^ + +error: aborting due to 3 previous errors + From bff4c30ac7614c636a085e9ae65e1dec2714804e Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sat, 11 Feb 2017 00:32:12 +0100 Subject: [PATCH 02/13] added test, fixed message & description, rustfmt --- clippy_lints/src/ptr.rs | 30 +++++++++++++++++++----------- tests/ui/mut_from_ref.rs | 4 ++++ tests/ui/mut_from_ref.stderr | 21 +++++++++++++-------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 7a7631d9769..a30d632f159 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -44,13 +44,15 @@ declare_lint! { "comparing a pointer to a null pointer, suggesting to use `.is_null()` instead." } -/// **What it does:** This lint checks for functions that take immutable refs and return +/// **What it does:** This lint checks for functions that take immutable references and return /// mutable ones. /// -/// **Why is this bad?** This is trivially unsound, as one can create two mutable refs -/// from the same source. +/// **Why is this bad?** This is trivially unsound, as one can create two mutable references +/// from the same (immutable!) source. This [error](https://github.com/rust-lang/rust/issues/39465) +/// actually lead to an interim Rust release 1.15.1. /// -/// **Known problems:** This lint will overlook functions where input and output lifetimes differ +/// **Known problems:** To be on the conservative side, if there's at least one mutable reference +/// with the output lifetime, this lint will not trigger. In practice, this case is unlikely anyway. /// /// **Example:** /// ```rust @@ -131,25 +133,31 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) { if let FunctionRetTy::Return(ref ty) = decl.output { if let Some((out, MutMutable)) = get_rptr_lm(ty) { - if let Some(MutImmutable) = decl.inputs.iter() - .filter_map(|ty| get_rptr_lm(ty)) - .filter(|&(lt, _)| lt.name == out.name) - .fold(None, |x, (_, m)| match (x, m) { + if let Some(MutImmutable) = + decl.inputs + .iter() + .filter_map(|ty| get_rptr_lm(ty)) + .filter(|&(lt, _)| lt.name == out.name) + .fold(None, |x, (_, m)| match (x, m) { (Some(MutMutable), _) | (_, MutMutable) => Some(MutMutable), (_, m) => Some(m), - }) { + }) { span_lint(cx, MUT_FROM_REF, ty.span, - "this function takes an immutable ref to return a mutable one:"); + "this function takes an immutable ref to return a mutable one"); } } } } fn get_rptr_lm(ty: &Ty) -> Option<(&Lifetime, Mutability)> { - if let Ty_::TyRptr(ref lt, ref m) = ty.node { Some((lt, m.mutbl)) } else { None } + if let Ty_::TyRptr(ref lt, ref m) = ty.node { + Some((lt, m.mutbl)) + } else { + None + } } fn is_null_path(expr: &Expr) -> bool { diff --git a/tests/ui/mut_from_ref.rs b/tests/ui/mut_from_ref.rs index 09cc8a6061c..24b73eacc32 100644 --- a/tests/ui/mut_from_ref.rs +++ b/tests/ui/mut_from_ref.rs @@ -25,6 +25,10 @@ fn fail(x: &u32) -> &mut u16 { unimplemented!() } +fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { + unimplemented!() +} + // this is OK, because the result borrows y fn works<'a>(x: &u32, y: &'a mut u32) -> &'a mut u32 { unimplemented!() diff --git a/tests/ui/mut_from_ref.stderr b/tests/ui/mut_from_ref.stderr index 23fc20d9a4b..799adb00714 100644 --- a/tests/ui/mut_from_ref.stderr +++ b/tests/ui/mut_from_ref.stderr @@ -1,26 +1,31 @@ -error: this function takes an immutable ref to return a mutable one: - --> $DIR/mut_from_ref.rs:9:39 +error: this function takes an immutable ref to return a mutable one + --> tests/ui/mut_from_ref.rs:9:39 | 9 | fn this_wont_hurt_a_bit(&self) -> &mut Foo { | ^^^^^^^^ | note: lint level defined here - --> $DIR/mut_from_ref.rs:4:9 + --> tests/ui/mut_from_ref.rs:4:9 | 4 | #![deny(mut_from_ref)] | ^^^^^^^^^^^^ -error: this function takes an immutable ref to return a mutable one: - --> $DIR/mut_from_ref.rs:15:25 +error: this function takes an immutable ref to return a mutable one + --> tests/ui/mut_from_ref.rs:15:25 | 15 | fn ouch(x: &Foo) -> &mut Foo; | ^^^^^^^^ -error: this function takes an immutable ref to return a mutable one: - --> $DIR/mut_from_ref.rs:24:21 +error: this function takes an immutable ref to return a mutable one + --> tests/ui/mut_from_ref.rs:24:21 | 24 | fn fail(x: &u32) -> &mut u16 { | ^^^^^^^^ -error: aborting due to 3 previous errors +error: this function takes an immutable ref to return a mutable one + --> tests/ui/mut_from_ref.rs:28:50 + | +28 | fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { + | ^^^^^^^^^^^ +error: aborting due to 4 previous errors From 673ee4800dd2023b1ecc5376bb8e6003683d7c3d Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sat, 11 Feb 2017 01:41:49 +0100 Subject: [PATCH 03/13] fix test --- tests/ui/mut_from_ref.stderr | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/ui/mut_from_ref.stderr b/tests/ui/mut_from_ref.stderr index 799adb00714..509ac83b4da 100644 --- a/tests/ui/mut_from_ref.stderr +++ b/tests/ui/mut_from_ref.stderr @@ -1,31 +1,32 @@ error: this function takes an immutable ref to return a mutable one - --> tests/ui/mut_from_ref.rs:9:39 + --> $DIR/mut_from_ref.rs:9:39 | 9 | fn this_wont_hurt_a_bit(&self) -> &mut Foo { | ^^^^^^^^ | note: lint level defined here - --> tests/ui/mut_from_ref.rs:4:9 + --> $DIR/mut_from_ref.rs:4:9 | 4 | #![deny(mut_from_ref)] | ^^^^^^^^^^^^ error: this function takes an immutable ref to return a mutable one - --> tests/ui/mut_from_ref.rs:15:25 + --> $DIR/mut_from_ref.rs:15:25 | 15 | fn ouch(x: &Foo) -> &mut Foo; | ^^^^^^^^ error: this function takes an immutable ref to return a mutable one - --> tests/ui/mut_from_ref.rs:24:21 + --> $DIR/mut_from_ref.rs:24:21 | 24 | fn fail(x: &u32) -> &mut u16 { | ^^^^^^^^ error: this function takes an immutable ref to return a mutable one - --> tests/ui/mut_from_ref.rs:28:50 + --> $DIR/mut_from_ref.rs:28:50 | 28 | fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { | ^^^^^^^^^^^ error: aborting due to 4 previous errors + From ad01fa9b57a0d30358e5ab74e0ecbc565e005f7d Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sat, 11 Feb 2017 01:42:14 +0100 Subject: [PATCH 04/13] Remove stabilized feature flag --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 7a744356eb4..ac90528c606 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ // error-pattern:yummy #![feature(box_syntax)] #![feature(rustc_private)] -#![feature(static_in_const)] #![allow(unknown_lints, missing_docs_in_private_items)] From a2d752807a017f70f28a70b7f16dc6a20cd566ee Mon Sep 17 00:00:00 2001 From: Bood Qian Date: Sat, 11 Feb 2017 14:57:50 +0800 Subject: [PATCH 05/13] Lint on `Err(_)` arm of a match --- CHANGELOG.md | 1 + README.md | 3 +- clippy_lints/src/lib.rs | 1 + clippy_lints/src/matches.rs | 65 +++++++++++++++++++++++++++++++++++-- tests/ui/matches.rs | 38 ++++++++++++++++++++++ tests/ui/matches.stderr | 25 +++++++++++++- 6 files changed, 129 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b72387f0d71..4d6aff398b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -362,6 +362,7 @@ All notable changes to this project will be documented in this file. [`match_overlapping_arm`]: https://github.com/Manishearth/rust-clippy/wiki#match_overlapping_arm [`match_ref_pats`]: https://github.com/Manishearth/rust-clippy/wiki#match_ref_pats [`match_same_arms`]: https://github.com/Manishearth/rust-clippy/wiki#match_same_arms +[`match_wild_err_arm`]: https://github.com/Manishearth/rust-clippy/wiki#match_wild_err_arm [`mem_forget`]: https://github.com/Manishearth/rust-clippy/wiki#mem_forget [`min_max`]: https://github.com/Manishearth/rust-clippy/wiki#min_max [`misrefactored_assign_op`]: https://github.com/Manishearth/rust-clippy/wiki#misrefactored_assign_op diff --git a/README.md b/README.md index 3f3ed135e0c..de01a253323 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ transparently: ## Lints -There are 186 lints included in this crate: +There are 187 lints included in this crate: name | default | triggers on -----------------------------------------------------------------------------------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------- @@ -271,6 +271,7 @@ name [match_overlapping_arm](https://github.com/Manishearth/rust-clippy/wiki#match_overlapping_arm) | warn | a match with overlapping arms [match_ref_pats](https://github.com/Manishearth/rust-clippy/wiki#match_ref_pats) | warn | a match or `if let` with all arms prefixed with `&` instead of deref-ing the match expression [match_same_arms](https://github.com/Manishearth/rust-clippy/wiki#match_same_arms) | warn | `match` with identical arm bodies +[match_wild_err_arm](https://github.com/Manishearth/rust-clippy/wiki#match_wild_err_arm) | warn | a match with `Err(_)` arm [mem_forget](https://github.com/Manishearth/rust-clippy/wiki#mem_forget) | allow | `mem::forget` usage on `Drop` types, likely to cause memory leaks [min_max](https://github.com/Manishearth/rust-clippy/wiki#min_max) | warn | `min(_, max(_, _))` (or vice versa) with bounds clamping the result to a constant [misrefactored_assign_op](https://github.com/Manishearth/rust-clippy/wiki#misrefactored_assign_op) | warn | having a variable on both sides of an assign op diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 1099738d799..661e6415bb3 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -412,6 +412,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { matches::MATCH_BOOL, matches::MATCH_OVERLAPPING_ARM, matches::MATCH_REF_PATS, + matches::MATCH_WILD_ERR_ARM, matches::SINGLE_MATCH, methods::CHARS_NEXT_CMP, methods::CLONE_DOUBLE_REF, diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 9cb557ba646..d9e5daf889f 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -10,7 +10,7 @@ use std::collections::Bound; use syntax::ast::LitKind; use syntax::codemap::Span; use utils::paths; -use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block}; +use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block, walk_ptrs_ty, is_expn_of}; use utils::sugg::Sugg; /// **What it does:** Checks for matches with a single arm where an `if let` @@ -121,6 +121,26 @@ declare_lint! { "a match with overlapping arms" } +/// **What it does:** Checks for arm matches all errors with `Err(_)`. +/// +/// **Why is this bad?** It is a bad practice to catch all errors the same way +/// +/// **Known problems:** None. +/// +/// **Example:** +/// ```rust +/// let x : Result(i32, &str) = Ok(3); +/// match x { +/// Ok(_) => println!("ok"), +/// Err(_) => println!("err"), +/// } +/// ``` +declare_lint! { + pub MATCH_WILD_ERR_ARM, + Warn, + "a match with `Err(_)` arm" +} + #[allow(missing_copy_implementations)] pub struct MatchPass; @@ -130,7 +150,8 @@ impl LintPass for MatchPass { MATCH_REF_PATS, MATCH_BOOL, SINGLE_MATCH_ELSE, - MATCH_OVERLAPPING_ARM) + MATCH_OVERLAPPING_ARM, + MATCH_WILD_ERR_ARM) } } @@ -143,6 +164,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass { check_single_match(cx, ex, arms, expr); check_match_bool(cx, ex, arms, expr); check_overlapping_arms(cx, ex, arms); + check_wild_err_arm(cx, ex, arms); } if let ExprMatch(ref ex, ref arms, source) = expr.node { check_match_ref_pats(cx, ex, arms, source, expr); @@ -322,6 +344,45 @@ fn check_overlapping_arms(cx: &LateContext, ex: &Expr, arms: &[Arm]) { } } +fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) { + let ex_ty = walk_ptrs_ty(cx.tables.expr_ty(ex)); + if match_type(cx, ex_ty, &paths::RESULT) { + for arm in arms { + if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pats[0].node { + let path_str = print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)); + if inner.iter().any(|pat| pat.node == PatKind::Wild) && + path_str == "Err" { + // `Err(_)` arm found + let mut need_lint = true; + if let ExprBlock(ref block) = arm.body.node { + if is_unreachable_block(cx, block) { + need_lint = false; + } + } + + if need_lint { + span_note_and_lint(cx, + MATCH_WILD_ERR_ARM, + arm.pats[0].span, + "Err(_) will match all errors, maybe not a good idea", + arm.pats[0].span, + "to remove this warning, match each error seperately or use unreachable macro"); + } + } + } + } + } +} + +// If the block contains only a `unreachable!` macro (as expression or statement) +fn is_unreachable_block(cx: &LateContext, block: &Block) -> bool { + match (&block.expr, block.stmts.len(), block.stmts.first()) { + (&Some(ref exp), 0, _) => is_expn_of(cx, exp.span, "unreachable").is_some(), + (&None, 1, Some(ref stmt)) => is_expn_of(cx, stmt.span, "unreachable").is_some(), + _ => false + } +} + fn check_match_ref_pats(cx: &LateContext, ex: &Expr, arms: &[Arm], source: MatchSource, expr: &Expr) { if has_only_ref_pats(arms) { if let ExprAddrOf(Mutability::MutImmutable, ref inner) = ex.node { diff --git a/tests/ui/matches.rs b/tests/ui/matches.rs index 00faed26818..277f96be686 100644 --- a/tests/ui/matches.rs +++ b/tests/ui/matches.rs @@ -283,5 +283,43 @@ fn overlapping() { } } +fn match_wild_err_arm() { + let x: Result = Ok(3); + + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => println!("err") + } + + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => { + println!("err"); + unreachable!() + } + } + + // allowed when using with unreachable as the only statement/expression + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => unreachable!() + } + + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => {unreachable!()} + } + + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => {unreachable!();} + } +} + fn main() { } diff --git a/tests/ui/matches.stderr b/tests/ui/matches.stderr index bc254cb0bcb..438c2e12c29 100644 --- a/tests/ui/matches.stderr +++ b/tests/ui/matches.stderr @@ -388,5 +388,28 @@ note: overlaps with this 275 | 0 ... 11 => println!("0 ... 11"), | ^^^^^^^^ -error: aborting due to 23 previous errors +error: Err(_) will match all errors, maybe not a good idea + --> $DIR/matches.rs:292:9 + | +292 | Err(_) => println!("err") + | ^^^^^^ + | + = note: #[deny(match_wild_err_arm)] implied by #[deny(clippy)] +note: lint level defined here + --> $DIR/matches.rs:5:9 + | +5 | #![deny(clippy)] + | ^^^^^^ + = note: to remove this warning, match each error seperately or use unreachable macro + +error: Err(_) will match all errors, maybe not a good idea + --> $DIR/matches.rs:298:9 + | +298 | Err(_) => { + | ^^^^^^ + | + = note: #[deny(match_wild_err_arm)] implied by #[deny(clippy)] + = note: to remove this warning, match each error seperately or use unreachable macro + +error: aborting due to 25 previous errors From 64d2f8af8e44423edc94f0ac3eb186c66966541b Mon Sep 17 00:00:00 2001 From: Bood Qian Date: Sat, 11 Feb 2017 21:42:42 +0800 Subject: [PATCH 06/13] Lint on `panic!` only --- clippy_lints/src/matches.rs | 32 +++++++++++++--------------- tests/ui/matches.rs | 42 +++++++++++++++++++++++-------------- tests/ui/matches.stderr | 15 ++++++++++--- 3 files changed, 53 insertions(+), 36 deletions(-) diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index d9e5daf889f..57216ad8d41 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -121,9 +121,11 @@ declare_lint! { "a match with overlapping arms" } -/// **What it does:** Checks for arm matches all errors with `Err(_)`. +/// **What it does:** Checks for arm which matches all errors with `Err(_)` +/// and take drastic actions like `panic!`. /// -/// **Why is this bad?** It is a bad practice to catch all errors the same way +/// **Why is this bad?** It is generally a bad practice, just like +/// catching all exceptions in java with `catch(Exception)` /// /// **Known problems:** None. /// @@ -132,13 +134,13 @@ declare_lint! { /// let x : Result(i32, &str) = Ok(3); /// match x { /// Ok(_) => println!("ok"), -/// Err(_) => println!("err"), +/// Err(_) => panic!("err"), /// } /// ``` declare_lint! { pub MATCH_WILD_ERR_ARM, Warn, - "a match with `Err(_)` arm" + "a match with `Err(_)` arm and take drastic actions" } #[allow(missing_copy_implementations)] @@ -353,32 +355,28 @@ fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) { if inner.iter().any(|pat| pat.node == PatKind::Wild) && path_str == "Err" { // `Err(_)` arm found - let mut need_lint = true; - if let ExprBlock(ref block) = arm.body.node { - if is_unreachable_block(cx, block) { - need_lint = false; - } - } - - if need_lint { + if_let_chain! {[ + let ExprBlock(ref block) = arm.body.node, + is_panic_block(cx, block) + ], { span_note_and_lint(cx, MATCH_WILD_ERR_ARM, arm.pats[0].span, "Err(_) will match all errors, maybe not a good idea", arm.pats[0].span, "to remove this warning, match each error seperately or use unreachable macro"); - } + }} } } } } } -// If the block contains only a `unreachable!` macro (as expression or statement) -fn is_unreachable_block(cx: &LateContext, block: &Block) -> bool { +// If the block contains only a `panic!` macro (as expression or statement) +fn is_panic_block(cx: &LateContext, block: &Block) -> bool { match (&block.expr, block.stmts.len(), block.stmts.first()) { - (&Some(ref exp), 0, _) => is_expn_of(cx, exp.span, "unreachable").is_some(), - (&None, 1, Some(ref stmt)) => is_expn_of(cx, stmt.span, "unreachable").is_some(), + (&Some(ref exp), 0, _) => is_expn_of(cx, exp.span, "panic").is_some() && is_expn_of(cx, exp.span, "unreachable").is_none(), + (&None, 1, Some(ref stmt)) => is_expn_of(cx, stmt.span, "panic").is_some() && is_expn_of(cx, stmt.span, "unreachable").is_none(), _ => false } } diff --git a/tests/ui/matches.rs b/tests/ui/matches.rs index 277f96be686..46a99293a35 100644 --- a/tests/ui/matches.rs +++ b/tests/ui/matches.rs @@ -286,34 +286,44 @@ fn overlapping() { fn match_wild_err_arm() { let x: Result = Ok(3); + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => panic!("err") + } + + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => {panic!()} + } + + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => {panic!();} + } + + // allowed when not with `panic!` block match x { Ok(3) => println!("ok"), Ok(_) => println!("ok"), Err(_) => println!("err") } - match x { - Ok(3) => println!("ok"), - Ok(_) => println!("ok"), - Err(_) => { - println!("err"); - unreachable!() - } - } - - // allowed when using with unreachable as the only statement/expression - match x { - Ok(3) => println!("ok"), - Ok(_) => println!("ok"), - Err(_) => unreachable!() - } - + // allowed when used with `unreachable!` match x { Ok(3) => println!("ok"), Ok(_) => println!("ok"), Err(_) => {unreachable!()} } + match x { + Ok(3) => println!("ok"), + Ok(_) => println!("ok"), + Err(_) => unreachable!() + } + match x { Ok(3) => println!("ok"), Ok(_) => println!("ok"), diff --git a/tests/ui/matches.stderr b/tests/ui/matches.stderr index 438c2e12c29..bc8584b8587 100644 --- a/tests/ui/matches.stderr +++ b/tests/ui/matches.stderr @@ -391,7 +391,7 @@ note: overlaps with this error: Err(_) will match all errors, maybe not a good idea --> $DIR/matches.rs:292:9 | -292 | Err(_) => println!("err") +292 | Err(_) => panic!("err") | ^^^^^^ | = note: #[deny(match_wild_err_arm)] implied by #[deny(clippy)] @@ -405,11 +405,20 @@ note: lint level defined here error: Err(_) will match all errors, maybe not a good idea --> $DIR/matches.rs:298:9 | -298 | Err(_) => { +298 | Err(_) => {panic!()} | ^^^^^^ | = note: #[deny(match_wild_err_arm)] implied by #[deny(clippy)] = note: to remove this warning, match each error seperately or use unreachable macro -error: aborting due to 25 previous errors +error: Err(_) will match all errors, maybe not a good idea + --> $DIR/matches.rs:304:9 + | +304 | Err(_) => {panic!();} + | ^^^^^^ + | + = note: #[deny(match_wild_err_arm)] implied by #[deny(clippy)] + = note: to remove this warning, match each error seperately or use unreachable macro + +error: aborting due to 26 previous errors From 12c53752b80a6bb1c0cab735f5e87a765de1ef26 Mon Sep 17 00:00:00 2001 From: Bood Qian Date: Sat, 11 Feb 2017 21:47:26 +0800 Subject: [PATCH 07/13] Apply rustfmt --- clippy_lints/src/matches.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 57216ad8d41..310e9a22753 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -10,7 +10,8 @@ use std::collections::Bound; use syntax::ast::LitKind; use syntax::codemap::Span; use utils::paths; -use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block, walk_ptrs_ty, is_expn_of}; +use utils::{match_type, snippet, span_note_and_lint, span_lint_and_then, in_external_macro, expr_block, walk_ptrs_ty, + is_expn_of}; use utils::sugg::Sugg; /// **What it does:** Checks for matches with a single arm where an `if let` @@ -352,10 +353,9 @@ fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) { for arm in arms { if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pats[0].node { let path_str = print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)); - if inner.iter().any(|pat| pat.node == PatKind::Wild) && - path_str == "Err" { - // `Err(_)` arm found - if_let_chain! {[ + if inner.iter().any(|pat| pat.node == PatKind::Wild) && path_str == "Err" { + // `Err(_)` arm found + if_let_chain! {[ let ExprBlock(ref block) = arm.body.node, is_panic_block(cx, block) ], { @@ -364,7 +364,8 @@ fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) { arm.pats[0].span, "Err(_) will match all errors, maybe not a good idea", arm.pats[0].span, - "to remove this warning, match each error seperately or use unreachable macro"); + "to remove this warning, match each error seperately \ + or use unreachable macro"); }} } } @@ -375,9 +376,13 @@ fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) { // If the block contains only a `panic!` macro (as expression or statement) fn is_panic_block(cx: &LateContext, block: &Block) -> bool { match (&block.expr, block.stmts.len(), block.stmts.first()) { - (&Some(ref exp), 0, _) => is_expn_of(cx, exp.span, "panic").is_some() && is_expn_of(cx, exp.span, "unreachable").is_none(), - (&None, 1, Some(ref stmt)) => is_expn_of(cx, stmt.span, "panic").is_some() && is_expn_of(cx, stmt.span, "unreachable").is_none(), - _ => false + (&Some(ref exp), 0, _) => { + is_expn_of(cx, exp.span, "panic").is_some() && is_expn_of(cx, exp.span, "unreachable").is_none() + }, + (&None, 1, Some(ref stmt)) => { + is_expn_of(cx, stmt.span, "panic").is_some() && is_expn_of(cx, stmt.span, "unreachable").is_none() + }, + _ => false, } } From 1c381ec642556ced608517df5518bbec7f535e14 Mon Sep 17 00:00:00 2001 From: Bood Qian Date: Sat, 11 Feb 2017 22:11:19 +0800 Subject: [PATCH 08/13] Move more into if_let_chain --- clippy_lints/src/matches.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 310e9a22753..02d089e1af4 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -353,21 +353,21 @@ fn check_wild_err_arm(cx: &LateContext, ex: &Expr, arms: &[Arm]) { for arm in arms { if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pats[0].node { let path_str = print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)); - if inner.iter().any(|pat| pat.node == PatKind::Wild) && path_str == "Err" { - // `Err(_)` arm found - if_let_chain! {[ - let ExprBlock(ref block) = arm.body.node, - is_panic_block(cx, block) - ], { - span_note_and_lint(cx, - MATCH_WILD_ERR_ARM, - arm.pats[0].span, - "Err(_) will match all errors, maybe not a good idea", - arm.pats[0].span, - "to remove this warning, match each error seperately \ - or use unreachable macro"); - }} - } + if_let_chain! {[ + path_str == "Err", + inner.iter().any(|pat| pat.node == PatKind::Wild), + let ExprBlock(ref block) = arm.body.node, + is_panic_block(cx, block) + ], { + // `Err(_)` arm with `panic!` found + span_note_and_lint(cx, + MATCH_WILD_ERR_ARM, + arm.pats[0].span, + "Err(_) will match all errors, maybe not a good idea", + arm.pats[0].span, + "to remove this warning, match each error seperately \ + or use unreachable macro"); + }} } } } From 9824c997fe6ee50015e1701e94231331d815db7d Mon Sep 17 00:00:00 2001 From: Bood Qian Date: Sun, 12 Feb 2017 09:16:37 +0800 Subject: [PATCH 09/13] Remove unnecessary ref --- clippy_lints/src/matches.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 02d089e1af4..66b55c48455 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -379,7 +379,7 @@ fn is_panic_block(cx: &LateContext, block: &Block) -> bool { (&Some(ref exp), 0, _) => { is_expn_of(cx, exp.span, "panic").is_some() && is_expn_of(cx, exp.span, "unreachable").is_none() }, - (&None, 1, Some(ref stmt)) => { + (&None, 1, Some(stmt)) => { is_expn_of(cx, stmt.span, "panic").is_some() && is_expn_of(cx, stmt.span, "unreachable").is_none() }, _ => false, From 36b8554cf1553a3d1e5a86f876efdc0b847c40e5 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sun, 12 Feb 2017 13:53:30 +0100 Subject: [PATCH 10/13] add notes for immutable inputs --- clippy_lints/src/lib.rs | 1 + clippy_lints/src/ptr.rs | 40 ++++++++++++++++++++---------------- tests/ui/mut_from_ref.rs | 4 ++++ tests/ui/mut_from_ref.stderr | 34 +++++------------------------- 4 files changed, 32 insertions(+), 47 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index da8b04bdf6f..4b7b72f0c79 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -15,6 +15,7 @@ #![allow(needless_lifetimes)] extern crate syntax; +extern crate syntax_pos; #[macro_use] extern crate rustc; extern crate rustc_data_structures; diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index a30d632f159..9ea6d18a878 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -5,7 +5,9 @@ use rustc::hir::map::NodeItem; use rustc::lint::*; use rustc::ty; use syntax::ast::NodeId; -use utils::{match_path, match_type, paths, span_lint}; +use syntax::codemap::Span; +use syntax_pos::MultiSpan; +use utils::{match_path, match_type, paths, span_lint, span_lint_and_then}; /// **What it does:** This lint checks for function arguments of type `&String` or `&Vec` unless /// the references are mutable. @@ -132,29 +134,31 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) { } if let FunctionRetTy::Return(ref ty) = decl.output { - if let Some((out, MutMutable)) = get_rptr_lm(ty) { - if let Some(MutImmutable) = - decl.inputs - .iter() - .filter_map(|ty| get_rptr_lm(ty)) - .filter(|&(lt, _)| lt.name == out.name) - .fold(None, |x, (_, m)| match (x, m) { - (Some(MutMutable), _) | - (_, MutMutable) => Some(MutMutable), - (_, m) => Some(m), - }) { - span_lint(cx, - MUT_FROM_REF, - ty.span, - "this function takes an immutable ref to return a mutable one"); + if let Some((out, MutMutable, _)) = get_rptr_lm(ty) { + let mut immutables = vec![]; + for (_, ref mutbl, ref argspan) in decl.inputs + .iter() + .filter_map(|ty| get_rptr_lm(ty)) + .filter(|&(lt, _, _)| lt.name == out.name) { + if *mutbl == MutMutable { return; } + immutables.push(*argspan); } + if immutables.is_empty() { return; } + span_lint_and_then(cx, + MUT_FROM_REF, + ty.span, + "mutable borrow from immutable input(s)", + |db| { + let ms = MultiSpan::from_spans(immutables); + db.span_note(ms, "immutable borrow here"); + }); } } } -fn get_rptr_lm(ty: &Ty) -> Option<(&Lifetime, Mutability)> { +fn get_rptr_lm(ty: &Ty) -> Option<(&Lifetime, Mutability, Span)> { if let Ty_::TyRptr(ref lt, ref m) = ty.node { - Some((lt, m.mutbl)) + Some((lt, m.mutbl, ty.span)) } else { None } diff --git a/tests/ui/mut_from_ref.rs b/tests/ui/mut_from_ref.rs index 24b73eacc32..1bb6bea66f6 100644 --- a/tests/ui/mut_from_ref.rs +++ b/tests/ui/mut_from_ref.rs @@ -29,6 +29,10 @@ fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { unimplemented!() } +fn fail_double<'a>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { + unimplemented!() +} + // this is OK, because the result borrows y fn works<'a>(x: &u32, y: &'a mut u32) -> &'a mut u32 { unimplemented!() diff --git a/tests/ui/mut_from_ref.stderr b/tests/ui/mut_from_ref.stderr index 509ac83b4da..5f9cee2af0d 100644 --- a/tests/ui/mut_from_ref.stderr +++ b/tests/ui/mut_from_ref.stderr @@ -1,32 +1,8 @@ -error: this function takes an immutable ref to return a mutable one - --> $DIR/mut_from_ref.rs:9:39 - | -9 | fn this_wont_hurt_a_bit(&self) -> &mut Foo { - | ^^^^^^^^ - | -note: lint level defined here - --> $DIR/mut_from_ref.rs:4:9 - | -4 | #![deny(mut_from_ref)] - | ^^^^^^^^^^^^ - -error: this function takes an immutable ref to return a mutable one - --> $DIR/mut_from_ref.rs:15:25 +error[E0261]: use of undeclared lifetime name `'b` + --> $DIR/mut_from_ref.rs:32:48 | -15 | fn ouch(x: &Foo) -> &mut Foo; - | ^^^^^^^^ +32 | fn fail_double<'a>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { + | ^^ undeclared lifetime -error: this function takes an immutable ref to return a mutable one - --> $DIR/mut_from_ref.rs:24:21 - | -24 | fn fail(x: &u32) -> &mut u16 { - | ^^^^^^^^ - -error: this function takes an immutable ref to return a mutable one - --> $DIR/mut_from_ref.rs:28:50 - | -28 | fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { - | ^^^^^^^^^^^ - -error: aborting due to 4 previous errors +error: aborting due to previous error From 2a0bfdcd7225f49b67c481cbaa97316b964c2f3e Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sun, 12 Feb 2017 14:11:18 +0100 Subject: [PATCH 11/13] rustfmt --- clippy_lints/src/ptr.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 9ea6d18a878..e9176372ebc 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -136,22 +136,23 @@ fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) { if let FunctionRetTy::Return(ref ty) = decl.output { if let Some((out, MutMutable, _)) = get_rptr_lm(ty) { let mut immutables = vec![]; - for (_, ref mutbl, ref argspan) in decl.inputs - .iter() - .filter_map(|ty| get_rptr_lm(ty)) - .filter(|&(lt, _, _)| lt.name == out.name) { - if *mutbl == MutMutable { return; } + for (_, ref mutbl, ref argspan) in + decl.inputs + .iter() + .filter_map(|ty| get_rptr_lm(ty)) + .filter(|&(lt, _, _)| lt.name == out.name) { + if *mutbl == MutMutable { + return; + } immutables.push(*argspan); } - if immutables.is_empty() { return; } - span_lint_and_then(cx, - MUT_FROM_REF, - ty.span, - "mutable borrow from immutable input(s)", - |db| { - let ms = MultiSpan::from_spans(immutables); - db.span_note(ms, "immutable borrow here"); - }); + if immutables.is_empty() { + return; + } + span_lint_and_then(cx, MUT_FROM_REF, ty.span, "mutable borrow from immutable input(s)", |db| { + let ms = MultiSpan::from_spans(immutables); + db.span_note(ms, "immutable borrow here"); + }); } } } From 21d226e7d22138cea44e35e58757bde5ed9df2f7 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sun, 12 Feb 2017 15:10:25 +0100 Subject: [PATCH 12/13] fixed multi-span test --- tests/ui/mut_from_ref.rs | 2 +- tests/ui/mut_from_ref.stderr | 69 +++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/tests/ui/mut_from_ref.rs b/tests/ui/mut_from_ref.rs index 1bb6bea66f6..35bff9371d9 100644 --- a/tests/ui/mut_from_ref.rs +++ b/tests/ui/mut_from_ref.rs @@ -29,7 +29,7 @@ fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { unimplemented!() } -fn fail_double<'a>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { +fn fail_double<'a, 'b>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { unimplemented!() } diff --git a/tests/ui/mut_from_ref.stderr b/tests/ui/mut_from_ref.stderr index 5f9cee2af0d..5098d7d0ab5 100644 --- a/tests/ui/mut_from_ref.stderr +++ b/tests/ui/mut_from_ref.stderr @@ -1,8 +1,67 @@ -error[E0261]: use of undeclared lifetime name `'b` - --> $DIR/mut_from_ref.rs:32:48 +error: mutable borrow from immutable input(s) + --> $DIR/mut_from_ref.rs:9:39 + | +9 | fn this_wont_hurt_a_bit(&self) -> &mut Foo { + | ^^^^^^^^ + | +note: lint level defined here + --> $DIR/mut_from_ref.rs:4:9 + | +4 | #![deny(mut_from_ref)] + | ^^^^^^^^^^^^ +note: immutable borrow here + --> $DIR/mut_from_ref.rs:9:29 + | +9 | fn this_wont_hurt_a_bit(&self) -> &mut Foo { + | ^^^^^ + +error: mutable borrow from immutable input(s) + --> $DIR/mut_from_ref.rs:15:25 | -32 | fn fail_double<'a>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { - | ^^ undeclared lifetime +15 | fn ouch(x: &Foo) -> &mut Foo; + | ^^^^^^^^ + | +note: immutable borrow here + --> $DIR/mut_from_ref.rs:15:16 + | +15 | fn ouch(x: &Foo) -> &mut Foo; + | ^^^^ -error: aborting due to previous error +error: mutable borrow from immutable input(s) + --> $DIR/mut_from_ref.rs:24:21 + | +24 | fn fail(x: &u32) -> &mut u16 { + | ^^^^^^^^ + | +note: immutable borrow here + --> $DIR/mut_from_ref.rs:24:12 + | +24 | fn fail(x: &u32) -> &mut u16 { + | ^^^^ + +error: mutable borrow from immutable input(s) + --> $DIR/mut_from_ref.rs:28:50 + | +28 | fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { + | ^^^^^^^^^^^ + | +note: immutable borrow here + --> $DIR/mut_from_ref.rs:28:25 + | +28 | fn fail_lifetime<'a>(x: &'a u32, y: &mut u32) -> &'a mut u32 { + | ^^^^^^^ + +error: mutable borrow from immutable input(s) + --> $DIR/mut_from_ref.rs:32:67 + | +32 | fn fail_double<'a, 'b>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { + | ^^^^^^^^^^^ + | +note: immutable borrow here + --> $DIR/mut_from_ref.rs:32:27 + | +32 | fn fail_double<'a, 'b>(x: &'a u32, y: &'a u32, z: &'b mut u32) -> &'a mut u32 { + | ^^^^^^^ ^^^^^^^ + +error: aborting due to 5 previous errors From 1177f3915c7c487ef968552e7cbe4c7bc2e5228a Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 13 Feb 2017 11:15:36 +0100 Subject: [PATCH 13/13] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c87733f9dfa..3660937262a 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ transparently: ## Lints -There are 187 lints included in this crate: +There are 188 lints included in this crate: name | default | triggers on -----------------------------------------------------------------------------------------------------------------------|---------|---------------------------------------------------------------------------------------------------------------------------------- @@ -271,7 +271,7 @@ name [match_overlapping_arm](https://github.com/Manishearth/rust-clippy/wiki#match_overlapping_arm) | warn | a match with overlapping arms [match_ref_pats](https://github.com/Manishearth/rust-clippy/wiki#match_ref_pats) | warn | a match or `if let` with all arms prefixed with `&` instead of deref-ing the match expression [match_same_arms](https://github.com/Manishearth/rust-clippy/wiki#match_same_arms) | warn | `match` with identical arm bodies -[match_wild_err_arm](https://github.com/Manishearth/rust-clippy/wiki#match_wild_err_arm) | warn | a match with `Err(_)` arm +[match_wild_err_arm](https://github.com/Manishearth/rust-clippy/wiki#match_wild_err_arm) | warn | a match with `Err(_)` arm and take drastic actions [mem_forget](https://github.com/Manishearth/rust-clippy/wiki#mem_forget) | allow | `mem::forget` usage on `Drop` types, likely to cause memory leaks [min_max](https://github.com/Manishearth/rust-clippy/wiki#min_max) | warn | `min(_, max(_, _))` (or vice versa) with bounds clamping the result to a constant [misrefactored_assign_op](https://github.com/Manishearth/rust-clippy/wiki#misrefactored_assign_op) | warn | having a variable on both sides of an assign op