From 2713ad43425bef040e3c7d55618d18035fe68c1a Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Sat, 13 Mar 2021 08:27:42 -0500 Subject: [PATCH] Properly lint macro arguments for `explicit_deref_methods` --- clippy_lints/src/dereference.rs | 6 +++--- tests/ui/explicit_deref_methods.fixed | 2 ++ tests/ui/explicit_deref_methods.rs | 2 ++ tests/ui/explicit_deref_methods.stderr | 8 +++++++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs index a1d0110929ce..40ed6d8d1541 100644 --- a/clippy_lints/src/dereference.rs +++ b/clippy_lints/src/dereference.rs @@ -105,7 +105,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing { match kind { RefOp::Method(target_mut) if !is_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id) - && is_linted_explicit_deref_position(parent, expr.hir_id) => + && is_linted_explicit_deref_position(parent, expr.hir_id, expr.span) => { self.state = Some(( State::DerefMethod { @@ -189,9 +189,9 @@ fn deref_method_same_type(result_ty: Ty<'tcx>, arg_ty: Ty<'tcx>) -> bool { // Checks whether the parent node is a suitable context for switching from a deref method to the // deref operator. -fn is_linted_explicit_deref_position(parent: Option>, child_id: HirId) -> bool { +fn is_linted_explicit_deref_position(parent: Option>, child_id: HirId, child_span: Span) -> bool { let parent = match parent { - Some(Node::Expr(e)) => e, + Some(Node::Expr(e)) if e.span.ctxt() == child_span.ctxt() => e, _ => return true, }; match parent.kind { diff --git a/tests/ui/explicit_deref_methods.fixed b/tests/ui/explicit_deref_methods.fixed index 8155fdf99503..51d0468e47ca 100644 --- a/tests/ui/explicit_deref_methods.fixed +++ b/tests/ui/explicit_deref_methods.fixed @@ -76,6 +76,8 @@ fn main() { } let b: &str = expr_deref!(a); + let b: &str = expr_deref!(&*a); + // The struct does not implement Deref trait #[derive(Copy, Clone)] struct NoLint(u32); diff --git a/tests/ui/explicit_deref_methods.rs b/tests/ui/explicit_deref_methods.rs index 8dc5272e67fa..680664bd4f6f 100644 --- a/tests/ui/explicit_deref_methods.rs +++ b/tests/ui/explicit_deref_methods.rs @@ -76,6 +76,8 @@ fn main() { } let b: &str = expr_deref!(a); + let b: &str = expr_deref!(a.deref()); + // The struct does not implement Deref trait #[derive(Copy, Clone)] struct NoLint(u32); diff --git a/tests/ui/explicit_deref_methods.stderr b/tests/ui/explicit_deref_methods.stderr index 335c0e4bb9d1..8035d77d18d5 100644 --- a/tests/ui/explicit_deref_methods.stderr +++ b/tests/ui/explicit_deref_methods.stderr @@ -66,5 +66,11 @@ error: explicit `deref` method call LL | let b = opt_a.unwrap().deref(); | ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&*opt_a.unwrap()` -error: aborting due to 11 previous errors +error: explicit `deref` method call + --> $DIR/explicit_deref_methods.rs:79:31 + | +LL | let b: &str = expr_deref!(a.deref()); + | ^^^^^^^^^ help: try this: `&*a` + +error: aborting due to 12 previous errors