Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioC31 committed Apr 17, 2023
1 parent 059c439 commit bc66d9a
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 58 deletions.
140 changes: 109 additions & 31 deletions tests/ui/redundant_type_annotations.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,151 @@
#![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()
}

fn plus_one<T: std::ops::Add<u8, Output = T>>(val: T) -> T {
val + 1
}

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();

// This should lint (doesn't)
let _c: Cake<u8> = make_cake::<u8>();

// This should lint (doesn't)
let _c: u8 = make_something::<u8>();

// This should lint (doesn't)
let _c: u8 = plus_one(5_u8);

// Annotation needed otherwise T is i32
let _c: u8 = plus_one(5);
}

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 (doesn't)
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 (doesn't)
let _tuple_u32: (u32, u32) = (3_u32, 4_u32);

// Should lint (doesn't)
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 _return: String = return_a_string();

let a: A = f_struct();
let _return: Pie = return_a_struct();

let a: E = f_enum();
let _return: Pizza = return_an_enum();

let a: u32 = f_prim();
let _return: u32 = return_an_int();

let a: String = String::new();
let _return: String = String::new();

let st: ATest2 = ATest2::new();
let a: u32 = st.func(); // this should lint but doesn't (MethodCall)
let new_pie: Pie = Pie::new();

let a: String = String::from("test");
let _return: u32 = new_pie.return_an_int(); // this should lint but doesn't (MethodCall)

let a: u32 = u32::MAX;
let _return: String = String::from("test");

let a: u32 = ATest2::get_num();
let _return: u32 = Pie::associated_return_an_int();

let a: String = ATest2::get_string();
let _return: String = Pie::associated_return_a_string();
}

fn test_simple_types() {
let _var: u32 = u32::MAX;

// Should lint (doesn't)
let _var: u32 = 5_u32;
}

fn main() {}

// TODO: test refs
54 changes: 27 additions & 27 deletions tests/ui/redundant_type_annotations.stderr
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:121: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:123: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:125: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:127: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:129: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:131: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:137: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:139: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:143:5
|
LL | let a: String = ATest2::get_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _var: u32 = u32::MAX;
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors

0 comments on commit bc66d9a

Please sign in to comment.