about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-10-13 12:04:57 +0000
committerbors <bors@rust-lang.org>2021-10-13 12:04:57 +0000
commit57dc0343bdd05ed13aa2704ca6a873843143f5a9 (patch)
treebf0626683d57c28e7aac88f65bf1cb37356e89c6
parent3d9c4a655b04e9616ded8374dc61cb9108f4ae06 (diff)
parent28fb591b6e681493b6ba3917146fbfe410dd45d0 (diff)
downloadrust-57dc0343bdd05ed13aa2704ca6a873843143f5a9.tar.gz
rust-57dc0343bdd05ed13aa2704ca6a873843143f5a9.zip
Auto merge of #7788 - flip1995:eq_if_let_sugg, r=giraffate
Do not expand macros in equatable_if_let suggestion

Fixes #7781

Let's use Hacktoberfest as a motivation to start contributing PRs myself again :)

changelog: [`equatable_if_let`]: No longer expands macros in the suggestion
-rw-r--r--clippy_lints/src/equatable_if_let.rs8
-rw-r--r--tests/ui/equatable_if_let.fixed9
-rw-r--r--tests/ui/equatable_if_let.rs9
-rw-r--r--tests/ui/equatable_if_let.stderr8
4 files changed, 29 insertions, 5 deletions
diff --git a/clippy_lints/src/equatable_if_let.rs b/clippy_lints/src/equatable_if_let.rs
index 0c6ba91c943..e8b1d6f6eda 100644
--- a/clippy_lints/src/equatable_if_let.rs
+++ b/clippy_lints/src/equatable_if_let.rs
@@ -1,5 +1,5 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
-use clippy_utils::source::snippet_with_applicability;
+use clippy_utils::source::snippet_with_context;
 use clippy_utils::ty::implements_trait;
 use if_chain::if_chain;
 use rustc_errors::Applicability;
@@ -77,9 +77,9 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
                 let pat_str = match pat.kind {
                     PatKind::Struct(..) => format!(
                         "({})",
-                        snippet_with_applicability(cx, pat.span, "..", &mut applicability),
+                        snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0,
                     ),
-                    _ => snippet_with_applicability(cx, pat.span, "..", &mut applicability).to_string(),
+                    _ => snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(),
                 };
                 span_lint_and_sugg(
                     cx,
@@ -89,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
                     "try",
                     format!(
                         "{} == {}",
-                        snippet_with_applicability(cx, exp.span, "..", &mut applicability),
+                        snippet_with_context(cx, exp.span, expr.span.ctxt(), "..", &mut applicability).0,
                         pat_str,
                     ),
                     applicability,
diff --git a/tests/ui/equatable_if_let.fixed b/tests/ui/equatable_if_let.fixed
index ba72cc237b4..88918d9671e 100644
--- a/tests/ui/equatable_if_let.fixed
+++ b/tests/ui/equatable_if_let.fixed
@@ -66,4 +66,13 @@ fn main() {
     if g == NotStructuralEq::A {}
     if let Some(NotPartialEq::A) = Some(f) {}
     if Some(g) == Some(NotStructuralEq::A) {}
+
+    macro_rules! m1 {
+        (x) => {
+            "abc"
+        };
+    }
+    if "abc" == m1!(x) {
+        println!("OK");
+    }
 }
diff --git a/tests/ui/equatable_if_let.rs b/tests/ui/equatable_if_let.rs
index 12526ca193d..9a7ab75ef45 100644
--- a/tests/ui/equatable_if_let.rs
+++ b/tests/ui/equatable_if_let.rs
@@ -66,4 +66,13 @@ fn main() {
     if let NotStructuralEq::A = g {}
     if let Some(NotPartialEq::A) = Some(f) {}
     if let Some(NotStructuralEq::A) = Some(g) {}
+
+    macro_rules! m1 {
+        (x) => {
+            "abc"
+        };
+    }
+    if let m1!(x) = "abc" {
+        println!("OK");
+    }
 }
diff --git a/tests/ui/equatable_if_let.stderr b/tests/ui/equatable_if_let.stderr
index 79ef919384d..760ff88f448 100644
--- a/tests/ui/equatable_if_let.stderr
+++ b/tests/ui/equatable_if_let.stderr
@@ -60,5 +60,11 @@ error: this pattern matching can be expressed using equality
 LL |     if let Some(NotStructuralEq::A) = Some(g) {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(g) == Some(NotStructuralEq::A)`
 
-error: aborting due to 10 previous errors
+error: this pattern matching can be expressed using equality
+  --> $DIR/equatable_if_let.rs:75:8
+   |
+LL |     if let m1!(x) = "abc" {
+   |        ^^^^^^^^^^^^^^^^^^ help: try: `"abc" == m1!(x)`
+
+error: aborting due to 11 previous errors