mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
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
This commit is contained in:
commit
08c8cd5014
3 changed files with 18 additions and 4 deletions
|
@ -188,7 +188,6 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
|
||||||
match init_lit.node {
|
match init_lit.node {
|
||||||
// In these cases the annotation is redundant
|
// In these cases the annotation is redundant
|
||||||
LitKind::Str(..)
|
LitKind::Str(..)
|
||||||
| LitKind::ByteStr(..)
|
|
||||||
| LitKind::Byte(..)
|
| LitKind::Byte(..)
|
||||||
| LitKind::Char(..)
|
| LitKind::Char(..)
|
||||||
| LitKind::Bool(..)
|
| LitKind::Bool(..)
|
||||||
|
@ -202,6 +201,16 @@ impl LateLintPass<'_> for RedundantTypeAnnotations {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
LitKind::Err => (),
|
LitKind::Err => (),
|
||||||
|
LitKind::ByteStr(..) => {
|
||||||
|
// We only lint if the type annotation is an array type (e.g. &[u8; 4]).
|
||||||
|
// If instead it is a slice (e.g. &[u8]) it may not be redundant, so we
|
||||||
|
// don't lint.
|
||||||
|
if let hir::TyKind::Ref(_, mut_ty) = ty.kind
|
||||||
|
&& matches!(mut_ty.ty.kind, hir::TyKind::Array(..))
|
||||||
|
{
|
||||||
|
span_lint(cx, REDUNDANT_TYPE_ANNOTATIONS, local.span, "redundant type annotation");
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
|
@ -196,13 +196,18 @@ fn test_simple_types() {
|
||||||
let _var: &str = "test";
|
let _var: &str = "test";
|
||||||
//~^ ERROR: redundant type annotation
|
//~^ ERROR: redundant type annotation
|
||||||
|
|
||||||
let _var: &[u8] = b"test";
|
let _var: &[u8; 4] = b"test";
|
||||||
//~^ ERROR: redundant type annotation
|
//~^ ERROR: redundant type annotation
|
||||||
|
|
||||||
let _var: bool = false;
|
let _var: bool = false;
|
||||||
//~^ ERROR: redundant type annotation
|
//~^ ERROR: redundant type annotation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue12212() {
|
||||||
|
// This should not be linted
|
||||||
|
let _var: &[u8] = b"test";
|
||||||
|
}
|
||||||
|
|
||||||
fn issue11190() {}
|
fn issue11190() {}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -94,8 +94,8 @@ LL | let _var: &str = "test";
|
||||||
error: redundant type annotation
|
error: redundant type annotation
|
||||||
--> $DIR/redundant_type_annotations.rs:199:5
|
--> $DIR/redundant_type_annotations.rs:199:5
|
||||||
|
|
|
|
||||||
LL | let _var: &[u8] = b"test";
|
LL | let _var: &[u8; 4] = b"test";
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: redundant type annotation
|
error: redundant type annotation
|
||||||
--> $DIR/redundant_type_annotations.rs:202:5
|
--> $DIR/redundant_type_annotations.rs:202:5
|
||||||
|
|
Loading…
Reference in a new issue