]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/methods.rs
Merge branch 'pr-462'
[rust.git] / tests / compile-fail / methods.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #![allow(unused)]
5 #![deny(clippy, clippy_pedantic)]
6
7 use std::ops::Mul;
8
9 struct T;
10
11 impl T {
12     fn add(self, other: T) -> T { self } //~ERROR defining a method called `add`
13     fn drop(&mut self) { } //~ERROR defining a method called `drop`
14
15     fn sub(&self, other: T) -> &T { self } // no error, self is a ref
16     fn div(self) -> T { self } // no error, different #arguments
17     fn rem(self, other: T) { } // no error, wrong return type
18
19     fn into_u32(self) -> u32 { 0 } // fine
20     fn into_u16(&self) -> u16 { 0 } //~ERROR methods called `into_*` usually take self by value
21
22     fn to_something(self) -> u32 { 0 } //~ERROR methods called `to_*` usually take self by reference
23 }
24
25 #[derive(Clone,Copy)]
26 struct U;
27
28 impl U {
29     fn to_something(self) -> u32 { 0 } // ok because U is Copy
30 }
31
32 impl Mul<T> for T {
33     type Output = T;
34     fn mul(self, other: T) -> T { self } // no error, obviously
35 }
36
37 fn main() {
38     use std::io;
39
40     let opt = Some(0);
41     let _ = opt.unwrap();  //~ERROR used unwrap() on an Option
42
43     let res: Result<i32, ()> = Ok(0);
44     let _ = res.unwrap();  //~ERROR used unwrap() on a Result
45
46     let _ = "str".to_string();  //~ERROR `"str".to_owned()` is faster
47
48     let v = &"str";
49     let string = v.to_string();  //~ERROR `(*v).to_owned()` is faster
50     let _again = string.to_string();  //~ERROR `String.to_string()` is a no-op
51
52     res.ok().expect("disaster!"); //~ERROR called `ok().expect()`
53     // the following should not warn, since `expect` isn't implemented unless
54     // the error type implements `Debug`
55     let res2: Result<i32, MyError> = Ok(0);
56     res2.ok().expect("oh noes!");
57     // we currently don't warn if the error type has a type parameter
58     // (but it would be nice if we did)
59     let res3: Result<u32, MyErrorWithParam<u8>>= Ok(0);
60     res3.ok().expect("whoof");
61     let res4: Result<u32, io::Error> = Ok(0);
62     res4.ok().expect("argh"); //~ERROR called `ok().expect()`
63     let res5: io::Result<u32> = Ok(0);
64     res5.ok().expect("oops"); //~ERROR called `ok().expect()`
65     let res6: Result<u32, &str> = Ok(0);
66     res6.ok().expect("meh"); //~ERROR called `ok().expect()`
67 }
68
69 struct MyError(()); // doesn't implement Debug
70
71 #[derive(Debug)]
72 struct MyErrorWithParam<T> {
73     x: T
74 }