Only suggest unit for unit fallback when spans dont come from macro expansion

This commit is contained in:
Michael Goulet 2025-02-05 18:59:07 +00:00
parent 820bfffc25
commit cac63ffc0f
5 changed files with 52 additions and 10 deletions

View File

@ -651,6 +651,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
if let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(inf_id)
&& let Some(vid) = self.fcx.root_vid(ty)
&& self.reachable_vids.contains(&vid)
&& inf_span.can_be_used_for_suggestions()
{
return ControlFlow::Break(errors::SuggestAnnotation::Unit(inf_span));
}
@ -662,7 +663,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
&mut self,
qpath: &'tcx rustc_hir::QPath<'tcx>,
id: HirId,
_span: Span,
span: Span,
) -> Self::Result {
let arg_segment = match qpath {
hir::QPath::Resolved(_, path) => {
@ -674,7 +675,9 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
}
};
// Alternatively, try to turbofish `::<_, (), _>`.
if let Some(def_id) = self.fcx.typeck_results.borrow().qpath_res(qpath, id).opt_def_id() {
if let Some(def_id) = self.fcx.typeck_results.borrow().qpath_res(qpath, id).opt_def_id()
&& span.can_be_used_for_suggestions()
{
self.suggest_for_segment(arg_segment, def_id, id)?;
}
hir::intravisit::walk_qpath(self, qpath, id)
@ -691,17 +694,21 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
&& let Some(vid) = self.fcx.root_vid(self_ty)
&& self.reachable_vids.contains(&vid)
&& let [.., trait_segment, _method_segment] = path.segments
&& expr.span.can_be_used_for_suggestions()
{
let span = path.span.shrink_to_lo().to(trait_segment.ident.span);
return ControlFlow::Break(errors::SuggestAnnotation::Path(span));
}
// Or else, try suggesting turbofishing the method args.
if let hir::ExprKind::MethodCall(segment, ..) = expr.kind
&& let Some(def_id) =
self.fcx.typeck_results.borrow().type_dependent_def_id(expr.hir_id)
&& expr.span.can_be_used_for_suggestions()
{
self.suggest_for_segment(segment, def_id, expr.hir_id)?;
}
hir::intravisit::walk_expr(self, expr)
}
@ -712,6 +719,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
&& let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(local.hir_id)
&& let Some(vid) = self.fcx.root_vid(ty)
&& self.reachable_vids.contains(&vid)
&& local.span.can_be_used_for_suggestions()
{
return ControlFlow::Break(errors::SuggestAnnotation::Local(
local.pat.span.shrink_to_hi(),

View File

@ -0,0 +1,20 @@
#![deny(dependency_on_unit_never_type_fallback)]
fn create_ok_default<C>() -> Result<C, ()>
where
C: Default,
{
Ok(C::default())
}
fn main() -> Result<(), ()> {
//~^ ERROR this function depends on never type fallback being `()`
//~| WARN this was previously accepted by the compiler but is being phased out
let (returned_value, _) = (|| {
let created = create_ok_default()?;
Ok((created, ()))
})()?;
let _ = format_args!("{:?}", returned_value);
Ok(())
}

View File

@ -0,0 +1,22 @@
error: this function depends on never type fallback being `()`
--> $DIR/dont-suggest-turbofish-from-expansion.rs:10:1
|
LL | fn main() -> Result<(), ()> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>
= help: specify the types explicitly
note: in edition 2024, the requirement `!: Default` will fail
--> $DIR/dont-suggest-turbofish-from-expansion.rs:14:23
|
LL | let created = create_ok_default()?;
| ^^^^^^^^^^^^^^^^^^^
note: the lint level is defined here
--> $DIR/dont-suggest-turbofish-from-expansion.rs:1:9
|
LL | #![deny(dependency_on_unit_never_type_fallback)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 1 previous error

View File

@ -130,10 +130,6 @@ LL | msg_send!();
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>
= help: specify the type explicitly
= note: this warning originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `()` annotations to avoid fallback changes
|
LL | match send_message::<() /* ?0 */>() {
| ~~
warning: 10 warnings emitted

View File

@ -130,10 +130,6 @@ LL | msg_send!();
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/never-type-fallback.html>
= help: specify the type explicitly
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `()` annotations to avoid fallback changes
|
LL | match send_message::<() /* ?0 */>() {
| ~~
warning: the type `!` does not permit zero-initialization
--> $DIR/lint-never-type-fallback-flowing-into-unsafe.rs:12:18