Skip to content

Commit 7da5b80

Browse files
committed
Change skip trivial behaviour
1 parent 7ab0aaa commit 7da5b80

File tree

4 files changed

+51
-45
lines changed

4 files changed

+51
-45
lines changed

crates/ide/src/inlay_hints.rs

+42-36
Original file line numberDiff line numberDiff line change
@@ -258,36 +258,29 @@ fn lifetime_hints(
258258
return None;
259259
}
260260

261-
let skip_due_trivial_single = config.lifetime_elision_hints
262-
== LifetimeElisionHints::SkipTrivial
263-
&& (allocated_lifetimes.len() == 1)
264-
&& generic_param_list.as_ref().map_or(true, |it| it.lifetime_params().next().is_none());
265-
266-
if skip_due_trivial_single {
267-
cov_mark::hit!(lifetime_hints_single);
268-
return None;
269-
}
270-
271261
// apply hints
272262
// apply output if required
273-
match (&output, ret_type) {
274-
(Some(output_lt), Some(r)) => {
275-
if let Some(ty) = r.ty() {
276-
walk_ty(&ty, &mut |ty| match ty {
277-
ast::Type::RefType(ty) if ty.lifetime().is_none() => {
278-
if let Some(amp) = ty.amp_token() {
279-
acc.push(InlayHint {
280-
range: amp.text_range(),
281-
kind: InlayKind::LifetimeHint,
282-
label: output_lt.clone(),
283-
});
284-
}
263+
let mut is_trivial = true;
264+
if let (Some(output_lt), Some(r)) = (&output, ret_type) {
265+
if let Some(ty) = r.ty() {
266+
walk_ty(&ty, &mut |ty| match ty {
267+
ast::Type::RefType(ty) if ty.lifetime().is_none() => {
268+
if let Some(amp) = ty.amp_token() {
269+
is_trivial = false;
270+
acc.push(InlayHint {
271+
range: amp.text_range(),
272+
kind: InlayKind::LifetimeHint,
273+
label: output_lt.clone(),
274+
});
285275
}
286-
_ => (),
287-
})
288-
}
276+
}
277+
_ => (),
278+
})
289279
}
290-
_ => (),
280+
}
281+
282+
if config.lifetime_elision_hints == LifetimeElisionHints::SkipTrivial && is_trivial {
283+
return None;
291284
}
292285

293286
let mut idx = match &self_param {
@@ -2061,6 +2054,9 @@ fn nested_out(a: &()) -> & &X< &()>{}
20612054
//^'0 ^'0 ^'0 ^'0
20622055
20632056
impl () {
2057+
fn foo(&self) {}
2058+
// ^^^<'0>
2059+
// ^'0
20642060
fn foo(&self) -> &() {}
20652061
// ^^^<'0>
20662062
// ^'0 ^'0
@@ -2085,26 +2081,36 @@ fn nested_in<'named>(named: & &X< &()>) {}
20852081
}
20862082

20872083
#[test]
2088-
fn hints_lifetimes_skingle_skip() {
2089-
cov_mark::check!(lifetime_hints_single);
2084+
fn hints_lifetimes_trivial_skip() {
20902085
check_with_config(
20912086
InlayHintsConfig {
20922087
lifetime_elision_hints: LifetimeElisionHints::SkipTrivial,
20932088
..TEST_CONFIG
20942089
},
20952090
r#"
2096-
fn single(a: &()) -> &() {}
2097-
2098-
fn double(a: &(), b: &()) {}
2099-
// ^^^^^^<'0, '1>
2100-
// ^'0 ^'1
2091+
fn no_gpl(a: &()) {}
2092+
fn empty_gpl<>(a: &()) {}
2093+
fn partial<'b>(a: &(), b: &'b ()) {}
21012094
fn partial<'a>(a: &'a (), b: &()) {}
2102-
//^'0, $ ^'0
2103-
fn partial2<'a>(a: &'a ()) -> &() {}
2104-
//^'a
2095+
2096+
fn single_ret(a: &()) -> &() {}
2097+
// ^^^^^^^^^^<'0>
2098+
// ^'0 ^'0
2099+
fn full_mul(a: &(), b: &()) {}
2100+
2101+
fn foo<'c>(a: &'c ()) -> &() {}
2102+
// ^'c
2103+
2104+
fn nested_in(a: & &X< &()>) {}
2105+
fn nested_out(a: &()) -> & &X< &()>{}
2106+
// ^^^^^^^^^^<'0>
2107+
//^'0 ^'0 ^'0 ^'0
21052108
21062109
impl () {
2110+
fn foo(&self) {}
21072111
fn foo(&self) -> &() {}
2112+
// ^^^<'0>
2113+
// ^'0 ^'0
21082114
fn foo(&self, a: &()) -> &() {}
21092115
// ^^^<'0, '1>
21102116
// ^'0 ^'1 ^'0

crates/rust-analyzer/src/config.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ config_data! {
258258
inlayHints_closureReturnTypeHints: bool = "false",
259259
/// Whether to show inlay type hints for elided lifetimes in function signatures.
260260
inlayHints_lifetimeElisionHints: LifetimeElisionDef = "\"never\"",
261-
/// Whether to show prefer using parameter names as the name for elided lifetime hints.
262-
inlayHints_paramNamesForLifetimeElisionHints: bool = "false",
261+
/// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
262+
inlayHints_lifetimeElisionHints_useParameterNames: bool = "false",
263263
/// Whether to hide inlay hints for constructors.
264264
inlayHints_hideNamedConstructorHints: bool = "false",
265265

@@ -868,7 +868,7 @@ impl Config {
868868
hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints,
869869
param_names_for_lifetime_elision_hints: self
870870
.data
871-
.inlayHints_paramNamesForLifetimeElisionHints,
871+
.inlayHints_lifetimeElisionHints_useParameterNames,
872872
max_length: self.data.inlayHints_maxLength,
873873
}
874874
}
@@ -1406,7 +1406,7 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
14061406
"enumDescriptions": [
14071407
"Always show lifetime elision hints.",
14081408
"Never show lifetime elision hints.",
1409-
"Always show lifetime elision hints but skip them for trivial single input to output mapping."
1409+
"Only show lifetime elision hints if a return type is involved."
14101410
],
14111411
},
14121412
_ => panic!("missing entry for {}: {}", ty, default),

docs/user/generated_config.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ Whether to show inlay type hints for return types of closures with blocks.
383383
--
384384
Whether to show inlay type hints for elided lifetimes in function signatures.
385385
--
386-
[[rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints]]rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints (default: `false`)::
386+
[[rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames]]rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames (default: `false`)::
387387
+
388388
--
389-
Whether to show prefer using parameter names as the name for elided lifetime hints.
389+
Whether to prefer using parameter names as the name for elided lifetime hints if possible.
390390
--
391391
[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`)::
392392
+

editors/code/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,11 @@
812812
"enumDescriptions": [
813813
"Always show lifetime elision hints.",
814814
"Never show lifetime elision hints.",
815-
"Always show lifetime elision hints but skip them for trivial single input to output mapping."
815+
"Only show lifetime elision hints if a return type is involved."
816816
]
817817
},
818-
"rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints": {
819-
"markdownDescription": "Whether to show prefer using parameter names as the name for elided lifetime hints.",
818+
"rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames": {
819+
"markdownDescription": "Whether to prefer using parameter names as the name for elided lifetime hints if possible.",
820820
"default": false,
821821
"type": "boolean"
822822
},

0 commit comments

Comments
 (0)