about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Schulz <benshu@benshu.de>2025-07-09 18:46:11 +0200
committerBen Schulz <benshu@benshu.de>2025-07-24 20:44:09 +0200
commite958b20af7ad3e3a053070a9c45389fe463ccc0c (patch)
tree64985cdf8eedbe2f0493373cc5112ebd3527901e
parent5d22242a3a84a55be2f648a94eecff58887547f4 (diff)
downloadrust-e958b20af7ad3e3a053070a9c45389fe463ccc0c.tar.gz
rust-e958b20af7ad3e3a053070a9c45389fe463ccc0c.zip
Fix unused_parens false positive
-rw-r--r--compiler/rustc_lint/src/unused.rs7
-rw-r--r--tests/ui/lint/unused/unused-parens-false-positive-issue-143653.rs9
2 files changed, 15 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs
index a9eb1739f7f..5873e7bd6a7 100644
--- a/compiler/rustc_lint/src/unused.rs
+++ b/compiler/rustc_lint/src/unused.rs
@@ -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,
@@ -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);
             }
             ast::TyKind::TraitObject(bounds, _) | ast::TyKind::ImplTrait(_, bounds) => {
                 for i in 0..bounds.len() {
diff --git a/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.rs b/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.rs
new file mode 100644
index 00000000000..78f335c26a7
--- /dev/null
+++ b/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.rs
@@ -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() {}