Skip to content

Commit 57dc034

Browse files
committed
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
2 parents 3d9c4a6 + 28fb591 commit 57dc034

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

clippy_lints/src/equatable_if_let.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::snippet_with_applicability;
2+
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::ty::implements_trait;
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
@@ -77,9 +77,9 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
7777
let pat_str = match pat.kind {
7878
PatKind::Struct(..) => format!(
7979
"({})",
80-
snippet_with_applicability(cx, pat.span, "..", &mut applicability),
80+
snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0,
8181
),
82-
_ => snippet_with_applicability(cx, pat.span, "..", &mut applicability).to_string(),
82+
_ => snippet_with_context(cx, pat.span, expr.span.ctxt(), "..", &mut applicability).0.to_string(),
8383
};
8484
span_lint_and_sugg(
8585
cx,
@@ -89,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternEquality {
8989
"try",
9090
format!(
9191
"{} == {}",
92-
snippet_with_applicability(cx, exp.span, "..", &mut applicability),
92+
snippet_with_context(cx, exp.span, expr.span.ctxt(), "..", &mut applicability).0,
9393
pat_str,
9494
),
9595
applicability,

tests/ui/equatable_if_let.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,13 @@ fn main() {
6666
if g == NotStructuralEq::A {}
6767
if let Some(NotPartialEq::A) = Some(f) {}
6868
if Some(g) == Some(NotStructuralEq::A) {}
69+
70+
macro_rules! m1 {
71+
(x) => {
72+
"abc"
73+
};
74+
}
75+
if "abc" == m1!(x) {
76+
println!("OK");
77+
}
6978
}

tests/ui/equatable_if_let.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,13 @@ fn main() {
6666
if let NotStructuralEq::A = g {}
6767
if let Some(NotPartialEq::A) = Some(f) {}
6868
if let Some(NotStructuralEq::A) = Some(g) {}
69+
70+
macro_rules! m1 {
71+
(x) => {
72+
"abc"
73+
};
74+
}
75+
if let m1!(x) = "abc" {
76+
println!("OK");
77+
}
6978
}

tests/ui/equatable_if_let.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,11 @@ error: this pattern matching can be expressed using equality
6060
LL | if let Some(NotStructuralEq::A) = Some(g) {}
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(g) == Some(NotStructuralEq::A)`
6262

63-
error: aborting due to 10 previous errors
63+
error: this pattern matching can be expressed using equality
64+
--> $DIR/equatable_if_let.rs:75:8
65+
|
66+
LL | if let m1!(x) = "abc" {
67+
| ^^^^^^^^^^^^^^^^^^ help: try: `"abc" == m1!(x)`
68+
69+
error: aborting due to 11 previous errors
6470

0 commit comments

Comments
 (0)