Description
Regarding my failed attempt to fully solve #32 (which would require more typeck than external lints can currently acquire), I thought about the social contract around .len()
and .is_empty()
. While we don't have a Bounded
trait that defines .len()
and implements .is_empty()
, we can still ask implementers to provide the latter.
There is some precedence to the use of is_empty: E.g. in Java, the Collection
interface defines boolean isEmpty()
with the usual meaning. C++'s STL defines empty()
on all containers, Ocaml at least defines is_empty
for stacks. Python's lists and Lua's tables are basically arrays, so they have no problem with using len(_)
.
In any event, we could add a lint that looks for traits which define .len()
but do not define .is_empty()
and ask the implementer to consider adding it. This would also remove the need for method lookup on that other lint – if most traits have .is_empty()
, we won't need to care for the few remaining false positives, right?
A basic implementation would check_item
for ItemTrait
s and see if the contained TraitItem
s fail to adhere to the social contract.