Skip to content

Commit d602ab1

Browse files
committed
Don't lint exlipicit_auto_deref when other adjustments are needed
1 parent 84e03b6 commit d602ab1

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

clippy_lints/src/dereference.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,11 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
357357
}),
358358
StateData { span: expr.span, hir_id: expr.hir_id, position },
359359
));
360-
} else if position.is_deref_stable() {
360+
} else if position.is_deref_stable()
361+
// Auto-deref doesn't combine with other adjustments
362+
&& next_adjust.map_or(true, |a| matches!(a.kind, Adjust::Deref(_) | Adjust::Borrow(_)))
363+
&& iter.all(|a| matches!(a.kind, Adjust::Deref(_) | Adjust::Borrow(_)))
364+
{
361365
self.state = Some((
362366
State::Borrow { mutability },
363367
StateData {

tests/ui/explicit_auto_deref.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,17 @@ fn main() {
242242
fn ret_any(x: &Box<dyn std::any::Any>) -> &dyn std::any::Any {
243243
&**x
244244
}
245+
246+
let x = String::new();
247+
let _: *const str = &*x;
248+
249+
struct S7([u32; 1]);
250+
impl core::ops::Deref for S7 {
251+
type Target = [u32; 1];
252+
fn deref(&self) -> &Self::Target {
253+
&self.0
254+
}
255+
}
256+
let x = S7([0]);
257+
let _: &[u32] = &*x;
245258
}

tests/ui/explicit_auto_deref.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,17 @@ fn main() {
242242
fn ret_any(x: &Box<dyn std::any::Any>) -> &dyn std::any::Any {
243243
&**x
244244
}
245+
246+
let x = String::new();
247+
let _: *const str = &*x;
248+
249+
struct S7([u32; 1]);
250+
impl core::ops::Deref for S7 {
251+
type Target = [u32; 1];
252+
fn deref(&self) -> &Self::Target {
253+
&self.0
254+
}
255+
}
256+
let x = S7([0]);
257+
let _: &[u32] = &*x;
245258
}

0 commit comments

Comments
 (0)