Skip to content

bool_assert_comparison false positive when one side is non-bool with PartialEq<bool> impl #7548

@tl-alex-butler

Description

@tl-alex-butler

Lint name: bool_assert_comparison

When testing serde_json::Value values, or more generally more-complex-than-bool types that implement PartialEq<bool>, assert_eq! testing should not be linted against.

Example

// serde_json = "1"

let val: serde_json::Value = serde_json::from_str(r#"{"bar": true, "baz": 123}"#).unwrap();

assert_eq!(val["bar"], true);
assert_eq!(val["baz"], false);

These trigger bool_assert_comparison lint

warning: used `assert_eq!` with a literal bool
 --> src/main.rs:5:5
  |
5 |     assert_eq!(val["baz"], false);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `assert!(..)`

However, these aren't trivially fixable with assert!(..). Moreover, converting to a bool for testing is worse code as it provides less info in a test failure scenario. For example, running the example code tells us not only that the 2nd assert fails, but exactly what the actual value was.

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Number(123)`,
 right: `false`', src/main.rs:5:5

A workaround like assert_eq!(val["baz"].as_bool(), Some(false)) would make this test code worse, and does not seem to be the intention of this lint.

Meta

  • cargo clippy -V: clippy 0.1.56 (ad981d5 2021-08-08)
  • rustc -Vv:
    rustc 1.56.0-nightly (ad981d58e 2021-08-08)
    binary: rustc
    commit-hash: ad981d58e1ca16bcf4072577934630deb11c5e14
    commit-date: 2021-08-08
    host: x86_64-unknown-linux-gnu
    release: 1.56.0-nightly
    LLVM version: 12.0.1
    

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't havegood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions