Fix unused_parens false positive

This commit is contained in:
Ben Schulz
2025-07-09 18:46:11 +02:00
parent 5d22242a3a
commit e958b20af7
2 changed files with 15 additions and 1 deletions

View File

@@ -1062,6 +1062,7 @@ pub(crate) struct UnusedParens {
/// ``` /// ```
/// type Example = Box<dyn Fn() -> &'static dyn Send>; /// type Example = Box<dyn Fn() -> &'static dyn Send>;
/// ``` /// ```
#[derive(Copy, Clone)]
enum NoBoundsException { enum NoBoundsException {
/// The type must be parenthesized. /// The type must be parenthesized.
None, None,
@@ -1340,7 +1341,11 @@ impl EarlyLintPass for UnusedParens {
self.with_self_ty_parens = false; self.with_self_ty_parens = false;
} }
ast::TyKind::Ref(_, mut_ty) | ast::TyKind::Ptr(mut_ty) => { 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);
} }
ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => { ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
for i in 0..bounds.len() { for i in 0..bounds.len() {

View File

@@ -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>) {}
fn main() {}