Do not suggest replacing an expression by an ambiguous type name

An expression such as `<_>::default()` should not be replaced by an
ambiguous type name such as `_` even if the inferred type in the
original expression is a singleton.
This commit is contained in:
Samuel Tardieu 2025-03-06 22:31:03 +01:00
parent cee9abf10c
commit 429c09ead2
3 changed files with 20 additions and 0 deletions

View file

@ -70,6 +70,8 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
&& let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant()
&& !var.is_field_list_non_exhaustive()
&& !expr.span.from_expansion() && !qpath.span().from_expansion()
// do not suggest replacing an expression by a type name with placeholders
&& !base.is_suggestable_infer_ty()
{
span_lint_and_sugg(
cx,

View file

@ -161,3 +161,12 @@ fn main() {
let _ = <struct_from_macro!()>::default();
}
fn issue12654() {
#[derive(Default)]
struct G;
fn f(_g: G) {}
f(<_>::default());
}

View file

@ -161,3 +161,12 @@ fn main() {
let _ = <struct_from_macro!()>::default();
}
fn issue12654() {
#[derive(Default)]
struct G;
fn f(_g: G) {}
f(<_>::default());
}