Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix unused_parens false positive
  • Loading branch information
benschulz committed Jul 24, 2025
commit e958b20af7ad3e3a053070a9c45389fe463ccc0c
7 changes: 6 additions & 1 deletion compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ pub(crate) struct UnusedParens {
/// ```
/// type Example = Box<dyn Fn() -> &'static dyn Send>;
/// ```
#[derive(Copy, Clone)]
enum NoBoundsException {
/// The type must be parenthesized.
None,
Expand Down Expand Up @@ -1340,7 +1341,11 @@ impl EarlyLintPass for UnusedParens {
self.with_self_ty_parens = false;
}
ast::TyKind::Ref(_, mut_ty) | ast::TyKind::Ptr(mut_ty) => {
self.in_no_bounds_pos.insert(mut_ty.ty.id, NoBoundsException::OneBound);
// If this type itself appears in no-bounds position, we propagate its
// potentially tighter constraint or risk a false posive (issue 143653).
let own_constraint = self.in_no_bounds_pos.get(&ty.id).copied();
let constraint = own_constraint.unwrap_or(NoBoundsException::OneBound);
self.in_no_bounds_pos.insert(mut_ty.ty.id, constraint);
Copy link
Contributor

Choose a reason for hiding this comment

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

in a sense we want tis to be constraint = own_constraint.max(NoBoundsException::OneBound) where as NoBoundsException::OneBound is currently the minimal value this is correct?

I would feel safer if we were to make this a match on own_constraint or add a NoBoundsException.merge function or sth 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in a sense we want tis to be constraint = own_constraint.max(NoBoundsException::OneBound) where as NoBoundsException::OneBound is currently the minimal value this is correct?

Correct.

I would feel safer if we were to make this a match on own_constraint or add a NoBoundsException.merge function or sth 🤔

I see where you're coming from. I went with the match because 1) I don't see this sprawling and 2) the Option makes the alternatives a bit "messy". Happy to take this any other direction though. 🙂

}
ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
for i in 0..bounds.len() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ check-pass

#![deny(unused_parens)]
#![allow(warnings)]
trait MyTrait {}

fn foo(_: Box<dyn FnMut(&mut u32) -> &mut (dyn MyTrait) + Send + Sync>) {}
Copy link
Member

@compiler-errors compiler-errors Jul 9, 2025

Choose a reason for hiding this comment

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

How does this behave with dyn FnMut(&()) -> &(dyn Trait) (i.e. one bound that is parenthesized)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added that case to the test: We continue to emit a warning.


fn main() {}