Skip to content

Fix false positive for unused_unit #14962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/unused_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedUnit {
&& let AssocItemConstraintKind::Equality { term: Term::Ty(hir_ty) } = constraints[0].kind
&& args.span_ext.hi() != poly.span.hi()
&& !hir_ty.span.from_expansion()
&& args.span_ext.hi() != hir_ty.span.hi()
Copy link
Author

@benschulz benschulz Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this should have been covered by !hir_ty.span.from_expansion(). Certainly, there are no explicit -> () tokens in the source. If this is the case, I'm happy to close this PR and file an issue upstream.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those expansion checks are quite finicky; sometimes you have an expression without expansion but a subexpression expanded, etc. So as long as this appears to fix the issue, it's better than nothing.

&& is_unit_ty(hir_ty)
{
lint_unneeded_unit_return(cx, hir_ty.span, poly.span);
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/unused_unit.edition2021.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,10 @@ mod issue14577 {
todo!()
}
}
}
}

mod pr14962 {
#[allow(unused_parens)]
type UnusedParensButNoUnit = Box<dyn (Fn())>;
}

8 changes: 7 additions & 1 deletion tests/ui/unused_unit.edition2024.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,10 @@ mod issue14577 {
todo!()
}
}
}
}

mod pr14962 {
#[allow(unused_parens)]
type UnusedParensButNoUnit = Box<dyn (Fn())>;
}

8 changes: 7 additions & 1 deletion tests/ui/unused_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,10 @@ mod issue14577 {
todo!()
}
}
}
}

mod pr14962 {
#[allow(unused_parens)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a targeted lint, could you please use #[expect(unused_parens)] instead? That doubles as an extra free test for unused_parens that would force us to think about it if its behavior changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not actually emit a warning by rustc yet. I'm currently looking into why that is exactly. I can remove the attribute and change the name of the type alias if you want.

type UnusedParensButNoUnit = Box<dyn (Fn())>;
}