Properly lint macro arguments for explicit_deref_methods

This commit is contained in:
Jason Newcomb 2021-03-13 08:27:42 -05:00
parent 704f7a8e50
commit 2713ad4342
No known key found for this signature in database
GPG Key ID: DA59E8643A37ED06
4 changed files with 14 additions and 4 deletions

View File

@ -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<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 {

View File

@ -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);

View File

@ -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);

View File

@ -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