Repository navigation
Do not suggest as_deref for a slice pattern on Err - #165406
lapla-cogito wants to merge 1 commit into
Conversation
|
r? @adwinwhite rustbot has assigned @adwinwhite. Use Why was this reviewer chosen?The reviewer was selected based on:
|
| let deref_ty = args.type_at(0).peel_refs(); | ||
| if self.can_eq(self.param_env, expected_ty.peel_refs(), deref_ty) { |
There was a problem hiding this comment.
this might need to account for the constructor(s) used in the pattern? it e.g. still produces false positives when the success and error types of Result are the same, such as for Err([]) matching on Result<Vec<E>, Vec<E>>
or for a more contrived example where the pattern itself matters,
enum Contrived<'a> {
Layer(&'a Contrived<'a>),
Value(Vec<E>),
}
impl Deref for Contrived<'_> {
type Target = [E];
fn deref(&self) -> &[E] {
match self {
Contrived::Layer(peeled) => peeled.deref(),
Contrived::Value(value) => value.deref(),
}
}
}
fn contrived(opt: Option<Contrived<'_>>) {
match opt {
// suggests `.as_deref()`, but that causes a type mismatch:
Some(Contrived::Layer([])) => {}
_ => {}
}
}There was a problem hiding this comment.
Thanks! Now only suggest when the slice pattern is the payload of Some or Ok itself.
5ecdedc to
cfefcf2
Compare
| }) | ||
| } | ||
|
|
||
| fn slice_pattern_is_direct_payload( |
There was a problem hiding this comment.
this traversal looks pretty complicated relative to what we'd be gaining from it. a few thoughts:
- maybe it could help to use a hir parent iterator to start at the slice pattern and go up to the top? that way it wouldn't have to search each or-pattern alternative
- instead of tracking state with the
AsDerefPatPosenum, maybe it would be clearer to have a helper that peels off&s/guards/or-patterns/etc.? with a hir parent iterator I imagine you could check that doing that once gets you to anOk/Someand doing it again gets you to the top of the pattern - the cases that continue searching with
AsDerefPatPos::Rejectedlook like they could just returnfalseinstead
There was a problem hiding this comment.
It makes sense. Thanks.
| PatKind::Struct(_, fields, _) => { | ||
| fields.iter().any(|field| step(field.pat, AsDerefPatPos::Rejected)) | ||
| } |
There was a problem hiding this comment.
probably rare enough that it's not worth accounting for, but just in case it's easy, I feel obliged to note that Some { 0: [] } and Ok { 0: [] } are equivalent to Some([]) and Ok([])
cfefcf2 to
ad726ca
Compare
Closes #165365