now the method lookup actually works (and I understand why! 😄), reduces unnecessary loops, and has a few comments

This commit is contained in:
llogiq 2015-06-01 12:49:36 +02:00
parent c5dfcaeeaf
commit cdca2c93c1
4 changed files with 98 additions and 27 deletions

View file

@ -5,14 +5,14 @@ struct One;
#[deny(len_without_is_empty)]
impl One {
fn len(self: &Self) -> isize { //~ERROR Item 'One' has a '.len()' method
fn len(self: &Self) -> isize { //~ERROR Item 'One' has a '.len(_: &Self)'
1
}
}
#[deny(len_without_is_empty)]
trait TraitsToo {
fn len(self: &Self) -> isize; //~ERROR Trait 'TraitsToo' has a '.len()' method,
fn len(self: &Self) -> isize; //~ERROR Trait 'TraitsToo' has a '.len(_:
}
impl TraitsToo for One {
@ -21,17 +21,47 @@ impl TraitsToo for One {
}
}
#[allow(dead_code)]
struct HasIsEmpty;
#[deny(len_without_is_empty)]
#[allow(dead_code)]
impl HasIsEmpty {
fn len(self: &Self) -> isize {
1
}
fn is_empty(self: &Self) -> bool {
false
}
}
struct Wither;
#[deny(len_without_is_empty)]
trait WithIsEmpty {
fn len(self: &Self) -> isize;
fn is_empty(self: &Self) -> bool;
}
impl WithIsEmpty for Wither {
fn len(self: &Self) -> isize {
1
}
fn is_empty(self: &Self) -> bool {
false
}
}
struct HasWrongIsEmpty;
#[deny(len_without_is_empty)]
impl HasWrongIsEmpty {
fn len(self: &Self) -> isize { //~ERROR Item 'HasWrongIsEmpty' has a '.len(_: &Self)'
1
}
fn is_empty() -> bool {
#[allow(dead_code, unused)]
fn is_empty(self: &Self, x : u32) -> bool {
false
}
}
@ -49,7 +79,24 @@ fn main() {
}
let z : &TraitsToo = &y;
if z.len() > 0 { //~ERROR Consider replacing the len comparison
if z.len() > 0 { //no error, because TraitsToo has no .is_empty() method
println!("Nor should this!");
}
let hie = HasIsEmpty;
if hie.len() == 0 { //~ERROR Consider replacing the len comparison
println!("Or this!");
}
assert!(!hie.is_empty());
let wie : &WithIsEmpty = &Wither;
if wie.len() == 0 { //~ERROR Consider replacing the len comparison
println!("Or this!");
}
assert!(!wie.is_empty());
let hwie = HasWrongIsEmpty;
if hwie.len() == 0 { //no error as HasWrongIsEmpty does not have .is_empty()
println!("Or this!");
}
}