From 5ae56f008c5b3df9686bb5de27a07e5fbf8b5c55 Mon Sep 17 00:00:00 2001 From: Alessio Cosenza Date: Tue, 4 Apr 2023 22:59:08 +0200 Subject: [PATCH] Add check `from_expansion` --- .../src/redundant_type_annotations.rs | 3 +- tests/ui/redundant_type_annotations.rs | 119 +++++++++++++----- tests/ui/redundant_type_annotations.stderr | 54 ++++---- 3 files changed, 117 insertions(+), 59 deletions(-) diff --git a/clippy_lints/src/redundant_type_annotations.rs b/clippy_lints/src/redundant_type_annotations.rs index c4edb1bb6f9d..00c91fae6506 100644 --- a/clippy_lints/src/redundant_type_annotations.rs +++ b/clippy_lints/src/redundant_type_annotations.rs @@ -102,7 +102,8 @@ fn is_redundant_in_func_call<'tcx>( impl LateLintPass<'_> for RedundantTypeAnnotations { fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx rustc_hir::Local<'tcx>) { // type annotation part - if let Some(ty) = &local.ty + if !local.span.from_expansion() + && let Some(ty) = &local.ty && let hir::TyKind::Path(ty_path) = &ty.kind && let hir::QPath::Resolved(_, resolved_path_ty) = ty_path diff --git a/tests/ui/redundant_type_annotations.rs b/tests/ui/redundant_type_annotations.rs index ab9541b1a62b..571325b85764 100644 --- a/tests/ui/redundant_type_annotations.rs +++ b/tests/ui/redundant_type_annotations.rs @@ -1,38 +1,50 @@ #![allow(unused)] #![warn(clippy::redundant_type_annotations)] -struct A; -enum E { +#[derive(Debug, Default)] +struct Cake { + _data: T, +} + +fn make_something() -> T { + T::default() +} + +fn make_cake() -> Cake { + Cake::::default() +} + +struct Pie { + inner: u32, +} + +enum Pizza { One, Two, } -fn f() -> String { +fn return_a_string() -> String { String::new() } -fn f_struct() -> A { - A +fn return_a_struct() -> Pie { + Pie { inner: 5 } } -fn f_enum() -> E { - E::One +fn return_an_enum() -> Pizza { + Pizza::One } -struct ATest2 { - inner: u32, +fn return_an_int() -> u32 { + 5 } -impl ATest2 { - fn func(&self) -> u32 { +impl Pie { + fn return_an_int(&self) -> u32 { self.inner } - fn a(&self) { - let v: u32 = self.func(); // This should lint but doesn't (MethodCall) - } - - fn get_num() -> u32 { + fn associated_return_an_int() -> u32 { 5 } @@ -40,34 +52,79 @@ impl ATest2 { Self { inner: 5 } } - fn get_string() -> String { + fn associated_return_a_string() -> String { String::from("") } + + fn test_method_call(&self) { + let v: u32 = self.return_an_int(); // This should lint but doesn't (MethodCall) + } } -fn f_prim() -> u32 { - 5 +fn test_generics() { + // The type annotation is needed to determine T + let _c: Cake = make_something(); + + // The type annotation is needed to determine the topic + let _c: Cake = make_cake(); +} + +fn test_non_locals() { + // This shouldn't lint + fn _arg(x: u32) -> u32 { + x + } + + // This could lint, but probably shouldn't + let _closure_arg = |x: u32| x; +} + +fn test_complex_types() { + // Shouldn't lint, since the literal will be i32 otherwise + let _u8: u8 = 128; + + // Should lint + let _tuple_i32: (i32, i32) = (12, 13); + + // Shouldn't lint, since the tuple will be i32 otherwise + let _tuple_u32: (u32, u32) = (1, 2); + + // Should lint, since the type is determined by the init value + let _tuple_u32: (u32, u32) = (3_u32, 4_u32); + + // Should lint + let _array: [i32; 3] = [5, 6, 7]; + + // Shouldn't lint + let _array: [u32; 2] = [8, 9]; } -fn main() { - let a: String = f(); +fn test_functions() { + // Everything here should lint - let a: A = f_struct(); + let _return: String = return_a_string(); - let a: E = f_enum(); + let _return: Pie = return_a_struct(); - let a: u32 = f_prim(); + let _return: Pizza = return_an_enum(); - let a: String = String::new(); + let _return: u32 = return_an_int(); - let st: ATest2 = ATest2::new(); - let a: u32 = st.func(); // this should lint but doesn't (MethodCall) + let _return: String = String::new(); - let a: String = String::from("test"); + let new_pie: Pie = Pie::new(); - let a: u32 = u32::MAX; + let _return: u32 = new_pie.return_an_int(); // this should lint but doesn't (MethodCall) - let a: u32 = ATest2::get_num(); + let _return: String = String::from("test"); - let a: String = ATest2::get_string(); + let _return: u32 = Pie::associated_return_an_int(); + + let _return: String = Pie::associated_return_a_string(); +} + +fn test_simple_types() { + let _var: u32 = u32::MAX; } + +fn main() {} diff --git a/tests/ui/redundant_type_annotations.stderr b/tests/ui/redundant_type_annotations.stderr index c4aa0483bee3..50f393178085 100644 --- a/tests/ui/redundant_type_annotations.stderr +++ b/tests/ui/redundant_type_annotations.stderr @@ -1,58 +1,58 @@ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:53:5 + --> $DIR/redundant_type_annotations.rs:107:5 | -LL | let a: String = f(); - | ^^^^^^^^^^^^^^^^^^^^ +LL | let _return: String = return_a_string(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::redundant-type-annotations` implied by `-D warnings` error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:55:5 + --> $DIR/redundant_type_annotations.rs:109:5 | -LL | let a: A = f_struct(); - | ^^^^^^^^^^^^^^^^^^^^^^ +LL | let _return: Pie = return_a_struct(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:57:5 + --> $DIR/redundant_type_annotations.rs:111:5 | -LL | let a: E = f_enum(); - | ^^^^^^^^^^^^^^^^^^^^ +LL | let _return: Pizza = return_an_enum(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:59:5 + --> $DIR/redundant_type_annotations.rs:113:5 | -LL | let a: u32 = f_prim(); - | ^^^^^^^^^^^^^^^^^^^^^^ +LL | let _return: u32 = return_an_int(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:61:5 + --> $DIR/redundant_type_annotations.rs:115:5 | -LL | let a: String = String::new(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _return: String = String::new(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:63:5 + --> $DIR/redundant_type_annotations.rs:117:5 | -LL | let st: ATest2 = ATest2::new(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let new_pie: Pie = Pie::new(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:68:5 + --> $DIR/redundant_type_annotations.rs:123:5 | -LL | let a: u32 = u32::MAX; - | ^^^^^^^^^^^^^^^^^^^^^^ +LL | let _return: u32 = Pie::associated_return_an_int(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:70:5 + --> $DIR/redundant_type_annotations.rs:125:5 | -LL | let a: u32 = ATest2::get_num(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _return: String = Pie::associated_return_a_string(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: redundant type annotation - --> $DIR/redundant_type_annotations.rs:72:5 + --> $DIR/redundant_type_annotations.rs:129:5 | -LL | let a: String = ATest2::get_string(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _var: u32 = u32::MAX; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 9 previous errors