Skip to content

Commit 08c8cd5

Browse files
committedFeb 7, 2024
Auto merge of #12216 - bpandreotti:redundant-type-annotations-fix, r=Jarcho
Fix false positive in `redundant_type_annotations` lint This PR changes the `redundant_type_annotations` lint to allow slice type annotations (i.e., `&[u8]`) for byte string literals. It will still consider _array_ type annotations (i.e., `&[u8; 4]`) as redundant. The reasoning behind this is that the type of byte string literals is by default a reference to an array, but, by using a type annotation, you can force it to be a slice. For example: ```rust let a: &[u8; 4] = b"test"; let b: &[u8] = b"test"; ``` Now, the type annotation for `a` will still be linted (as it is still redundant), but the type annotation for `b` will not. Fixes #12212. changelog: [`redundant_type_annotations`]: Fix false positive with byte string literals
2 parents b1e5a58 + 3106219 commit 08c8cd5

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed
 

‎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
_ => (),

‎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() {}

‎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)