Use the full lifetime name in suggestions

Using `lifetime.ident.name` in suggestions will not output the raw
modifier. For example, `'r#struct` will be rendered as `'struct` which
would be incorrect.
This commit is contained in:
Samuel Tardieu 2024-12-29 23:58:42 +01:00
parent b57d98b00e
commit a657fcc89a
4 changed files with 28 additions and 3 deletions

View file

@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use rustc_ast::ast::{BindingMode, ByRef, Lifetime, Mutability, Param, PatKind, Path, TyKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass};
@ -80,7 +81,8 @@ fn check_param_inner(cx: &EarlyContext<'_>, path: &Path, span: Span, binding_mod
applicability = Applicability::HasPlaceholders;
"&'_ mut self".to_string()
} else {
format!("&{} mut self", &lifetime.ident.name)
let lt_name = snippet_with_applicability(cx, lifetime.ident.span, "..", &mut applicability);
format!("&{lt_name} mut self")
}
},
(Mode::Ref(None), Mutability::Not) => "&self".to_string(),
@ -89,7 +91,8 @@ fn check_param_inner(cx: &EarlyContext<'_>, path: &Path, span: Span, binding_mod
applicability = Applicability::HasPlaceholders;
"&'_ self".to_string()
} else {
format!("&{} self", &lifetime.ident.name)
let lt_name = snippet_with_applicability(cx, lifetime.ident.span, "..", &mut applicability);
format!("&{lt_name} self")
}
},
(Mode::Value, Mutability::Mut) => "mut self".to_string(),

View file

@ -64,4 +64,9 @@ impl ValType {
}
}
trait Foo<'r#struct> {
fn f1(&'r#struct self) {}
fn f2(&'r#struct mut self) {}
}
fn main() {}

View file

@ -64,4 +64,9 @@ impl ValType {
}
}
trait Foo<'r#struct> {
fn f1(self: &'r#struct Self) {}
fn f2(self: &'r#struct mut Self) {}
}
fn main() {}

View file

@ -37,5 +37,17 @@ error: the type of the `self` parameter does not need to be arbitrary
LL | pub fn mut_ref_bad_with_lifetime<'a>(self: &'a mut Self) {
| ^^^^^^^^^^^^^^^^^^ help: consider to change this parameter to: `&'a mut self`
error: aborting due to 6 previous errors
error: the type of the `self` parameter does not need to be arbitrary
--> tests/ui/needless_arbitrary_self_type.rs:68:11
|
LL | fn f1(self: &'r#struct Self) {}
| ^^^^^^^^^^^^^^^^^^^^^ help: consider to change this parameter to: `&'r#struct self`
error: the type of the `self` parameter does not need to be arbitrary
--> tests/ui/needless_arbitrary_self_type.rs:69:11
|
LL | fn f2(self: &'r#struct mut Self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider to change this parameter to: `&'r#struct mut self`
error: aborting due to 8 previous errors