diff --git a/clippy_lints/src/methods.rs b/clippy_lints/src/methods.rs index c3198d1dd78..30ffcc9f6e3 100644 --- a/clippy_lints/src/methods.rs +++ b/clippy_lints/src/methods.rs @@ -949,6 +949,7 @@ fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: ty::Ty) -> Option bool { match ty.sty { ty::TySlice(_) => true, + ty::TyAdt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()), ty::TyAdt(..) => match_type(cx, ty, &paths::VEC), ty::TyArray(_, size) => size < 32, ty::TyRef(_, ty::TypeAndMut { ty: inner, .. }) => may_slice(cx, inner), @@ -965,6 +966,7 @@ fn derefs_to_slice(cx: &LateContext, expr: &hir::Expr, ty: ty::Ty) -> Option sugg::Sugg::hir_opt(cx, expr), + ty::TyAdt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => sugg::Sugg::hir_opt(cx, expr), ty::TyRef(_, ty::TypeAndMut { ty: inner, .. }) => { if may_slice(cx, inner) { sugg::Sugg::hir_opt(cx, expr) diff --git a/tests/compile-fail/methods.rs b/tests/compile-fail/methods.rs index cd04a3b6b97..43a4386886d 100644 --- a/tests/compile-fail/methods.rs +++ b/tests/compile-fail/methods.rs @@ -350,6 +350,7 @@ fn or_fun_call() { /// Checks implementation of `ITER_NTH` lint fn iter_nth() { let mut some_vec = vec![0, 1, 2, 3]; + let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); let mut some_vec_deque: VecDeque<_> = some_vec.iter().cloned().collect(); { @@ -358,6 +359,8 @@ fn iter_nth() { //~^ERROR called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable let bad_slice = &some_vec[..].iter().nth(3); //~^ERROR called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable + let bad_boxed_slice = boxed_slice.iter().nth(3); + //~^ERROR called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable let bad_vec_deque = some_vec_deque.iter().nth(3); //~^ERROR called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable } @@ -414,6 +417,7 @@ impl GetFalsePositive { /// Checks implementation of `GET_UNWRAP` lint fn get_unwrap() { + let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); let mut some_slice = &mut [0, 1, 2, 3]; let mut some_vec = vec![0, 1, 2, 3]; let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect(); @@ -422,6 +426,10 @@ fn get_unwrap() { let mut false_positive = GetFalsePositive { arr: [0, 1, 2] }; { // Test `get().unwrap()` + let _ = boxed_slice.get(1).unwrap(); + //~^ERROR called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise + //~|HELP try this + //~|SUGGESTION boxed_slice[1] let _ = some_slice.get(0).unwrap(); //~^ERROR called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise //~|HELP try this @@ -447,6 +455,10 @@ fn get_unwrap() { } { // Test `get_mut().unwrap()` + *boxed_slice.get_mut(0).unwrap() = 1; + //~^ERROR called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise + //~|HELP try this + //~|SUGGESTION &mut boxed_slice[0] *some_slice.get_mut(0).unwrap() = 1; //~^ERROR called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise //~|HELP try this