blob: 803d3b39f375d7b109fd18caccf7fcaf9e476c2b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#![deny(clippy::useless_conversion)]
#![allow(clippy::needless_if, clippy::unnecessary_fallible_conversions)]
fn test_generic<T: Copy>(val: T) -> T {
let _ = T::try_from(val).unwrap();
//~^ ERROR: useless conversion to the same type: `T`
val.try_into().unwrap()
//~^ ERROR: useless conversion to the same type: `T`
}
fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
// ok
let _: i32 = val.try_into().unwrap();
let _: U = val.try_into().unwrap();
let _ = U::try_from(val).unwrap();
}
fn main() {
test_generic(10i32);
test_generic2::<i32, i32>(10i32);
let _: String = "foo".try_into().unwrap();
let _: String = TryFrom::try_from("foo").unwrap();
let _ = String::try_from("foo").unwrap();
#[allow(clippy::useless_conversion)]
{
let _ = String::try_from("foo").unwrap();
let _: String = "foo".try_into().unwrap();
}
let _: String = "foo".to_string().try_into().unwrap();
//~^ ERROR: useless conversion to the same type: `std::string::String`
let _: String = TryFrom::try_from("foo".to_string()).unwrap();
//~^ ERROR: useless conversion to the same type: `std::string::String`
let _ = String::try_from("foo".to_string()).unwrap();
//~^ ERROR: useless conversion to the same type: `std::string::String`
let _ = String::try_from(format!("A: {:04}", 123)).unwrap();
//~^ ERROR: useless conversion to the same type: `std::string::String`
let _: String = format!("Hello {}", "world").try_into().unwrap();
//~^ ERROR: useless conversion to the same type: `std::string::String`
let _: String = String::new().try_into().unwrap();
//~^ ERROR: useless conversion to the same type: `std::string::String`
let _: String = match String::from("_").try_into() {
//~^ ERROR: useless conversion to the same type: `std::string::String`
Ok(a) => a,
Err(_) => String::new(),
};
// FIXME this is a false negative
#[allow(clippy::cmp_owned)]
if String::from("a") == TryInto::<String>::try_into(String::from("a")).unwrap() {}
}
|