Skip to content

Commit

Permalink
Properly lint macro arguments for explicit_deref_methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Mar 13, 2021
1 parent 704f7a8 commit 2713ad4
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Node<'_>>, child_id: HirId) -> bool {
fn is_linted_explicit_deref_position(parent: Option<Node<'_>>, 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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/explicit_deref_methods.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/explicit_deref_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/explicit_deref_methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2713ad4

Please sign in to comment.