Should Clippy require Option<&T>?

Would it be useful to have Clippy declare that Option<&T> be used as a function's argument, over &Option<T>? The former does seem more widely used.

Some related discussion: API design: &Option<T> vs Option<&T>

1 Like

Sure, it could be a lint. I'm not sure what false positive rate it would have, though, or how to decide when exactly it should warn.

I think it could be beneficial.

The &Option<T> form is almost never what you actually want because it requires having a concrete Option<T> to reference, whereas a Option<&T> is more flexible. It could come from calling as_ref() on a Option<T>, or you could convert an Option<String> to a Option<&str> via as_deref(), or you could just pass a None literal into the function.

7 Likes