about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_lint/src/unused.rs10
-rw-r--r--tests/ui/lint/unused/unused-parens-false-positive-issue-143653.fixed12
-rw-r--r--tests/ui/lint/unused/unused-parens-false-positive-issue-143653.rs12
-rw-r--r--tests/ui/lint/unused/unused-parens-false-positive-issue-143653.stderr19
4 files changed, 52 insertions, 1 deletions
diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs
index a9eb1739f7f..df9f3a500d9 100644
--- a/compiler/rustc_lint/src/unused.rs
+++ b/compiler/rustc_lint/src/unused.rs
@@ -1340,7 +1340,15 @@ 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);
+                let constraint = match own_constraint {
+                    Some(NoBoundsException::None) => NoBoundsException::None,
+                    Some(NoBoundsException::OneBound) => NoBoundsException::OneBound,
+                    None => 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.fixed b/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.fixed
new file mode 100644
index 00000000000..4b0bca3d44a
--- /dev/null
+++ b/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.fixed
@@ -0,0 +1,12 @@
+//@ run-rustfix
+
+#![deny(unused_parens)]
+#![allow(warnings)]
+trait MyTrait {}
+
+fn foo(_: Box<dyn FnMut(&mut u32) -> &mut (dyn MyTrait) + Send + Sync>) {}
+
+//~v ERROR unnecessary parentheses around type
+fn bar(_: Box<dyn FnMut(&mut u32) -> &mut dyn MyTrait>) {}
+
+fn main() {}
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..4eefd3dc81a
--- /dev/null
+++ b/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.rs
@@ -0,0 +1,12 @@
+//@ run-rustfix
+
+#![deny(unused_parens)]
+#![allow(warnings)]
+trait MyTrait {}
+
+fn foo(_: Box<dyn FnMut(&mut u32) -> &mut (dyn MyTrait) + Send + Sync>) {}
+
+//~v ERROR unnecessary parentheses around type
+fn bar(_: Box<dyn FnMut(&mut u32) -> &mut (dyn MyTrait)>) {}
+
+fn main() {}
diff --git a/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.stderr b/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.stderr
new file mode 100644
index 00000000000..89455e3db73
--- /dev/null
+++ b/tests/ui/lint/unused/unused-parens-false-positive-issue-143653.stderr
@@ -0,0 +1,19 @@
+error: unnecessary parentheses around type
+  --> $DIR/unused-parens-false-positive-issue-143653.rs:10:43
+   |
+LL | fn bar(_: Box<dyn FnMut(&mut u32) -> &mut (dyn MyTrait)>) {}
+   |                                           ^           ^
+   |
+note: the lint level is defined here
+  --> $DIR/unused-parens-false-positive-issue-143653.rs:3:9
+   |
+LL | #![deny(unused_parens)]
+   |         ^^^^^^^^^^^^^
+help: remove these parentheses
+   |
+LL - fn bar(_: Box<dyn FnMut(&mut u32) -> &mut (dyn MyTrait)>) {}
+LL + fn bar(_: Box<dyn FnMut(&mut u32) -> &mut dyn MyTrait>) {}
+   |
+
+error: aborting due to 1 previous error
+