Skip to content

Commit a657fcc

Browse files
committed
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.
1 parent b57d98b commit a657fcc

4 files changed

+28
-3
lines changed

clippy_lints/src/needless_arbitrary_self_type.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_with_applicability;
23
use rustc_ast::ast::{BindingMode, ByRef, Lifetime, Mutability, Param, PatKind, Path, TyKind};
34
use rustc_errors::Applicability;
45
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -80,7 +81,8 @@ fn check_param_inner(cx: &EarlyContext<'_>, path: &Path, span: Span, binding_mod
8081
applicability = Applicability::HasPlaceholders;
8182
"&'_ mut self".to_string()
8283
} else {
83-
format!("&{} mut self", &lifetime.ident.name)
84+
let lt_name = snippet_with_applicability(cx, lifetime.ident.span, "..", &mut applicability);
85+
format!("&{lt_name} mut self")
8486
}
8587
},
8688
(Mode::Ref(None), Mutability::Not) => "&self".to_string(),
@@ -89,7 +91,8 @@ fn check_param_inner(cx: &EarlyContext<'_>, path: &Path, span: Span, binding_mod
8991
applicability = Applicability::HasPlaceholders;
9092
"&'_ self".to_string()
9193
} else {
92-
format!("&{} self", &lifetime.ident.name)
94+
let lt_name = snippet_with_applicability(cx, lifetime.ident.span, "..", &mut applicability);
95+
format!("&{lt_name} self")
9396
}
9497
},
9598
(Mode::Value, Mutability::Mut) => "mut self".to_string(),

tests/ui/needless_arbitrary_self_type.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ impl ValType {
6464
}
6565
}
6666

67+
trait Foo<'r#struct> {
68+
fn f1(&'r#struct self) {}
69+
fn f2(&'r#struct mut self) {}
70+
}
71+
6772
fn main() {}

tests/ui/needless_arbitrary_self_type.rs

+5
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ impl ValType {
6464
}
6565
}
6666

67+
trait Foo<'r#struct> {
68+
fn f1(self: &'r#struct Self) {}
69+
fn f2(self: &'r#struct mut Self) {}
70+
}
71+
6772
fn main() {}

tests/ui/needless_arbitrary_self_type.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,17 @@ error: the type of the `self` parameter does not need to be arbitrary
3737
LL | pub fn mut_ref_bad_with_lifetime<'a>(self: &'a mut Self) {
3838
| ^^^^^^^^^^^^^^^^^^ help: consider to change this parameter to: `&'a mut self`
3939

40-
error: aborting due to 6 previous errors
40+
error: the type of the `self` parameter does not need to be arbitrary
41+
--> tests/ui/needless_arbitrary_self_type.rs:68:11
42+
|
43+
LL | fn f1(self: &'r#struct Self) {}
44+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider to change this parameter to: `&'r#struct self`
45+
46+
error: the type of the `self` parameter does not need to be arbitrary
47+
--> tests/ui/needless_arbitrary_self_type.rs:69:11
48+
|
49+
LL | fn f2(self: &'r#struct mut Self) {}
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider to change this parameter to: `&'r#struct mut self`
51+
52+
error: aborting due to 8 previous errors
4153

0 commit comments

Comments
 (0)