Skip to content

Commit 2713ad4

Browse files
committed
Properly lint macro arguments for explicit_deref_methods
1 parent 704f7a8 commit 2713ad4

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

clippy_lints/src/dereference.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
105105
match kind {
106106
RefOp::Method(target_mut)
107107
if !is_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
108-
&& is_linted_explicit_deref_position(parent, expr.hir_id) =>
108+
&& is_linted_explicit_deref_position(parent, expr.hir_id, expr.span) =>
109109
{
110110
self.state = Some((
111111
State::DerefMethod {
@@ -189,9 +189,9 @@ fn deref_method_same_type(result_ty: Ty<'tcx>, arg_ty: Ty<'tcx>) -> bool {
189189

190190
// Checks whether the parent node is a suitable context for switching from a deref method to the
191191
// deref operator.
192-
fn is_linted_explicit_deref_position(parent: Option<Node<'_>>, child_id: HirId) -> bool {
192+
fn is_linted_explicit_deref_position(parent: Option<Node<'_>>, child_id: HirId, child_span: Span) -> bool {
193193
let parent = match parent {
194-
Some(Node::Expr(e)) => e,
194+
Some(Node::Expr(e)) if e.span.ctxt() == child_span.ctxt() => e,
195195
_ => return true,
196196
};
197197
match parent.kind {

tests/ui/explicit_deref_methods.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ fn main() {
7676
}
7777
let b: &str = expr_deref!(a);
7878

79+
let b: &str = expr_deref!(&*a);
80+
7981
// The struct does not implement Deref trait
8082
#[derive(Copy, Clone)]
8183
struct NoLint(u32);

tests/ui/explicit_deref_methods.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ fn main() {
7676
}
7777
let b: &str = expr_deref!(a);
7878

79+
let b: &str = expr_deref!(a.deref());
80+
7981
// The struct does not implement Deref trait
8082
#[derive(Copy, Clone)]
8183
struct NoLint(u32);

tests/ui/explicit_deref_methods.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,11 @@ error: explicit `deref` method call
6666
LL | let b = opt_a.unwrap().deref();
6767
| ^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&*opt_a.unwrap()`
6868

69-
error: aborting due to 11 previous errors
69+
error: explicit `deref` method call
70+
--> $DIR/explicit_deref_methods.rs:79:31
71+
|
72+
LL | let b: &str = expr_deref!(a.deref());
73+
| ^^^^^^^^^ help: try this: `&*a`
74+
75+
error: aborting due to 12 previous errors
7076

0 commit comments

Comments
 (0)