Skip to content

Commit

Permalink
Use the full lifetime name in suggestions (#13907)
Browse files Browse the repository at this point in the history
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.

Fix #13899

changelog: [`needless_arbitrary_self_type`]: use the raw lifetime name
in suggestions
  • Loading branch information
Manishearth authored Dec 30, 2024
2 parents 2aea7a0 + a657fcc commit 7a834b5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
7 changes: 5 additions & 2 deletions clippy_lints/src/needless_arbitrary_self_type.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/needless_arbitrary_self_type.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ impl ValType {
}
}

trait Foo<'r#struct> {
fn f1(&'r#struct self) {}
fn f2(&'r#struct mut self) {}
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/needless_arbitrary_self_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
14 changes: 13 additions & 1 deletion tests/ui/needless_arbitrary_self_type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7a834b5

Please sign in to comment.