Open Aaron1011 opened 3 years ago
This sounds interesting, and I'd want to do it, any help on how I would do this though (sorry for naive question, this would be my first issue).
You can ping rustbot like rustbot claim
(plus an @ of course)
In regards to implementing this, I'm not sure. I don't think implicit deref coercion is explicitly in the HIR (as a node) so unless there's a query for it you'd need to figure it out yourself, maybe there's some method to do that in dereference.rs
(likely where you'd implement the lint). Perhaps @xFrednet (or others) have any ideas :)?
PS: FnCtxt::deref_steps
looks interesting! Same with can_coerce
. Haven't tested them so perhaps they won't catch this but it could be a good starting point ^^
Perhaps this could use TypeckResults::expr_adjustments
by checking if there are any Adjust::Deref
s where the target type is different from the source type (from expr_ty
without adjusts) with references removed
I hadn't thought of that; that sounds perfect for this
You found the correct method :+1:
What it does
Lints on any use of deref coercions - for example, accessing a field or method through a
Deref
implCategories (optional)
What is the advantage of the recommended code over the original code
Normally, deref coercions are a very useful part of rust, making smart points like
Box
,Rc
,Ref
, andRefMut
much easier to use. However, this can be undesirable when writingunsafe
code, since a non-trivial function call could be completely hidden from view. For example:Vec::set_len
). A deref coercion might cause a panic at an unexpected point (due to a panicking user-suppliedDeref
impl), leading to undefined behavior.&self
reference behind what looks like a normal field access, leading to a very subtle form of undefined behavior.Drawbacks
This would be a fairly niche lint - unless you're writing
unsafe
code, there's little need to use it.Example
Could be written as: