Render discriminant inlay hints for mixed variants if at least one discriminant is specified

This commit is contained in:
Lukas Wirth 2023-02-10 09:57:03 +01:00
parent 7fc0f8985d
commit 557aa1e378

View file

@ -19,12 +19,15 @@ pub(super) fn enum_hints(
_: FileId, _: FileId,
enum_: ast::Enum, enum_: ast::Enum,
) -> Option<()> { ) -> Option<()> {
let disabled = match config.discriminant_hints { let enabled = match config.discriminant_hints {
DiscriminantHints::Always => false, DiscriminantHints::Always => true,
DiscriminantHints::Fieldless => sema.to_def(&enum_)?.is_data_carrying(sema.db), DiscriminantHints::Fieldless => {
DiscriminantHints::Never => true, !sema.to_def(&enum_)?.is_data_carrying(sema.db)
|| enum_.variant_list()?.variants().any(|v| v.expr().is_some())
}
DiscriminantHints::Never => false,
}; };
if disabled { if !enabled {
return None; return None;
} }
for variant in enum_.variant_list()?.variants() { for variant in enum_.variant_list()?.variants() {
@ -161,8 +164,25 @@ enum Enum {
Variant1, Variant1,
Variant2 {}, Variant2 {},
Variant3, Variant3,
Variant5,
Variant6,
}
"#,
);
check_discriminants_fieldless(
r#"
enum Enum {
Variant(),
//^^^^^^^^^0
Variant1,
//^^^^^^^^1
Variant2 {},
//^^^^^^^^^^^2
Variant3,
//^^^^^^^^3
Variant5 = 5, Variant5 = 5,
Variant6, Variant6,
//^^^^^^^^6
} }
"#, "#,
); );