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 b72387f0d718..671450a120d3 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 3f3ed135e0ce..17282a7fc36a 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 1099738d7995..da8b04bdf6f9 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 590e3d587d4e..7a7631d97693 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 000000000000..09cc8a6061cf --- /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 000000000000..23fc20d9a4ba --- /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 7a7631d97693..a30d632f1599 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 09cc8a6061cf..24b73eacc32c 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 23fc20d9a4ba..799adb007146 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 799adb007146..509ac83b4dab 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 7a744356eb4f..ac90528c6063 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 7e4b633417fe4886d35c11d38b28b3f98ff4e373 Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Sat, 4 Feb 2017 17:42:35 -0700 Subject: [PATCH 05/13] Add some writes to a log file for debugging --- clippy_lints/src/large_enum_variant.rs | 8 ++++++++ log | 1 + 2 files changed, 9 insertions(+) create mode 100644 log diff --git a/clippy_lints/src/large_enum_variant.rs b/clippy_lints/src/large_enum_variant.rs index 36c5e7a288ae..aa7db1f77e2c 100644 --- a/clippy_lints/src/large_enum_variant.rs +++ b/clippy_lints/src/large_enum_variant.rs @@ -67,7 +67,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant { } }) .sum(); + + use std::io::Write; + let mut f = ::std::fs::File::create("log").unwrap(); + + writeln!(f, "size, max size: {}, {}", size, self.maximum_variant_size_allowed).unwrap(); if size > self.maximum_variant_size_allowed { + writeln!(f, "size > max").unwrap(); + // panic!("foo"); + span_lint_and_then(cx, LARGE_ENUM_VARIANT, def.variants[i].span, diff --git a/log b/log new file mode 100644 index 000000000000..0cbafc978894 --- /dev/null +++ b/log @@ -0,0 +1 @@ +size, max size: 0, 200 From 1938904fcd581e87c2be63d537fd402805627a0e Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Sat, 4 Feb 2017 20:12:55 -0700 Subject: [PATCH 06/13] Change large_enum_variant to lint against size differences rather than size --- clippy_lints/src/large_enum_variant.rs | 90 +++++++++++++++----------- log | 1 - tests/ui/large_enum_variant.rs | 55 ++++++++++------ 3 files changed, 88 insertions(+), 58 deletions(-) delete mode 100644 log diff --git a/clippy_lints/src/large_enum_variant.rs b/clippy_lints/src/large_enum_variant.rs index aa7db1f77e2c..1363183cb810 100644 --- a/clippy_lints/src/large_enum_variant.rs +++ b/clippy_lints/src/large_enum_variant.rs @@ -1,4 +1,4 @@ -//! lint when there are large variants on an enum +//! lint when there is a large size difference between variants on an enum use rustc::lint::*; use rustc::hir::*; @@ -7,7 +7,7 @@ use rustc::ty::layout::TargetDataLayout; use rustc::ty::TypeFoldable; use rustc::traits::Reveal; -/// **What it does:** Checks for large variants on `enum`s. +/// **What it does:** Checks for large size differences between variants on `enum`s. /// /// **Why is this bad?** Enum size is bounded by the largest variant. Having a large variant /// can penalize the memory layout of that enum. @@ -24,17 +24,17 @@ use rustc::traits::Reveal; declare_lint! { pub LARGE_ENUM_VARIANT, Warn, - "large variants on an enum" + "large size difference between variants on an enum" } #[derive(Copy,Clone)] pub struct LargeEnumVariant { - maximum_variant_size_allowed: u64, + maximum_size_difference_allowed: u64, } impl LargeEnumVariant { - pub fn new(maximum_variant_size_allowed: u64) -> Self { - LargeEnumVariant { maximum_variant_size_allowed: maximum_variant_size_allowed } + pub fn new(maximum_size_difference_allowed: u64) -> Self { + LargeEnumVariant { maximum_size_difference_allowed: maximum_size_difference_allowed } } } @@ -50,7 +50,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant { if let ItemEnum(ref def, _) = item.node { let ty = cx.tcx.item_type(did); let adt = ty.ty_adt_def().expect("already checked whether this is an enum"); - for (i, variant) in adt.variants.iter().enumerate() { + + let mut sizes = Vec::new(); + let mut variants = Vec::new(); + + for variant in &adt.variants { let data_layout = TargetDataLayout::parse(cx.sess()); cx.tcx.infer_ctxt((), Reveal::All).enter(|infcx| { let size: u64 = variant.fields @@ -68,39 +72,49 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant { }) .sum(); - use std::io::Write; - let mut f = ::std::fs::File::create("log").unwrap(); - - writeln!(f, "size, max size: {}, {}", size, self.maximum_variant_size_allowed).unwrap(); - if size > self.maximum_variant_size_allowed { - writeln!(f, "size > max").unwrap(); - // panic!("foo"); - - span_lint_and_then(cx, - LARGE_ENUM_VARIANT, - def.variants[i].span, - "large enum variant found", - |db| { - if variant.fields.len() == 1 { - let span = match def.variants[i].node.data { - VariantData::Struct(ref fields, _) | - VariantData::Tuple(ref fields, _) => fields[0].ty.span, - VariantData::Unit(_) => unreachable!(), - }; - if let Some(snip) = snippet_opt(cx, span) { - db.span_suggestion(span, - "consider boxing the large fields to reduce the total size of \ - the enum", - format!("Box<{}>", snip)); - return; - } - } - db.span_help(def.variants[i].span, - "consider boxing the large fields to reduce the total size of the enum"); - }); - } + sizes.push(size); + variants.push(variant); }); } + + let mut grouped = sizes.into_iter().zip(variants.into_iter().enumerate()).collect::>(); + + grouped.sort_by_key(|g| g.0); + + let smallest_variant = grouped.first(); + let largest_variant = grouped.last(); + + if let (Some(smallest), Some(largest)) = (smallest_variant, largest_variant) { + let difference = largest.0 - smallest.0; + + if difference > self.maximum_size_difference_allowed { + let (i, variant) = largest.1; + + span_lint_and_then(cx, + LARGE_ENUM_VARIANT, + def.variants[i].span, + "large size difference between variants", + |db| { + if variant.fields.len() == 1 { + let span = match def.variants[i].node.data { + VariantData::Struct(ref fields, _) | + VariantData::Tuple(ref fields, _) => fields[0].ty.span, + VariantData::Unit(_) => unreachable!(), + }; + if let Some(snip) = snippet_opt(cx, span) { + db.span_suggestion(span, + "consider boxing the large fields to reduce the total size of the \ + enum", + format!("Box<{}>", snip)); + return; + } + } + db.span_help(def.variants[i].span, + "consider boxing the large fields to reduce the total size of the enum"); + }); + } + } + } } } diff --git a/log b/log deleted file mode 100644 index 0cbafc978894..000000000000 --- a/log +++ /dev/null @@ -1 +0,0 @@ -size, max size: 0, 200 diff --git a/tests/ui/large_enum_variant.rs b/tests/ui/large_enum_variant.rs index 5bbcb93910b9..26ba883b1baa 100644 --- a/tests/ui/large_enum_variant.rs +++ b/tests/ui/large_enum_variant.rs @@ -7,19 +7,21 @@ enum LargeEnum { A(i32), - B([i32; 8000]), - - + B([i32; 8000]), //~ ERROR large size difference between variants + //~^ HELP consider boxing the large fields to reduce the total size of the enum + //~| SUGGESTION Box<[i32; 8000]> } -enum GenericEnum { +enum GenericEnumOk { + A(i32), + B([T; 8000]), +} + +enum GenericEnum2 { A(i32), B([i32; 8000]), - - - C([T; 8000]), - D(T, [i32; 8000]), - + C(T, [i32; 8000]), //~ ERROR large size difference between variants + //~^ HELP consider boxing the large fields to reduce the total size of the enum } trait SomeTrait { @@ -30,24 +32,39 @@ enum LargeEnumGeneric { Var(A::Item), // regression test, this used to ICE } -enum AnotherLargeEnum { +enum LargeEnum2 { VariantOk(i32, u32), - ContainingLargeEnum(LargeEnum), - - - ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), - + ContainingLargeEnum(LargeEnum), //~ ERROR large size difference between variants + //~^ HELP consider boxing the large fields to reduce the total size of the enum + //~| SUGGESTION Box +} +enum LargeEnum3 { + ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), //~ ERROR large size difference between variants + //~^ HELP consider boxing the large fields to reduce the total size of the enum VoidVariant, StructLikeLittle { x: i32, y: i32 }, - StructLikeLarge { x: [i32; 8000], y: i32 }, +} - StructLikeLarge2 { +enum LargeEnum4 { + VariantOk(i32, u32), + StructLikeLarge { x: [i32; 8000], y: i32 }, //~ ERROR large size difference between variants + //~^ HELP consider boxing the large fields to reduce the total size of the enum +} + +enum LargeEnum5 { + VariantOk(i32, u32), + StructLikeLarge2 { //~ ERROR large size difference between variants x: - [i32; 8000] - + [i32; 8000] //~ SUGGESTION Box<[i32; 8000]> + //~^ HELP consider boxing the large fields to reduce the total size of the enum }, } +enum LargeEnumOk { + LargeA([i32; 8000]), + LargeB([i32; 8001]), +} + fn main() { } From 96ae7da9b60e05bf16c376be73facd444f99446e Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Sat, 4 Feb 2017 20:26:08 -0700 Subject: [PATCH 07/13] Run update_lints.py --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3f3ed135e0ce..1f6fb4ff9c0b 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,7 @@ name [iter_next_loop](https://github.com/Manishearth/rust-clippy/wiki#iter_next_loop) | warn | for-looping over `_.next()` which is probably not intended [iter_nth](https://github.com/Manishearth/rust-clippy/wiki#iter_nth) | warn | using `.iter().nth()` on a standard library type with O(1) element access [iter_skip_next](https://github.com/Manishearth/rust-clippy/wiki#iter_skip_next) | warn | using `.skip(x).next()` on an iterator -[large_enum_variant](https://github.com/Manishearth/rust-clippy/wiki#large_enum_variant) | warn | large variants on an enum +[large_enum_variant](https://github.com/Manishearth/rust-clippy/wiki#large_enum_variant) | warn | large size difference between variants on an enum [len_without_is_empty](https://github.com/Manishearth/rust-clippy/wiki#len_without_is_empty) | warn | traits or impls with a public `len` method but no corresponding `is_empty` method [len_zero](https://github.com/Manishearth/rust-clippy/wiki#len_zero) | warn | checking `.len() == 0` or `.len() > 0` (or similar) when `.is_empty()` could be used instead [let_and_return](https://github.com/Manishearth/rust-clippy/wiki#let_and_return) | warn | creating a let-binding and then immediately returning it like `let x = expr; x` at the end of a block From 45a7012fd969ad97fcbcdd4af2530875e37663b7 Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Mon, 6 Feb 2017 12:25:38 -0700 Subject: [PATCH 08/13] Search directly for the largest and smallest variants instead of sorting --- clippy_lints/src/large_enum_variant.rs | 29 +++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/large_enum_variant.rs b/clippy_lints/src/large_enum_variant.rs index 1363183cb810..050362dede04 100644 --- a/clippy_lints/src/large_enum_variant.rs +++ b/clippy_lints/src/large_enum_variant.rs @@ -51,10 +51,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant { let ty = cx.tcx.item_type(did); let adt = ty.ty_adt_def().expect("already checked whether this is an enum"); - let mut sizes = Vec::new(); - let mut variants = Vec::new(); + let mut smallest_variant: Option<(_, _)> = None; + let mut largest_variant: Option<(_, _)> = None; - for variant in &adt.variants { + for (i, variant) in adt.variants.iter().enumerate() { let data_layout = TargetDataLayout::parse(cx.sess()); cx.tcx.infer_ctxt((), Reveal::All).enter(|infcx| { let size: u64 = variant.fields @@ -72,18 +72,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant { }) .sum(); - sizes.push(size); - variants.push(variant); + let grouped = (size, (i, variant)); + + update_if(&mut smallest_variant, grouped, |a, b| b.0 <= a.0); + update_if(&mut largest_variant, grouped, |a, b| b.0 >= a.0); }); } - let mut grouped = sizes.into_iter().zip(variants.into_iter().enumerate()).collect::>(); - - grouped.sort_by_key(|g| g.0); - - let smallest_variant = grouped.first(); - let largest_variant = grouped.last(); - if let (Some(smallest), Some(largest)) = (smallest_variant, largest_variant) { let difference = largest.0 - smallest.0; @@ -118,3 +113,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant { } } } + +fn update_if(old: &mut Option, new: T, f: F) where F: Fn(&T, &T) -> bool { + if let Some(ref mut val) = *old { + if f(val, &new) { + *val = new; + } + } else { + *old = Some(new); + } +} From 2a8ce7c4588d3847ffe91fd90fcd8d0be2edb4df Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Fri, 10 Feb 2017 21:04:19 -0700 Subject: [PATCH 09/13] Update large_enum_variant test --- tests/ui/large_enum_variant.rs | 29 ++++-------- tests/ui/large_enum_variant.stderr | 72 ++++++++++++------------------ 2 files changed, 37 insertions(+), 64 deletions(-) diff --git a/tests/ui/large_enum_variant.rs b/tests/ui/large_enum_variant.rs index 26ba883b1baa..8ac7571c1b1f 100644 --- a/tests/ui/large_enum_variant.rs +++ b/tests/ui/large_enum_variant.rs @@ -7,9 +7,7 @@ enum LargeEnum { A(i32), - B([i32; 8000]), //~ ERROR large size difference between variants - //~^ HELP consider boxing the large fields to reduce the total size of the enum - //~| SUGGESTION Box<[i32; 8000]> + B([i32; 8000]), } enum GenericEnumOk { @@ -20,8 +18,7 @@ enum GenericEnumOk { enum GenericEnum2 { A(i32), B([i32; 8000]), - C(T, [i32; 8000]), //~ ERROR large size difference between variants - //~^ HELP consider boxing the large fields to reduce the total size of the enum + C(T, [i32; 8000]), } trait SomeTrait { @@ -29,35 +26,27 @@ trait SomeTrait { } enum LargeEnumGeneric { - Var(A::Item), // regression test, this used to ICE + Var(A::Item), } enum LargeEnum2 { VariantOk(i32, u32), - ContainingLargeEnum(LargeEnum), //~ ERROR large size difference between variants - //~^ HELP consider boxing the large fields to reduce the total size of the enum - //~| SUGGESTION Box + ContainingLargeEnum(LargeEnum), } enum LargeEnum3 { - ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), //~ ERROR large size difference between variants - //~^ HELP consider boxing the large fields to reduce the total size of the enum + ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), VoidVariant, StructLikeLittle { x: i32, y: i32 }, } enum LargeEnum4 { VariantOk(i32, u32), - StructLikeLarge { x: [i32; 8000], y: i32 }, //~ ERROR large size difference between variants - //~^ HELP consider boxing the large fields to reduce the total size of the enum + StructLikeLarge { x: [i32; 8000], y: i32 }, } enum LargeEnum5 { VariantOk(i32, u32), - StructLikeLarge2 { //~ ERROR large size difference between variants - x: - [i32; 8000] //~ SUGGESTION Box<[i32; 8000]> - //~^ HELP consider boxing the large fields to reduce the total size of the enum - }, + StructLikeLarge2 { x: [i32; 8000] }, } enum LargeEnumOk { @@ -65,6 +54,4 @@ enum LargeEnumOk { LargeB([i32; 8001]), } -fn main() { - -} +fn main() {} diff --git a/tests/ui/large_enum_variant.stderr b/tests/ui/large_enum_variant.stderr index 77155b6ab5fe..84213003eb70 100644 --- a/tests/ui/large_enum_variant.stderr +++ b/tests/ui/large_enum_variant.stderr @@ -1,4 +1,4 @@ -error: large enum variant found +error: large size difference between variants --> $DIR/large_enum_variant.rs:10:5 | 10 | B([i32; 8000]), @@ -12,73 +12,59 @@ note: lint level defined here help: consider boxing the large fields to reduce the total size of the enum | B(Box<[i32; 8000]>), -error: large enum variant found - --> $DIR/large_enum_variant.rs:17:5 - | -17 | B([i32; 8000]), - | ^^^^^^^^^^^^^^ - | -help: consider boxing the large fields to reduce the total size of the enum - | B(Box<[i32; 8000]>), - -error: large enum variant found +error: large size difference between variants --> $DIR/large_enum_variant.rs:21:5 | -21 | D(T, [i32; 8000]), +21 | C(T, [i32; 8000]), | ^^^^^^^^^^^^^^^^^ | help: consider boxing the large fields to reduce the total size of the enum --> $DIR/large_enum_variant.rs:21:5 | -21 | D(T, [i32; 8000]), +21 | C(T, [i32; 8000]), | ^^^^^^^^^^^^^^^^^ -error: large enum variant found - --> $DIR/large_enum_variant.rs:35:5 +error: large size difference between variants + --> $DIR/large_enum_variant.rs:34:5 | -35 | ContainingLargeEnum(LargeEnum), +34 | ContainingLargeEnum(LargeEnum), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: consider boxing the large fields to reduce the total size of the enum | ContainingLargeEnum(Box), -error: large enum variant found - --> $DIR/large_enum_variant.rs:38:5 +error: large size difference between variants + --> $DIR/large_enum_variant.rs:37:5 | -38 | ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), +37 | ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: consider boxing the large fields to reduce the total size of the enum - --> $DIR/large_enum_variant.rs:38:5 + --> $DIR/large_enum_variant.rs:37:5 | -38 | ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), +37 | ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: large enum variant found - --> $DIR/large_enum_variant.rs:42:5 - | -42 | StructLikeLarge { x: [i32; 8000], y: i32 }, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: consider boxing the large fields to reduce the total size of the enum - --> $DIR/large_enum_variant.rs:42:5 - | -42 | StructLikeLarge { x: [i32; 8000], y: i32 }, - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: large enum variant found +error: large size difference between variants --> $DIR/large_enum_variant.rs:44:5 | -44 | StructLikeLarge2 { - | _____^ starting here... -45 | | x: -46 | | [i32; 8000] -47 | | -48 | | }, - | |_____^ ...ending here +44 | StructLikeLarge { x: [i32; 8000], y: i32 }, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | help: consider boxing the large fields to reduce the total size of the enum - | Box<[i32; 8000]> + --> $DIR/large_enum_variant.rs:44:5 + | +44 | StructLikeLarge { x: [i32; 8000], y: i32 }, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 7 previous errors +error: large size difference between variants + --> $DIR/large_enum_variant.rs:49:5 + | +49 | StructLikeLarge2 { x: [i32; 8000] }, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: consider boxing the large fields to reduce the total size of the enum + | StructLikeLarge2 { x: Box<[i32; 8000]> }, + +error: aborting due to 6 previous errors From 8fb582ea1c4aece4c0f6634c562c4a46de481dc6 Mon Sep 17 00:00:00 2001 From: Owen Sanchez Date: Fri, 10 Feb 2017 21:08:50 -0700 Subject: [PATCH 10/13] rustfmt --- clippy_lints/src/large_enum_variant.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/large_enum_variant.rs b/clippy_lints/src/large_enum_variant.rs index 050362dede04..f656d513b516 100644 --- a/clippy_lints/src/large_enum_variant.rs +++ b/clippy_lints/src/large_enum_variant.rs @@ -114,7 +114,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant { } } -fn update_if(old: &mut Option, new: T, f: F) where F: Fn(&T, &T) -> bool { +fn update_if(old: &mut Option, new: T, f: F) + where F: Fn(&T, &T) -> bool +{ if let Some(ref mut val) = *old { if f(val, &new) { *val = new; From 36b8554cf1553a3d1e5a86f876efdc0b847c40e5 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sun, 12 Feb 2017 13:53:30 +0100 Subject: [PATCH 11/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 da8b04bdf6f9..4b7b72f0c797 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 a30d632f1599..9ea6d18a8781 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 24b73eacc32c..1bb6bea66f61 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 509ac83b4dab..5f9cee2af0d4 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 12/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 9ea6d18a8781..e9176372ebce 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 13/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 1bb6bea66f61..35bff9371d9e 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 5f9cee2af0d4..5098d7d0ab56 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