Skip to content

nonminimal_bool and macros #12627

Open
Open
@ijackson

Description

@ijackson

Summary

When writing macros which expand to boolean expressions, it is sometimes best to write a nonminimal expression.

For example, in an MR in Arti I have this code:

    #[allow(clippy::nonminimal_bool)]
    fn same_relay_ids<T: HasRelayIds + ?Sized>(&self, other: &T) -> bool {
        derive_deftly_adhoc! {
            RelayIdType:
            $(
                self.identity($vtype) == other.identity($vtype) &&
            )
                true
        }
    }

The body expands to:

                self.identity(RelayIdType::Ed22519) == other.identity(RelayIdType::Ed22519) &&
                self.identity(RelayIdType::Rsa) == other.identity(RelayIdType::Rsa) &&
                true

This triggers the lint. But there isn't a better way to write it.

This is with derive-deftly. I think similar situations could arise with macro_rules! macros.

I suggest either or both of:

  • Don't lint when boolean literals true and false are involved. People who wrote those probably did so deliberately.
  • Don't lint when the expression is generated from a proc macro.

Lint Name

nonminimal_bool

Reproducer

To reproduce

git clone https://gitlab.torproject.org/Diziet/arti
cd arti
git checkout relay-id-perf-2-clippy-repro
cargo clippy --locked -p tor-linkspec

Actual output

warning: this boolean expression can be simplified
   --> crates/tor-linkspec/src/traits.rs:118:17
    |
118 | /                 self.identity($vtype) == other.identity($vtype) &&
119 | |             )
120 | |                 true
    | |____________________^ help: try: `self.identity($vtype) == other.identity( && self.identity($vtype) == other.identity(`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool
note: the lint level is defined here
   --> crates/tor-linkspec/src/lib.rs:9:9
    |
9   | #![warn(clippy::all)]
    |         ^^^^^^^^^^^
    = note: `#[warn(clippy::nonminimal_bool)]` implied by `#[warn(clippy::all)]`

warning: `tor-linkspec` (lib) generated 1 warning

Expected output

No complaint

Version

rustc 1.77.0-beta.5 (f2043422f 2024-02-17)
binary: rustc
commit-hash: f2043422f7b161a2fc1a00589a8c4956db963450
commit-date: 2024-02-17
host: x86_64-unknown-linux-gnu
release: 1.77.0-beta.5
LLVM version: 17.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveT-macrosType: Issues with macros and macro expansion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions