Skip to content

Commit bd637f2

Browse files
committed
Split off new lint slice_as_deref from redundant_slicing
1 parent 11f5850 commit bd637f2

10 files changed

+108
-33
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3260,6 +3260,7 @@ Released 2018-09-13
32603260
[`single_match_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else
32613261
[`size_of_in_element_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#size_of_in_element_count
32623262
[`skip_while_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next
3263+
[`slice_as_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#slice_as_deref
32633264
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
32643265
[`stable_sort_primitive`]: https://rust-lang.github.io/rust-clippy/master/index.html#stable_sort_primitive
32653266
[`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string

clippy_lints/src/lib.register_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ store.register_lints(&[
417417
redundant_field_names::REDUNDANT_FIELD_NAMES,
418418
redundant_pub_crate::REDUNDANT_PUB_CRATE,
419419
redundant_slicing::REDUNDANT_SLICING,
420+
redundant_slicing::SLICE_AS_DEREF,
420421
redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
421422
ref_option_ref::REF_OPTION_REF,
422423
reference::DEREF_ADDROF,

clippy_lints/src/lib.register_restriction.rs

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
5050
LintId::of(panic_unimplemented::UNIMPLEMENTED),
5151
LintId::of(panic_unimplemented::UNREACHABLE),
5252
LintId::of(pattern_type_mismatch::PATTERN_TYPE_MISMATCH),
53+
LintId::of(redundant_slicing::SLICE_AS_DEREF),
5354
LintId::of(same_name_method::SAME_NAME_METHOD),
5455
LintId::of(shadow::SHADOW_REUSE),
5556
LintId::of(shadow::SHADOW_SAME),

clippy_lints/src/redundant_slicing.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,31 @@ declare_clippy_lint! {
4242
"redundant slicing of the whole range of a type"
4343
}
4444

45-
declare_lint_pass!(RedundantSlicing => [REDUNDANT_SLICING]);
45+
declare_clippy_lint! {
46+
/// ### What it does
47+
/// Checks for slicing expression which are equivalent to dereferencing the
48+
/// value.
49+
///
50+
/// ### Why is this bad?
51+
/// Some people may prefer to dereference rather than slice.
52+
///
53+
/// ### Example
54+
/// ```rust
55+
/// let vec = vec![1, 2, 3];
56+
/// let slice = &v[..];
57+
/// ```
58+
/// Use instead:
59+
/// ```rust
60+
/// let vec = vec![1, 2, 3];
61+
/// let slice = &*v;
62+
/// ```
63+
#[clippy::version = "1.59.0"]
64+
pub SLICE_AS_DEREF,
65+
restriction,
66+
"slicing instead of dereferencing"
67+
}
68+
69+
declare_lint_pass!(RedundantSlicing => [REDUNDANT_SLICING, SLICE_AS_DEREF]);
4670

4771
impl LateLintPass<'_> for RedundantSlicing {
4872
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
@@ -65,7 +89,7 @@ impl LateLintPass<'_> for RedundantSlicing {
6589
});
6690
let mut app = Applicability::MachineApplicable;
6791

68-
let (help, sugg) = if TyS::same_type(expr_ty, indexed_ty) {
92+
let (lint, msg, help, sugg) = if TyS::same_type(expr_ty, indexed_ty) {
6993
if expr_ref_count > indexed_ref_count {
7094
// Indexing takes self by reference and can't return a reference to that
7195
// reference as it's a local variable. The only way this could happen is if
@@ -103,7 +127,7 @@ impl LateLintPass<'_> for RedundantSlicing {
103127
format!("{}{}{}", reborrow_str, "*".repeat(deref_count), snip)
104128
};
105129

106-
(help_str, sugg)
130+
(REDUNDANT_SLICING, "redundant slicing of the whole range", help_str, sugg)
107131
} else if let Some(target_id) = cx.tcx.lang_items().deref_target() {
108132
if let Ok(deref_ty) = cx.tcx.try_normalize_erasing_regions(
109133
cx.param_env,
@@ -116,7 +140,7 @@ impl LateLintPass<'_> for RedundantSlicing {
116140
} else {
117141
format!("&{}{}*{}", mutability.prefix_str(), "*".repeat(indexed_ref_count), snip)
118142
};
119-
("dereference the original value instead", sugg)
143+
(SLICE_AS_DEREF, "slicing when dereferencing would work", "dereference the original value instead", sugg)
120144
} else {
121145
return;
122146
}
@@ -129,9 +153,9 @@ impl LateLintPass<'_> for RedundantSlicing {
129153

130154
span_lint_and_sugg(
131155
cx,
132-
REDUNDANT_SLICING,
156+
lint,
133157
expr.span,
134-
"redundant slicing of the whole range",
158+
msg,
135159
help,
136160
sugg,
137161
app,

tests/ui/redundant_slicing.fixed

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22

3-
#![allow(unused)]
3+
#![allow(unused, clippy::slice_as_deref)]
44
#![warn(clippy::redundant_slicing)]
55

66
use std::io::Read;
@@ -10,18 +10,18 @@ fn main() {
1010
let _ = slice; // Redundant slice
1111

1212
let v = vec![0];
13-
let _ = &*v; // Deref instead of slice
13+
let _ = &v[..]; // Ok, results in `&[_]`
1414
let _ = (&*v); // Outer borrow is redundant
1515

1616
static S: &[u8] = &[0, 1, 2];
1717
let err = &mut &*S; // Should reborrow instead of slice
1818

1919
let mut vec = vec![0];
20-
let mut_slice = &mut *vec; // Deref instead of slice
20+
let mut_slice = &mut vec[..]; // Ok, results in `&mut [_]`
2121
let _ = &mut *mut_slice; // Should reborrow instead of slice
2222

2323
let ref_vec = &vec;
24-
let _ = &**ref_vec; // Deref instead of slice
24+
let _ = &ref_vec[..]; // Ok, results in `&[_]`
2525

2626
macro_rules! m {
2727
($e:expr) => {

tests/ui/redundant_slicing.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// run-rustfix
22

3-
#![allow(unused)]
3+
#![allow(unused, clippy::slice_as_deref)]
44
#![warn(clippy::redundant_slicing)]
55

66
use std::io::Read;
@@ -10,18 +10,18 @@ fn main() {
1010
let _ = &slice[..]; // Redundant slice
1111

1212
let v = vec![0];
13-
let _ = &v[..]; // Deref instead of slice
13+
let _ = &v[..]; // Ok, results in `&[_]`
1414
let _ = &(&*v)[..]; // Outer borrow is redundant
1515

1616
static S: &[u8] = &[0, 1, 2];
1717
let err = &mut &S[..]; // Should reborrow instead of slice
1818

1919
let mut vec = vec![0];
20-
let mut_slice = &mut vec[..]; // Deref instead of slice
20+
let mut_slice = &mut vec[..]; // Ok, results in `&mut [_]`
2121
let _ = &mut mut_slice[..]; // Should reborrow instead of slice
2222

2323
let ref_vec = &vec;
24-
let _ = &ref_vec[..]; // Deref instead of slice
24+
let _ = &ref_vec[..]; // Ok, results in `&[_]`
2525

2626
macro_rules! m {
2727
($e:expr) => {

tests/ui/redundant_slicing.stderr

+1-19
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ LL | let _ = &slice[..]; // Redundant slice
66
|
77
= note: `-D clippy::redundant-slicing` implied by `-D warnings`
88

9-
error: redundant slicing of the whole range
10-
--> $DIR/redundant_slicing.rs:13:13
11-
|
12-
LL | let _ = &v[..]; // Deref instead of slice
13-
| ^^^^^^ help: dereference the original value instead: `&*v`
14-
159
error: redundant slicing of the whole range
1610
--> $DIR/redundant_slicing.rs:14:13
1711
|
@@ -24,24 +18,12 @@ error: redundant slicing of the whole range
2418
LL | let err = &mut &S[..]; // Should reborrow instead of slice
2519
| ^^^^^^ help: reborrow the original value instead: `&*S`
2620

27-
error: redundant slicing of the whole range
28-
--> $DIR/redundant_slicing.rs:20:21
29-
|
30-
LL | let mut_slice = &mut vec[..]; // Deref instead of slice
31-
| ^^^^^^^^^^^^ help: dereference the original value instead: `&mut *vec`
32-
3321
error: redundant slicing of the whole range
3422
--> $DIR/redundant_slicing.rs:21:13
3523
|
3624
LL | let _ = &mut mut_slice[..]; // Should reborrow instead of slice
3725
| ^^^^^^^^^^^^^^^^^^ help: reborrow the original value instead: `&mut *mut_slice`
3826

39-
error: redundant slicing of the whole range
40-
--> $DIR/redundant_slicing.rs:24:13
41-
|
42-
LL | let _ = &ref_vec[..]; // Deref instead of slice
43-
| ^^^^^^^^^^^^ help: dereference the original value instead: `&**ref_vec`
44-
4527
error: redundant slicing of the whole range
4628
--> $DIR/redundant_slicing.rs:31:13
4729
|
@@ -60,5 +42,5 @@ error: redundant slicing of the whole range
6042
LL | let _ = (&bytes[..]).read_to_end(&mut vec![]).unwrap();
6143
| ^^^^^^^^^^^^ help: reborrow the original value instead: `(&*bytes)`
6244

63-
error: aborting due to 10 previous errors
45+
error: aborting due to 7 previous errors
6446

tests/ui/slice_as_deref.fixed

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::slice_as_deref)]
4+
5+
fn main() {
6+
let mut vec = vec![0];
7+
let _ = &*vec;
8+
let _ = &mut *vec;
9+
10+
let ref_vec = &mut vec;
11+
let _ = &**ref_vec;
12+
let _ = &mut **ref_vec;
13+
14+
let s = String::new();
15+
let _ = &*s;
16+
}

tests/ui/slice_as_deref.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::slice_as_deref)]
4+
5+
fn main() {
6+
let mut vec = vec![0];
7+
let _ = &vec[..];
8+
let _ = &mut vec[..];
9+
10+
let ref_vec = &mut vec;
11+
let _ = &ref_vec[..];
12+
let _ = &mut ref_vec[..];
13+
14+
let s = String::new();
15+
let _ = &s[..];
16+
}

tests/ui/slice_as_deref.stderr

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error: slicing when dereferencing would work
2+
--> $DIR/slice_as_deref.rs:7:13
3+
|
4+
LL | let _ = &vec[..];
5+
| ^^^^^^^^ help: dereference the original value instead: `&*vec`
6+
|
7+
= note: `-D clippy::slice-as-deref` implied by `-D warnings`
8+
9+
error: slicing when dereferencing would work
10+
--> $DIR/slice_as_deref.rs:8:13
11+
|
12+
LL | let _ = &mut vec[..];
13+
| ^^^^^^^^^^^^ help: dereference the original value instead: `&mut *vec`
14+
15+
error: slicing when dereferencing would work
16+
--> $DIR/slice_as_deref.rs:11:13
17+
|
18+
LL | let _ = &ref_vec[..];
19+
| ^^^^^^^^^^^^ help: dereference the original value instead: `&**ref_vec`
20+
21+
error: slicing when dereferencing would work
22+
--> $DIR/slice_as_deref.rs:12:13
23+
|
24+
LL | let _ = &mut ref_vec[..];
25+
| ^^^^^^^^^^^^^^^^ help: dereference the original value instead: `&mut **ref_vec`
26+
27+
error: slicing when dereferencing would work
28+
--> $DIR/slice_as_deref.rs:15:13
29+
|
30+
LL | let _ = &s[..];
31+
| ^^^^^^ help: dereference the original value instead: `&*s`
32+
33+
error: aborting due to 5 previous errors
34+

0 commit comments

Comments
 (0)