Skip to content

Commit

Permalink
Auto merge of #7788 - flip1995:eq_if_let_sugg, r=giraffate
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Oct 13, 2021
2 parents 3d9c4a6 + 28fb591 commit 9e835e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/equatable_if_let.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/equatable_if_let.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
9 changes: 9 additions & 0 deletions tests/ui/equatable_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
8 changes: 7 additions & 1 deletion tests/ui/equatable_if_let.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9e835e0

Please sign in to comment.