And locals too

This commit is contained in:
Michael Goulet 2024-10-31 16:05:42 +00:00
parent ea4fb7c25c
commit c930bba283
6 changed files with 31 additions and 6 deletions

View File

@ -214,6 +214,7 @@ pub(crate) struct DependencyOnUnitNeverTypeFallback<'tcx> {
pub(crate) enum SuggestAnnotation {
Unit(Span),
Path(Span),
Local(Span),
}
#[derive(Clone)]
@ -240,6 +241,9 @@ fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
suggestions.push((span.shrink_to_lo(), "<() as ".to_string()));
suggestions.push((span.shrink_to_hi(), ">".to_string()));
}
SuggestAnnotation::Local(span) => {
suggestions.push((span, ": ()".to_string()));
}
}
}

View File

@ -620,6 +620,19 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Self::Result {
}
hir::intravisit::walk_expr(self, expr)
}
fn visit_local(&mut self, local: &'tcx hir::LetStmt<'tcx>) -> Self::Result {
if let None = local.ty
&& let ty = self.fcx.typeck_results.borrow().node_type(local.hir_id)
&& let Some(vid) = self.fcx.root_vid(ty)
&& self.reachable_vids.contains(&vid)
{
return ControlFlow::Break(errors::SuggestAnnotation::Local(
local.pat.span.shrink_to_hi(),
));
}
hir::intravisit::walk_local(self, local)
}
}
#[derive(Debug, Copy, Clone)]

View File

@ -15,8 +15,8 @@ LL | true => Default::default(),
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | true => <() as Default>::default(),
| ++++++ +
LL | let x: () = match true {
| ++++
warning: this function depends on never type fallback being `()`
--> $DIR/never-type-fallback-breaking.rs:27:1

View File

@ -13,6 +13,10 @@ note: in edition 2024, the requirement `!: ImplementedForUnitButNotNever` will f
LL | foo(_x);
| ^^
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | let _x: () = return;
| ++++
warning: 1 warning emitted

View File

@ -15,8 +15,8 @@ LL | x = UnitDefault::default();
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | x = <() as UnitDefault>::default();
| ++++++ +
LL | let x: ();
| ++++
warning: this function depends on never type fallback being `()`
--> $DIR/diverging-fallback-control-flow.rs:42:1
@ -34,8 +34,8 @@ LL | x = UnitDefault::default();
| ^^^^^^^^^^^^^^^^^^^^^^
help: use `()` annotations to avoid fallback changes
|
LL | x = <() as UnitDefault>::default();
| ++++++ +
LL | let x: ();
| ++++
warning: 2 warnings emitted

View File

@ -13,6 +13,10 @@ note: in edition 2024, the requirement `!: UnitReturn` will fail
LL | let _ = if true { unconstrained_return() } else { panic!() };
| ^^^^^^^^^^^^^^^^^^^^^^
= note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
|
LL | let _: () = if true { unconstrained_return() } else { panic!() };
| ++++
warning: 1 warning emitted