]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/methods.rs
Merge pull request #452 from fhartwig/lifetime-false-positives
[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     let opt = Some(0);
39     let _ = opt.unwrap();  //~ERROR used unwrap() on an Option
40
41     let res: Result<i32, ()> = Ok(0);
42     let _ = res.unwrap();  //~ERROR used unwrap() on a Result
43
44     let _ = "str".to_string();  //~ERROR `"str".to_owned()` is faster
45
46     let v = &"str";
47     let string = v.to_string();  //~ERROR `(*v).to_owned()` is faster
48     let _again = string.to_string();  //~ERROR `String.to_string()` is a no-op
49 }