Skip to content

Commit 9ceaff9

Browse files
committed
Auto merge of rust-lang#12406 - harpsword:fix-add-inlayHints-closures-without-block, r=Veykril
fix: add an option to show inlay hint for return type of closures wit… fix rust-lang#12321
2 parents bd0c234 + 5550954 commit 9ceaff9

File tree

6 files changed

+84
-16
lines changed

6 files changed

+84
-16
lines changed

crates/ide/src/inlay_hints.rs

+34-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct InlayHintsConfig {
2121
pub parameter_hints: bool,
2222
pub chaining_hints: bool,
2323
pub reborrow_hints: ReborrowHints,
24-
pub closure_return_type_hints: bool,
24+
pub closure_return_type_hints: ClosureReturnTypeHints,
2525
pub binding_mode_hints: bool,
2626
pub lifetime_elision_hints: LifetimeElisionHints,
2727
pub param_names_for_lifetime_elision_hints: bool,
@@ -31,6 +31,13 @@ pub struct InlayHintsConfig {
3131
pub closing_brace_hints_min_lines: Option<usize>,
3232
}
3333

34+
#[derive(Clone, Debug, PartialEq, Eq)]
35+
pub enum ClosureReturnTypeHints {
36+
Always,
37+
WithBlock,
38+
Never,
39+
}
40+
3441
#[derive(Clone, Debug, PartialEq, Eq)]
3542
pub enum LifetimeElisionHints {
3643
Always,
@@ -86,7 +93,7 @@ pub enum InlayTooltip {
8693
//
8794
// Optionally, one can enable additional hints for
8895
//
89-
// * return types of closure expressions with blocks
96+
// * return types of closure expressions
9097
// * elided lifetimes
9198
// * compiler inserted reborrows
9299
//
@@ -460,15 +467,17 @@ fn closure_ret_hints(
460467
file_id: FileId,
461468
closure: ast::ClosureExpr,
462469
) -> Option<()> {
463-
if !config.closure_return_type_hints {
470+
if config.closure_return_type_hints == ClosureReturnTypeHints::Never {
464471
return None;
465472
}
466473

467474
if closure.ret_type().is_some() {
468475
return None;
469476
}
470477

471-
if !closure_has_block_body(&closure) {
478+
if !closure_has_block_body(&closure)
479+
&& config.closure_return_type_hints == ClosureReturnTypeHints::WithBlock
480+
{
472481
return None;
473482
}
474483

@@ -1092,13 +1101,15 @@ mod tests {
10921101
use crate::inlay_hints::ReborrowHints;
10931102
use crate::{fixture, inlay_hints::InlayHintsConfig, LifetimeElisionHints};
10941103

1104+
use super::ClosureReturnTypeHints;
1105+
10951106
const DISABLED_CONFIG: InlayHintsConfig = InlayHintsConfig {
10961107
render_colons: false,
10971108
type_hints: false,
10981109
parameter_hints: false,
10991110
chaining_hints: false,
11001111
lifetime_elision_hints: LifetimeElisionHints::Never,
1101-
closure_return_type_hints: false,
1112+
closure_return_type_hints: ClosureReturnTypeHints::Never,
11021113
reborrow_hints: ReborrowHints::Always,
11031114
binding_mode_hints: false,
11041115
hide_named_constructor_hints: false,
@@ -1112,7 +1123,7 @@ mod tests {
11121123
parameter_hints: true,
11131124
chaining_hints: true,
11141125
reborrow_hints: ReborrowHints::Always,
1115-
closure_return_type_hints: true,
1126+
closure_return_type_hints: ClosureReturnTypeHints::WithBlock,
11161127
binding_mode_hints: true,
11171128
lifetime_elision_hints: LifetimeElisionHints::Always,
11181129
..DISABLED_CONFIG
@@ -2054,6 +2065,23 @@ fn main() {
20542065
);
20552066
}
20562067

2068+
#[test]
2069+
fn return_type_hints_for_closure_without_block() {
2070+
check_with_config(
2071+
InlayHintsConfig {
2072+
closure_return_type_hints: ClosureReturnTypeHints::Always,
2073+
..DISABLED_CONFIG
2074+
},
2075+
r#"
2076+
fn main() {
2077+
let a = || { 0 };
2078+
//^^ i32
2079+
let b = || 0;
2080+
//^^ i32
2081+
}"#,
2082+
);
2083+
}
2084+
20572085
#[test]
20582086
fn skip_closure_type_hints() {
20592087
check_with_config(

crates/ide/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ pub use crate::{
8181
highlight_related::{HighlightRelatedConfig, HighlightedRange},
8282
hover::{HoverAction, HoverConfig, HoverDocFormat, HoverGotoTypeData, HoverResult},
8383
inlay_hints::{
84-
InlayHint, InlayHintsConfig, InlayKind, InlayTooltip, LifetimeElisionHints, ReborrowHints,
84+
ClosureReturnTypeHints, InlayHint, InlayHintsConfig, InlayKind, InlayTooltip,
85+
LifetimeElisionHints, ReborrowHints,
8586
},
8687
join_lines::JoinLinesConfig,
8788
markup::Markup,

crates/ide/src/static_index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl StaticIndex<'_> {
109109
type_hints: true,
110110
parameter_hints: true,
111111
chaining_hints: true,
112-
closure_return_type_hints: true,
112+
closure_return_type_hints: crate::ClosureReturnTypeHints::WithBlock,
113113
lifetime_elision_hints: crate::LifetimeElisionHints::Never,
114114
reborrow_hints: crate::ReborrowHints::Never,
115115
hide_named_constructor_hints: false,

crates/rust-analyzer/src/config.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ config_data! {
264264
/// Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1
265265
/// to always show them).
266266
inlayHints_closingBraceHints_minLines: usize = "25",
267-
/// Whether to show inlay type hints for return types of closures with blocks.
268-
inlayHints_closureReturnTypeHints_enable: bool = "false",
267+
/// Whether to show inlay type hints for return types of closures.
268+
inlayHints_closureReturnTypeHints_enable: ClosureReturnTypeHintsDef = "\"never\"",
269269
/// Whether to show inlay type hints for elided lifetimes in function signatures.
270270
inlayHints_lifetimeElisionHints_enable: LifetimeElisionDef = "\"never\"",
271271
/// Whether to prefer using parameter names as the name for elided lifetime hints if possible.
@@ -1014,7 +1014,11 @@ impl Config {
10141014
type_hints: self.data.inlayHints_typeHints_enable,
10151015
parameter_hints: self.data.inlayHints_parameterHints_enable,
10161016
chaining_hints: self.data.inlayHints_chainingHints_enable,
1017-
closure_return_type_hints: self.data.inlayHints_closureReturnTypeHints_enable,
1017+
closure_return_type_hints: match self.data.inlayHints_closureReturnTypeHints_enable {
1018+
ClosureReturnTypeHintsDef::Always => ide::ClosureReturnTypeHints::Always,
1019+
ClosureReturnTypeHintsDef::Never => ide::ClosureReturnTypeHints::Never,
1020+
ClosureReturnTypeHintsDef::WithBlock => ide::ClosureReturnTypeHints::WithBlock,
1021+
},
10181022
lifetime_elision_hints: match self.data.inlayHints_lifetimeElisionHints_enable {
10191023
LifetimeElisionDef::Always => ide::LifetimeElisionHints::Always,
10201024
LifetimeElisionDef::Never => ide::LifetimeElisionHints::Never,
@@ -1342,6 +1346,7 @@ mod de_unit_v {
13421346
named_unit_variant!(all);
13431347
named_unit_variant!(skip_trivial);
13441348
named_unit_variant!(mutable);
1349+
named_unit_variant!(with_block);
13451350
}
13461351

13471352
#[derive(Deserialize, Debug, Clone, Copy)]
@@ -1454,6 +1459,17 @@ enum LifetimeElisionDef {
14541459
SkipTrivial,
14551460
}
14561461

1462+
#[derive(Deserialize, Debug, Clone)]
1463+
#[serde(untagged)]
1464+
enum ClosureReturnTypeHintsDef {
1465+
#[serde(deserialize_with = "true_or_always")]
1466+
Always,
1467+
#[serde(deserialize_with = "false_or_never")]
1468+
Never,
1469+
#[serde(deserialize_with = "de_unit_v::with_block")]
1470+
WithBlock,
1471+
}
1472+
14571473
#[derive(Deserialize, Debug, Clone)]
14581474
#[serde(untagged)]
14591475
enum ReborrowHintsDef {
@@ -1740,6 +1756,19 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
17401756
"Only show lifetime elision hints if a return type is involved."
17411757
]
17421758
},
1759+
"ClosureReturnTypeHintsDef" => set! {
1760+
"type": "string",
1761+
"enum": [
1762+
"always",
1763+
"never",
1764+
"with_block"
1765+
],
1766+
"enumDescriptions": [
1767+
"Always show type hints for return types of closures.",
1768+
"Never show type hints for return types of closures.",
1769+
"Only show type hints for return types of closures with blocks."
1770+
]
1771+
},
17431772
"ReborrowHintsDef" => set! {
17441773
"type": "string",
17451774
"enum": [

docs/user/generated_config.adoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@ Whether to show inlay hints after a closing `}` to indicate what item it belongs
366366
Minimum number of lines required before the `}` until the hint is shown (set to 0 or 1
367367
to always show them).
368368
--
369-
[[rust-analyzer.inlayHints.closureReturnTypeHints.enable]]rust-analyzer.inlayHints.closureReturnTypeHints.enable (default: `false`)::
369+
[[rust-analyzer.inlayHints.closureReturnTypeHints.enable]]rust-analyzer.inlayHints.closureReturnTypeHints.enable (default: `"never"`)::
370370
+
371371
--
372-
Whether to show inlay type hints for return types of closures with blocks.
372+
Whether to show inlay type hints for return types of closures.
373373
--
374374
[[rust-analyzer.inlayHints.lifetimeElisionHints.enable]]rust-analyzer.inlayHints.lifetimeElisionHints.enable (default: `"never"`)::
375375
+

editors/code/package.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -811,9 +811,19 @@
811811
"minimum": 0
812812
},
813813
"rust-analyzer.inlayHints.closureReturnTypeHints.enable": {
814-
"markdownDescription": "Whether to show inlay type hints for return types of closures with blocks.",
815-
"default": false,
816-
"type": "boolean"
814+
"markdownDescription": "Whether to show inlay type hints for return types of closures.",
815+
"default": "never",
816+
"type": "string",
817+
"enum": [
818+
"always",
819+
"never",
820+
"with_block"
821+
],
822+
"enumDescriptions": [
823+
"Always show type hints for return types of closures.",
824+
"Never show type hints for return types of closures.",
825+
"Only show type hints for return types of closures with blocks."
826+
]
817827
},
818828
"rust-analyzer.inlayHints.lifetimeElisionHints.enable": {
819829
"markdownDescription": "Whether to show inlay type hints for elided lifetimes in function signatures.",

0 commit comments

Comments
 (0)