Skip to content

Account for new assert! desugaring in !condition suggestion #15453

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

Closed
wants to merge 1 commit into from

Conversation

estebank
Copy link
Contributor

rustc in rust-lang/rust#122661 is going to change the desugaring of assert! to be

match condition {
    true => {}
    _ => panic!(),
}

which will make the edge-case of condition being impl Not<Output = bool> while not being bool itself no longer a straightforward suggestion, but !!condition will coerce the expression to be bool, so it can be machine applicable.

changelog: [bool_assert]: Account for new assert! desugaring in suggestion

@rustbot
Copy link
Collaborator

rustbot commented Aug 10, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
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 the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 10, 2025
Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Could you please add this implementation and those tests?

impl Add for ImplNotTraitWithBool {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        self
    }
}
[]
    assert_eq!(!b, true);
    //~^ bool_assert_comparison
    assert_eq!(!b, false);
    //~^ bool_assert_comparison
    assert_eq!(b + b, true);
    //~^ bool_assert_comparison
    assert_eq!(b + b, false);
    //~^ bool_assert_comparison

Right now, the assert_eq!(b + b, true) will fail once fixed because it would be transformed into assert!(!!b + b). The proposed change fixes that.

assert_eq!(!b, false); will get an extra pair of parentheses once fixed assert!(!(!b)) but this will be rare and can be ignored as it is only an aesthetic issue.

Comment on lines 133 to 151
if let ty::Bool = non_lit_ty.kind() {
if bool_value ^ eq_macro {
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else {
return;
};
suggestions.push((non_lit_expr.span, (!sugg).to_string()));
}
} else {
// If we have a `value` that is *not* `bool` but that `!value` *is*, we suggest
// `!!value`.
suggestions.push((
non_lit_expr.span.shrink_to_lo(),
if bool_value ^ eq_macro {
"!".to_string()
} else {
"!!".to_string()
},
));
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if let ty::Bool = non_lit_ty.kind() {
if bool_value ^ eq_macro {
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else {
return;
};
suggestions.push((non_lit_expr.span, (!sugg).to_string()));
}
} else {
// If we have a `value` that is *not* `bool` but that `!value` *is*, we suggest
// `!!value`.
suggestions.push((
non_lit_expr.span.shrink_to_lo(),
if bool_value ^ eq_macro {
"!".to_string()
} else {
"!!".to_string()
},
));
}
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else {
return;
};
let sugg = if bool_value ^ eq_macro {
!sugg.maybe_paren()
} else if ty::Bool == *non_lit_ty.kind() {
sugg
} else {
!!sugg.maybe_paren()
};
suggestions.push((non_lit_expr.span, sugg.to_string()));

I would even remove the early return here with:

                if let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) {
                    let sugg = if bool_value ^ eq_macro {
                        !sugg.maybe_paren()
                    } else if ty::Bool == *non_lit_ty.kind() {
                        sugg
                    } else {
                        !!sugg.maybe_paren()
                    };
                    suggestions.push((non_lit_expr.span, sugg.to_string()));

                    diag.multipart_suggestion(
                        format!("replace it with `{non_eq_mac}!(..)`"),
                        suggestions,
                        Applicability::MachineApplicable,
                    );
                }

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Aug 11, 2025
`rustc` is going to change the desugaring of `assert!` to be

```rust
match condition {
    true => {}
    _ => panic!(),
}
```
which will make the edge-case of `condition` being `impl Not<Output = bool>`
while not being `bool` itself no longer a straightforward suggestion,
but `!!condition` will coerce the expression to be `bool`, so it can be
machine applicable.
@estebank
Copy link
Contributor Author

Addressed and squashed

Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

LGTM

Concerning the process: if we merge it here, you'll have to wait at least until the next sync PR (which will be submitted on Aug. 21) is merged into the compiler repository, which might not be ideal since it blocks rust-lang/rust#122661.

Since the change is very localized, a possibility would be to resubmit it in r-l/r where I could approve it again through bors. This would supersede this PR and would remove a blocker sooner.

Tell me what you prefer.

@estebank
Copy link
Contributor Author

@samueltardieu lets do the latter. I'll submit it later.

@estebank
Copy link
Contributor Author

Closing in favor of rust-lang/rust#145273

@estebank estebank closed this Aug 11, 2025
fmease added a commit to fmease/rust that referenced this pull request Aug 12, 2025
Account for new `assert!` desugaring in `!condition` suggestion

`rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be

```rust
match condition {
    true => {}
    _ => panic!(),
}
```
which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.

Transposing rust-lang/rust-clippy#15453 to the rustc repo.

r? ``@samueltardieu``
fmease added a commit to fmease/rust that referenced this pull request Aug 12, 2025
Account for new `assert!` desugaring in `!condition` suggestion

`rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be

```rust
match condition {
    true => {}
    _ => panic!(),
}
```
which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.

Transposing rust-lang/rust-clippy#15453 to the rustc repo.

r? ```@samueltardieu```
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 12, 2025
Account for new `assert!` desugaring in `!condition` suggestion

`rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be

```rust
match condition {
    true => {}
    _ => panic!(),
}
```
which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.

Transposing rust-lang/rust-clippy#15453 to the rustc repo.

r? ````@samueltardieu````
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 12, 2025
Account for new `assert!` desugaring in `!condition` suggestion

`rustc` in rust-lang#122661 is going to change the desugaring of `assert!` to be

```rust
match condition {
    true => {}
    _ => panic!(),
}
```
which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.

Transposing rust-lang/rust-clippy#15453 to the rustc repo.

r? `````@samueltardieu`````
rust-timer added a commit to rust-lang/rust that referenced this pull request Aug 12, 2025
Rollup merge of #145273 - estebank:not-not, r=samueltardieu

Account for new `assert!` desugaring in `!condition` suggestion

`rustc` in #122661 is going to change the desugaring of `assert!` to be

```rust
match condition {
    true => {}
    _ => panic!(),
}
```
which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.

Transposing rust-lang/rust-clippy#15453 to the rustc repo.

r? `````@samueltardieu`````
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Aug 13, 2025
Account for new `assert!` desugaring in `!condition` suggestion

`rustc` in rust-lang/rust#122661 is going to change the desugaring of `assert!` to be

```rust
match condition {
    true => {}
    _ => panic!(),
}
```
which will make the edge-case of `condition` being `impl Not<Output = bool>` while not being `bool` itself no longer a straightforward suggestion, but `!!condition` will coerce the expression to be `bool`, so it can be machine applicable.

Transposing rust-lang/rust-clippy#15453 to the rustc repo.

r? `````@samueltardieu`````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants