Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not expand macros in equatable_if_let suggestion #7788

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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