redundant_type_annotations: only pass certain def kinds to type_of

This commit is contained in:
Alex Macleod 2023-07-19 17:22:06 +00:00
parent 0b63e95dce
commit d4f735c3f5
3 changed files with 38 additions and 21 deletions

View file

@ -1,6 +1,8 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_lint_allowed;
use rustc_ast::LitKind;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty;
use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -45,8 +47,8 @@ fn is_same_type<'tcx>(cx: &LateContext<'tcx>, ty_resolved_path: hir::def::Res, f
return primty.name() == func_return_type_sym;
}
// type annotation is any other non generic type
if let hir::def::Res::Def(_, defid) = ty_resolved_path
// type annotation is a non generic type
if let hir::def::Res::Def(DefKind::Struct | DefKind::Union | DefKind::Enum, defid) = ty_resolved_path
&& let Some(annotation_ty) = cx.tcx.type_of(defid).no_bound_vars()
{
return annotation_ty == func_return_type;
@ -130,8 +132,9 @@ fn extract_primty(ty_kind: &hir::TyKind<'_>) -> Option<hir::PrimTy> {
impl LateLintPass<'_> for RedundantTypeAnnotations {
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'tcx>) {
// type annotation part
if !local.span.from_expansion()
if !is_lint_allowed(cx, REDUNDANT_TYPE_ANNOTATIONS, local.hir_id)
// type annotation part
&& !local.span.from_expansion()
&& let Some(ty) = &local.ty
// initialization part

View file

@ -6,8 +6,8 @@ struct Cake<T> {
_data: T,
}
fn make_something<T: Default>() -> T {
T::default()
fn make_something<T>() -> T {
unimplemented!()
}
fn make_cake<T: Default>() -> Cake<T> {
@ -117,7 +117,15 @@ fn test_non_locals() {
let _closure_arg = |x: u32| x;
}
fn test_complex_types() {
trait Trait {
type AssocTy;
}
impl Trait for () {
type AssocTy = String;
}
fn test_complex_types<T>() {
// Shouldn't be lint, since the literal will be i32 otherwise
let _u8: u8 = 128;
@ -135,6 +143,10 @@ fn test_complex_types() {
// Shouldn't be lint
let _array: [u32; 2] = [8, 9];
let ty_param: T = make_something();
let assoc_ty: <() as Trait>::AssocTy = String::new();
}
fn test_functions() {
@ -173,4 +185,6 @@ fn test_simple_types() {
let _var: bool = false;
}
fn issue11190() {}
fn main() {}

View file

@ -19,85 +19,85 @@ LL | let v: &Slice = self.return_a_ref_to_struct();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:143:5
--> $DIR/redundant_type_annotations.rs:155:5
|
LL | let _return: String = return_a_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:145:5
--> $DIR/redundant_type_annotations.rs:157:5
|
LL | let _return: Pie = return_a_struct();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:147:5
--> $DIR/redundant_type_annotations.rs:159:5
|
LL | let _return: Pizza = return_an_enum();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:149:5
--> $DIR/redundant_type_annotations.rs:161:5
|
LL | let _return: u32 = return_an_int();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:151:5
--> $DIR/redundant_type_annotations.rs:163:5
|
LL | let _return: String = String::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:153:5
--> $DIR/redundant_type_annotations.rs:165:5
|
LL | let new_pie: Pie = Pie::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:155:5
--> $DIR/redundant_type_annotations.rs:167:5
|
LL | let _return: u32 = new_pie.return_an_int();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:157:5
--> $DIR/redundant_type_annotations.rs:169:5
|
LL | let _return: u32 = Pie::associated_return_an_int();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:159:5
--> $DIR/redundant_type_annotations.rs:171:5
|
LL | let _return: String = Pie::associated_return_a_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:165:5
--> $DIR/redundant_type_annotations.rs:177:5
|
LL | let _var: u32 = u32::MAX;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:167:5
--> $DIR/redundant_type_annotations.rs:179:5
|
LL | let _var: u32 = 5_u32;
| ^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:169:5
--> $DIR/redundant_type_annotations.rs:181:5
|
LL | let _var: &str = "test";
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:171:5
--> $DIR/redundant_type_annotations.rs:183:5
|
LL | let _var: &[u8] = b"test";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
error: redundant type annotation
--> $DIR/redundant_type_annotations.rs:173:5
--> $DIR/redundant_type_annotations.rs:185:5
|
LL | let _var: bool = false;
| ^^^^^^^^^^^^^^^^^^^^^^^