Skip to content

Fix unused_parens false positive #143698

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 3 commits into from
Jul 26, 2025
Merged

Conversation

benschulz
Copy link
Contributor

@benschulz benschulz commented Jul 9, 2025

Resolves #143653.

The "no bounds exception" was indiscriminately set to OneBound for referents and pointees. However, if the reference or pointer type itself appears in no-bounds position, any constraints it has must be propagated.

// unused parens: not in no-bounds position
fn foo(_: Box<(dyn Send)>) {}

// unused parens: in no-bounds position, but one-bound exception applies
fn bar(_: Box<dyn Fn(&u32) -> &(dyn Send)>) {} 

// *NOT* unused parens: in no-bounds position, but no exceptions to be made
fn baz(_: Box<dyn Fn(&u32) -> &(dyn Send) + Send>) {}

@rustbot
Copy link
Collaborator

rustbot commented Jul 9, 2025

r? @lcnr

rustbot has assigned @lcnr.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 9, 2025
#![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.

Comment on lines 1339 to 1351
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. 🙂

@rustbot

This comment has been minimized.

@lcnr
Copy link
Contributor

lcnr commented Jul 25, 2025

thx, sorry for the slow review!

@bors r=lcnr,compiler-errors rollup

@bors
Copy link
Collaborator

bors commented Jul 25, 2025

📌 Commit 307f664 has been approved by lcnr,compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2025
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 26, 2025
…compiler-errors

Fix unused_parens false positive

Resolves rust-lang#143653.

The "no bounds exception" was indiscriminately set to `OneBound` for referents and pointees. However, if the reference or pointer type itself appears in no-bounds position, any constraints it has must be propagated.

```rust
// unused parens: not in no-bounds position
fn foo(_: Box<(dyn Send)>) {}

// unused parens: in no-bounds position, but one-bound exception applies
fn bar(_: Box<dyn Fn(&u32) -> &(dyn Send)>) {}

// *NOT* unused parens: in no-bounds position, but no exceptions to be made
fn baz(_: Box<dyn Fn(&u32) -> &(dyn Send) + Send>) {}
```
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 26, 2025
…compiler-errors

Fix unused_parens false positive

Resolves rust-lang#143653.

The "no bounds exception" was indiscriminately set to `OneBound` for referents and pointees. However, if the reference or pointer type itself appears in no-bounds position, any constraints it has must be propagated.

```rust
// unused parens: not in no-bounds position
fn foo(_: Box<(dyn Send)>) {}

// unused parens: in no-bounds position, but one-bound exception applies
fn bar(_: Box<dyn Fn(&u32) -> &(dyn Send)>) {}

// *NOT* unused parens: in no-bounds position, but no exceptions to be made
fn baz(_: Box<dyn Fn(&u32) -> &(dyn Send) + Send>) {}
```
bors added a commit that referenced this pull request Jul 26, 2025
Rollup of 9 pull requests

Successful merges:

 - #140871 (Don't lint against named labels in `naked_asm!`)
 - #141663 (rustdoc: add ways of collapsing all impl blocks)
 - #143272 (Upgrade the `fortanix-sgx-abi` dependency)
 - #143585 (`loop_match`: suggest extracting to a `const` item)
 - #143698 (Fix unused_parens false positive)
 - #143859 (Guarantee 8 bytes of alignment in Thread::into_raw)
 - #144042 (Verify llvm-needs-components are not empty and match the --target value)
 - #144160 (tests: debuginfo: Work around or disable broken tests on powerpc)
 - #144431 (Disable has_reliable_f128_math on musl targets)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Jul 26, 2025
Rollup of 9 pull requests

Successful merges:

 - #140871 (Don't lint against named labels in `naked_asm!`)
 - #141663 (rustdoc: add ways of collapsing all impl blocks)
 - #143272 (Upgrade the `fortanix-sgx-abi` dependency)
 - #143585 (`loop_match`: suggest extracting to a `const` item)
 - #143698 (Fix unused_parens false positive)
 - #143859 (Guarantee 8 bytes of alignment in Thread::into_raw)
 - #144160 (tests: debuginfo: Work around or disable broken tests on powerpc)
 - #144412 (Small cleanup: Use LocalKey<Cell> methods more)
 - #144431 (Disable has_reliable_f128_math on musl targets)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 75ed6de into rust-lang:master Jul 26, 2025
10 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Jul 26, 2025
rust-timer added a commit that referenced this pull request Jul 26, 2025
Rollup merge of #143698 - benschulz:unused-parens-2, r=lcnr,compiler-errors

Fix unused_parens false positive

Resolves #143653.

The "no bounds exception" was indiscriminately set to `OneBound` for referents and pointees. However, if the reference or pointer type itself appears in no-bounds position, any constraints it has must be propagated.

```rust
// unused parens: not in no-bounds position
fn foo(_: Box<(dyn Send)>) {}

// unused parens: in no-bounds position, but one-bound exception applies
fn bar(_: Box<dyn Fn(&u32) -> &(dyn Send)>) {}

// *NOT* unused parens: in no-bounds position, but no exceptions to be made
fn baz(_: Box<dyn Fn(&u32) -> &(dyn Send) + Send>) {}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive warning in unused_parens lint with dyn Trait returned from a closure
5 participants