Skip to content

Commit 77e395e

Browse files
committed
Auto merge of rust-lang#11376 - Jarcho:issue_11366, r=llogiq
Fix span when linting `explicit_auto_deref` immediately after `needless_borrow` fixes rust-lang#11366 changelog: `explicit_auto_deref`: Fix span when linting immediately after `needless_borrow`
2 parents c50d86f + 82f2e52 commit 77e395e

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

clippy_lints/src/dereference.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,14 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
609609
adjusted_ty,
610610
},
611611
));
612-
} else if stability.is_deref_stable() {
612+
} else if stability.is_deref_stable()
613+
&& let Some(parent) = get_parent_expr(cx, expr)
614+
{
613615
self.state = Some((
614616
State::ExplicitDeref { mutability: None },
615617
StateData {
616-
span: expr.span,
617-
hir_id: expr.hir_id,
618+
span: parent.span,
619+
hir_id: parent.hir_id,
618620
adjusted_ty,
619621
},
620622
));

tests/ui/explicit_auto_deref.fixed

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn main() {
205205
}
206206
}
207207

208-
f_str(&&ref_str); // `needless_borrow` will suggest removing both references
208+
f_str(&ref_str); // `needless_borrow` will suggest removing both references
209209
f_str(&ref_str); // `needless_borrow` will suggest removing only one reference
210210

211211
let x = &&40;
@@ -293,4 +293,10 @@ fn main() {
293293
fn return_dyn_assoc<'a>(x: &'a &'a u32) -> &'a <&'a u32 as WithAssoc>::Assoc {
294294
*x
295295
}
296+
297+
// Issue #11366
298+
let _: &mut u32 = match &mut Some(&mut 0u32) {
299+
Some(x) => x,
300+
None => panic!(),
301+
};
296302
}

tests/ui/explicit_auto_deref.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,10 @@ fn main() {
293293
fn return_dyn_assoc<'a>(x: &'a &'a u32) -> &'a <&'a u32 as WithAssoc>::Assoc {
294294
*x
295295
}
296+
297+
// Issue #11366
298+
let _: &mut u32 = match &mut Some(&mut 0u32) {
299+
Some(x) => &mut *x,
300+
None => panic!(),
301+
};
296302
}

tests/ui/explicit_auto_deref.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ LL | let _ = f_str(**ref_ref_str);
193193
| ^^^^^^^^^^^^^ help: try: `ref_ref_str`
194194

195195
error: deref which would be done by auto-deref
196-
--> $DIR/explicit_auto_deref.rs:208:13
196+
--> $DIR/explicit_auto_deref.rs:208:12
197197
|
198198
LL | f_str(&&*ref_str); // `needless_borrow` will suggest removing both references
199-
| ^^^^^^^^ help: try: `ref_str`
199+
| ^^^^^^^^^ help: try: `ref_str`
200200

201201
error: deref which would be done by auto-deref
202202
--> $DIR/explicit_auto_deref.rs:209:12
@@ -234,5 +234,11 @@ error: deref which would be done by auto-deref
234234
LL | *x
235235
| ^^ help: try: `x`
236236

237-
error: aborting due to 39 previous errors
237+
error: deref which would be done by auto-deref
238+
--> $DIR/explicit_auto_deref.rs:299:20
239+
|
240+
LL | Some(x) => &mut *x,
241+
| ^^^^^^^ help: try: `x`
242+
243+
error: aborting due to 40 previous errors
238244

0 commit comments

Comments
 (0)