fix: str_to_string wrongly unmangled macros

This commit is contained in:
Linshu Yang
2025-12-20 18:53:58 +00:00
parent 34fab5cd37
commit 4c4b2a1dbe
4 changed files with 38 additions and 3 deletions
+3 -2
View File
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::res::{MaybeDef, MaybeQPath};
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_context};
use clippy_utils::{
SpanlessEq, get_expr_use_or_unification_node, get_parent_expr, is_lint_allowed, method_calls, peel_blocks, sym,
};
@@ -404,7 +404,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
"`to_string()` called on a `&str`",
|diag| {
let mut applicability = Applicability::MachineApplicable;
let snippet = snippet_with_applicability(cx, self_arg.span, "..", &mut applicability);
let (snippet, _) =
snippet_with_context(cx, self_arg.span, expr.span.ctxt(), "..", &mut applicability);
diag.span_suggestion(expr.span, "try", format!("{snippet}.to_owned()"), applicability);
},
);
+14
View File
@@ -8,3 +8,17 @@ fn main() {
msg.to_owned();
//~^ str_to_string
}
fn issue16271(key: &[u8]) {
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}
let _value = t!(str::from_utf8(key)).to_owned();
//~^ str_to_string
}
+14
View File
@@ -8,3 +8,17 @@ fn main() {
msg.to_string();
//~^ str_to_string
}
fn issue16271(key: &[u8]) {
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}
let _value = t!(str::from_utf8(key)).to_string();
//~^ str_to_string
}
+7 -1
View File
@@ -13,5 +13,11 @@ error: `to_string()` called on a `&str`
LL | msg.to_string();
| ^^^^^^^^^^^^^^^ help: try: `msg.to_owned()`
error: aborting due to 2 previous errors
error: `to_string()` called on a `&str`
--> tests/ui/str_to_string.rs:22:18
|
LL | let _value = t!(str::from_utf8(key)).to_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t!(str::from_utf8(key)).to_owned()`
error: aborting due to 3 previous errors