-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c97cfc
commit 5ae56f0
Showing
3 changed files
with
117 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,130 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::redundant_type_annotations)] | ||
|
||
struct A; | ||
enum E { | ||
#[derive(Debug, Default)] | ||
struct Cake<T> { | ||
_data: T, | ||
} | ||
|
||
fn make_something<T: Default>() -> T { | ||
T::default() | ||
} | ||
|
||
fn make_cake<T: Default>() -> Cake<T> { | ||
Cake::<T>::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 | ||
} | ||
|
||
fn new() -> Self { | ||
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<i32> = make_something(); | ||
|
||
// The type annotation is needed to determine the topic | ||
let _c: Cake<u8> = 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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|