Skip to content

Commit 050b77e

Browse files
tommilliganThibsG
authored andcommitted
Ignore when part of macro expansion
1 parent 6938380 commit 050b77e

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

clippy_lints/src/dereference.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::rustc::hir::{Expr, ExprKind, QPath};
22
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
33
use crate::rustc::{declare_tool_lint, lint_array};
4-
use crate::utils::span_lint_and_sugg;
4+
use crate::utils::{in_macro, span_lint_and_sugg};
55
use if_chain::if_chain;
66

77
/// **What it does:** Checks for explicit deref() or deref_mut() method calls.
@@ -15,8 +15,7 @@ use if_chain::if_chain;
1515
/// let c = a.deref_mut();
1616
///
1717
/// // excludes
18-
/// let e = d.deref().unwrap();
19-
/// let f = a.deref().unwrap();
18+
/// let e = d.unwrap().deref();
2019
/// ```
2120
declare_clippy_lint! {
2221
pub EXPLICIT_DEREF_METHOD,
@@ -34,6 +33,10 @@ impl LintPass for Pass {
3433

3534
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
3635
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr) {
36+
if in_macro(expr.span) {
37+
return;
38+
}
39+
3740
if_chain! {
3841
// if this is a method call
3942
if let ExprKind::MethodCall(ref method_name, _, ref args) = &expr.node;

tests/ui/dereference.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ fn main() {
4747
{
4848
let b: &mut str = &mut *a;
4949
}
50+
51+
{
52+
macro_rules! expr_deref { ($body:expr) => { $body.deref() } }
53+
let b: &str = expr_deref!(a);
54+
}
5055
}

0 commit comments

Comments
 (0)