Change skip trivial behaviour

This commit is contained in:
Lukas Wirth 2022-03-19 20:12:14 +01:00
parent 7ab0aaa82a
commit 7da5b80f25
4 changed files with 51 additions and 45 deletions

View file

@ -258,24 +258,15 @@ fn lifetime_hints(
return None; return None;
} }
let skip_due_trivial_single = config.lifetime_elision_hints
== LifetimeElisionHints::SkipTrivial
&& (allocated_lifetimes.len() == 1)
&& generic_param_list.as_ref().map_or(true, |it| it.lifetime_params().next().is_none());
if skip_due_trivial_single {
cov_mark::hit!(lifetime_hints_single);
return None;
}
// apply hints // apply hints
// apply output if required // apply output if required
match (&output, ret_type) { let mut is_trivial = true;
(Some(output_lt), Some(r)) => { if let (Some(output_lt), Some(r)) = (&output, ret_type) {
if let Some(ty) = r.ty() { if let Some(ty) = r.ty() {
walk_ty(&ty, &mut |ty| match ty { walk_ty(&ty, &mut |ty| match ty {
ast::Type::RefType(ty) if ty.lifetime().is_none() => { ast::Type::RefType(ty) if ty.lifetime().is_none() => {
if let Some(amp) = ty.amp_token() { if let Some(amp) = ty.amp_token() {
is_trivial = false;
acc.push(InlayHint { acc.push(InlayHint {
range: amp.text_range(), range: amp.text_range(),
kind: InlayKind::LifetimeHint, kind: InlayKind::LifetimeHint,
@ -287,7 +278,9 @@ fn lifetime_hints(
}) })
} }
} }
_ => (),
if config.lifetime_elision_hints == LifetimeElisionHints::SkipTrivial && is_trivial {
return None;
} }
let mut idx = match &self_param { let mut idx = match &self_param {
@ -2061,6 +2054,9 @@ fn nested_out(a: &()) -> & &X< &()>{}
//^'0 ^'0 ^'0 ^'0 //^'0 ^'0 ^'0 ^'0
impl () { impl () {
fn foo(&self) {}
// ^^^<'0>
// ^'0
fn foo(&self) -> &() {} fn foo(&self) -> &() {}
// ^^^<'0> // ^^^<'0>
// ^'0 ^'0 // ^'0 ^'0
@ -2085,26 +2081,36 @@ fn nested_in<'named>(named: & &X< &()>) {}
} }
#[test] #[test]
fn hints_lifetimes_skingle_skip() { fn hints_lifetimes_trivial_skip() {
cov_mark::check!(lifetime_hints_single);
check_with_config( check_with_config(
InlayHintsConfig { InlayHintsConfig {
lifetime_elision_hints: LifetimeElisionHints::SkipTrivial, lifetime_elision_hints: LifetimeElisionHints::SkipTrivial,
..TEST_CONFIG ..TEST_CONFIG
}, },
r#" r#"
fn single(a: &()) -> &() {} fn no_gpl(a: &()) {}
fn empty_gpl<>(a: &()) {}
fn double(a: &(), b: &()) {} fn partial<'b>(a: &(), b: &'b ()) {}
// ^^^^^^<'0, '1>
// ^'0 ^'1
fn partial<'a>(a: &'a (), b: &()) {} fn partial<'a>(a: &'a (), b: &()) {}
//^'0, $ ^'0
fn partial2<'a>(a: &'a ()) -> &() {} fn single_ret(a: &()) -> &() {}
//^'a // ^^^^^^^^^^<'0>
// ^'0 ^'0
fn full_mul(a: &(), b: &()) {}
fn foo<'c>(a: &'c ()) -> &() {}
// ^'c
fn nested_in(a: & &X< &()>) {}
fn nested_out(a: &()) -> & &X< &()>{}
// ^^^^^^^^^^<'0>
//^'0 ^'0 ^'0 ^'0
impl () { impl () {
fn foo(&self) {}
fn foo(&self) -> &() {} fn foo(&self) -> &() {}
// ^^^<'0>
// ^'0 ^'0
fn foo(&self, a: &()) -> &() {} fn foo(&self, a: &()) -> &() {}
// ^^^<'0, '1> // ^^^<'0, '1>
// ^'0 ^'1 ^'0 // ^'0 ^'1 ^'0

View file

@ -258,8 +258,8 @@ config_data! {
inlayHints_closureReturnTypeHints: bool = "false", inlayHints_closureReturnTypeHints: bool = "false",
/// Whether to show inlay type hints for elided lifetimes in function signatures. /// Whether to show inlay type hints for elided lifetimes in function signatures.
inlayHints_lifetimeElisionHints: LifetimeElisionDef = "\"never\"", inlayHints_lifetimeElisionHints: LifetimeElisionDef = "\"never\"",
/// Whether to show prefer using parameter names as the name for elided lifetime hints. /// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
inlayHints_paramNamesForLifetimeElisionHints: bool = "false", inlayHints_lifetimeElisionHints_useParameterNames: bool = "false",
/// Whether to hide inlay hints for constructors. /// Whether to hide inlay hints for constructors.
inlayHints_hideNamedConstructorHints: bool = "false", inlayHints_hideNamedConstructorHints: bool = "false",
@ -868,7 +868,7 @@ impl Config {
hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints, hide_named_constructor_hints: self.data.inlayHints_hideNamedConstructorHints,
param_names_for_lifetime_elision_hints: self param_names_for_lifetime_elision_hints: self
.data .data
.inlayHints_paramNamesForLifetimeElisionHints, .inlayHints_lifetimeElisionHints_useParameterNames,
max_length: self.data.inlayHints_maxLength, max_length: self.data.inlayHints_maxLength,
} }
} }
@ -1406,7 +1406,7 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
"enumDescriptions": [ "enumDescriptions": [
"Always show lifetime elision hints.", "Always show lifetime elision hints.",
"Never show lifetime elision hints.", "Never show lifetime elision hints.",
"Always show lifetime elision hints but skip them for trivial single input to output mapping." "Only show lifetime elision hints if a return type is involved."
], ],
}, },
_ => panic!("missing entry for {}: {}", ty, default), _ => panic!("missing entry for {}: {}", ty, default),

View file

@ -383,10 +383,10 @@ Whether to show inlay type hints for return types of closures with blocks.
-- --
Whether to show inlay type hints for elided lifetimes in function signatures. Whether to show inlay type hints for elided lifetimes in function signatures.
-- --
[[rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints]]rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints (default: `false`):: [[rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames]]rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames (default: `false`)::
+ +
-- --
Whether to show prefer using parameter names as the name for elided lifetime hints. Whether to prefer using parameter names as the name for elided lifetime hints if possible.
-- --
[[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`):: [[rust-analyzer.inlayHints.hideNamedConstructorHints]]rust-analyzer.inlayHints.hideNamedConstructorHints (default: `false`)::
+ +

View file

@ -812,11 +812,11 @@
"enumDescriptions": [ "enumDescriptions": [
"Always show lifetime elision hints.", "Always show lifetime elision hints.",
"Never show lifetime elision hints.", "Never show lifetime elision hints.",
"Always show lifetime elision hints but skip them for trivial single input to output mapping." "Only show lifetime elision hints if a return type is involved."
] ]
}, },
"rust-analyzer.inlayHints.paramNamesForLifetimeElisionHints": { "rust-analyzer.inlayHints.lifetimeElisionHints.useParameterNames": {
"markdownDescription": "Whether to show prefer using parameter names as the name for elided lifetime hints.", "markdownDescription": "Whether to prefer using parameter names as the name for elided lifetime hints if possible.",
"default": false, "default": false,
"type": "boolean" "type": "boolean"
}, },