Skip to content

Commit 3106219

Browse files
committedJan 30, 2024
Don't lint slice type annotations for byte strings
1 parent 455c07b commit 3106219

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed
 

Diff for: ‎clippy_lints/src/redundant_type_annotations.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
188188
match init_lit.node {
189189
// In these cases the annotation is redundant
190190
LitKind::Str(..)
191-
| LitKind::ByteStr(..)
192191
| LitKind::Byte(..)
193192
| LitKind::Char(..)
194193
| LitKind::Bool(..)
@@ -202,6 +201,16 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
202201
}
203202
},
204203
LitKind::Err => (),
204+
LitKind::ByteStr(..) => {
205+
// We only lint if the type annotation is an array type (e.g. &[u8; 4]).
206+
// If instead it is a slice (e.g. &[u8]) it may not be redundant, so we
207+
// don't lint.
208+
if let hir::TyKind::Ref(_, mut_ty) = ty.kind
209+
&& matches!(mut_ty.ty.kind, hir::TyKind::Array(..))
210+
{
211+
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
212+
}
213+
},
205214
}
206215
},
207216
_ => (),

Diff for: ‎tests/ui/redundant_type_annotations.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,18 @@ fn test_simple_types() {
196196
let _var: &str = "test";
197197
//~^ ERROR: redundant type annotation
198198

199-
let _var: &[u8] = b"test";
199+
let _var: &[u8; 4] = b"test";
200200
//~^ ERROR: redundant type annotation
201201

202202
let _var: bool = false;
203203
//~^ ERROR: redundant type annotation
204204
}
205205

206+
fn issue12212() {
207+
// This should not be linted
208+
let _var: &[u8] = b"test";
209+
}
210+
206211
fn issue11190() {}
207212

208213
fn main() {}

Diff for: ‎tests/ui/redundant_type_annotations.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ LL | let _var: &str = "test";
9494
error: redundant type annotation
9595
--> $DIR/redundant_type_annotations.rs:199:5
9696
|
97-
LL | let _var: &[u8] = b"test";
98-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
97+
LL | let _var: &[u8; 4] = b"test";
98+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9999

100100
error: redundant type annotation
101101
--> $DIR/redundant_type_annotations.rs:202:5

0 commit comments

Comments
 (0)