Make array suggestions slightly more accurate

This commit is contained in:
Michael Goulet 2024-04-15 21:45:47 -04:00
parent 8a981b6fee
commit c95761385e
2 changed files with 12 additions and 5 deletions

View File

@ -4550,7 +4550,7 @@ fn ty_kind_suggestion(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> Opt
self.type_implements_trait(default_trait, [ty], param_env).must_apply_modulo_regions()
};
Some(match ty.kind() {
Some(match *ty.kind() {
ty::Never | ty::Error(_) => return None,
ty::Bool => "false".to_string(),
ty::Char => "\'x\'".to_string(),
@ -4577,12 +4577,19 @@ fn ty_kind_suggestion(&self, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> Opt
if let (ty::Str, hir::Mutability::Not) = (ty.kind(), mutability) {
"\"\"".to_string()
} else {
let ty = self.ty_kind_suggestion(param_env, *ty)?;
let ty = self.ty_kind_suggestion(param_env, ty)?;
format!("&{}{ty}", mutability.prefix_str())
}
}
ty::Array(ty, len) if let Some(len) = len.try_eval_target_usize(tcx, param_env) => {
format!("[{}; {}]", self.ty_kind_suggestion(param_env, *ty)?, len)
if len == 0 {
"[]".to_string()
} else if self.type_is_copy_modulo_regions(param_env, ty) || len == 1 {
// Can only suggest `[ty; 0]` if sz == 1 or copy
format!("[{}; {}]", self.ty_kind_suggestion(param_env, ty)?, len)
} else {
"/* value */".to_string()
}
}
ty::Tuple(tys) => format!(
"({}{})",

View File

@ -8,8 +8,8 @@ LL | a[i] = d();
|
help: consider assigning a value
|
LL | let mut a: [D; 4] = [/* value */; 4];
| ++++++++++++++++++
LL | let mut a: [D; 4] = /* value */;
| +++++++++++++
error: aborting due to 1 previous error