Skip to content

Commit 4882141

Browse files
authored
Fix: cmp_owned wrongly unmangled macros (rust-lang#16331)
Closes rust-lang#16322 changelog: [`cmp_owned`] fix wrongly unmangled macros
2 parents b527468 + aa4ec54 commit 4882141

File tree

4 files changed

+84
-34
lines changed

4 files changed

+84
-34
lines changed

clippy_lints/src/operators/cmp_owned.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::res::MaybeQPath;
3-
use clippy_utils::source::snippet;
3+
use clippy_utils::source::snippet_with_context;
44
use clippy_utils::ty::{implements_trait, is_copy};
55
use rustc_errors::Applicability;
66
use rustc_hir::{BinOpKind, Expr, ExprKind, UnOp};
@@ -94,51 +94,37 @@ fn check_op(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool)
9494
return;
9595
}
9696

97-
let arg_snip = snippet(cx, arg_span, "..");
98-
let expr_snip;
99-
let eq_impl;
100-
if with_deref.is_implemented() && !arg_ty.peel_refs().is_str() {
101-
expr_snip = format!("*{arg_snip}");
102-
eq_impl = with_deref;
97+
let mut applicability = Applicability::MachineApplicable;
98+
let (arg_snip, _) = snippet_with_context(cx, arg_span, expr.span.ctxt(), "..", &mut applicability);
99+
let (expr_snip, eq_impl) = if with_deref.is_implemented() && !arg_ty.peel_refs().is_str() {
100+
(format!("*{arg_snip}"), with_deref)
103101
} else {
104-
expr_snip = arg_snip.to_string();
105-
eq_impl = without_deref;
106-
}
102+
(arg_snip.to_string(), without_deref)
103+
};
107104

108-
let span;
109-
let hint;
110-
if (eq_impl.ty_eq_other && left) || (eq_impl.other_eq_ty && !left) {
111-
span = expr.span;
112-
hint = expr_snip;
105+
let (span, hint) = if (eq_impl.ty_eq_other && left) || (eq_impl.other_eq_ty && !left) {
106+
(expr.span, expr_snip)
113107
} else {
114-
span = expr.span.to(other.span);
108+
let span = expr.span.to(other.span);
115109

116110
let cmp_span = if other.span < expr.span {
117111
other.span.between(expr.span)
118112
} else {
119113
expr.span.between(other.span)
120114
};
115+
116+
let (cmp_snippet, _) = snippet_with_context(cx, cmp_span, expr.span.ctxt(), "..", &mut applicability);
117+
let (other_snippet, _) =
118+
snippet_with_context(cx, other.span, expr.span.ctxt(), "..", &mut applicability);
119+
121120
if eq_impl.ty_eq_other {
122-
hint = format!(
123-
"{expr_snip}{}{}",
124-
snippet(cx, cmp_span, ".."),
125-
snippet(cx, other.span, "..")
126-
);
121+
(span, format!("{expr_snip}{cmp_snippet}{other_snippet}"))
127122
} else {
128-
hint = format!(
129-
"{}{}{expr_snip}",
130-
snippet(cx, other.span, ".."),
131-
snippet(cx, cmp_span, "..")
132-
);
123+
(span, format!("{other_snippet}{cmp_snippet}{expr_snip}"))
133124
}
134-
}
125+
};
135126

136-
diag.span_suggestion(
137-
span,
138-
"try",
139-
hint,
140-
Applicability::MachineApplicable, // snippet
141-
);
127+
diag.span_suggestion(span, "try", hint, applicability);
142128
},
143129
);
144130
}

tests/ui/cmp_owned/with_suggestion.fixed

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,32 @@ fn issue_8103() {
8383
let _ = foo1 == foo2;
8484
//~^ cmp_owned
8585
}
86+
87+
macro_rules! issue16322_macro_generator {
88+
($locale:ident) => {
89+
mod $locale {
90+
macro_rules! _make {
91+
($token:tt) => {
92+
stringify!($token)
93+
};
94+
}
95+
96+
pub(crate) use _make;
97+
}
98+
99+
macro_rules! t {
100+
($token:tt) => {
101+
crate::$locale::_make!($token)
102+
};
103+
}
104+
};
105+
}
106+
107+
issue16322_macro_generator!(de);
108+
109+
fn issue16322(item: String) {
110+
if item == t!(frohes_neu_Jahr) {
111+
//~^ cmp_owned
112+
println!("Ja!");
113+
}
114+
}

tests/ui/cmp_owned/with_suggestion.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,32 @@ fn issue_8103() {
8383
let _ = foo1 == foo2.to_owned();
8484
//~^ cmp_owned
8585
}
86+
87+
macro_rules! issue16322_macro_generator {
88+
($locale:ident) => {
89+
mod $locale {
90+
macro_rules! _make {
91+
($token:tt) => {
92+
stringify!($token)
93+
};
94+
}
95+
96+
pub(crate) use _make;
97+
}
98+
99+
macro_rules! t {
100+
($token:tt) => {
101+
crate::$locale::_make!($token)
102+
};
103+
}
104+
};
105+
}
106+
107+
issue16322_macro_generator!(de);
108+
109+
fn issue16322(item: String) {
110+
if item == t!(frohes_neu_Jahr).to_string() {
111+
//~^ cmp_owned
112+
println!("Ja!");
113+
}
114+
}

tests/ui/cmp_owned/with_suggestion.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,11 @@ error: this creates an owned instance just for comparison
4949
LL | let _ = foo1 == foo2.to_owned();
5050
| ^^^^^^^^^^^^^^^ help: try: `foo2`
5151

52-
error: aborting due to 8 previous errors
52+
error: this creates an owned instance just for comparison
53+
--> tests/ui/cmp_owned/with_suggestion.rs:110:16
54+
|
55+
LL | if item == t!(frohes_neu_Jahr).to_string() {
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t!(frohes_neu_Jahr)`
57+
58+
error: aborting due to 9 previous errors
5359

0 commit comments

Comments
 (0)