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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions clippy_lints/src/bool_assert_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,22 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {

let mut suggestions = vec![(name_span, non_eq_mac.to_string()), (lit_span, String::new())];

if bool_value ^ eq_macro {
let Some(sugg) = Sugg::hir_opt(cx, non_lit_expr) else {
return;
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()));
}
suggestions.push((non_lit_expr.span, sugg.to_string()));

diag.multipart_suggestion(
format!("replace it with `{non_eq_mac}!(..)`"),
suggestions,
Applicability::MachineApplicable,
);
diag.multipart_suggestion(
format!("replace it with `{non_eq_mac}!(..)`"),
suggestions,
Applicability::MachineApplicable,
);
}
},
);
}
Expand Down
24 changes: 20 additions & 4 deletions tests/ui/bool_assert_comparison.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
#![warn(clippy::bool_assert_comparison)]

use std::ops::Not;
use std::ops::{Add, Not};

macro_rules! a {
() => {
Expand Down Expand Up @@ -62,6 +62,14 @@ impl Not for ImplNotTraitWithBool {
}
}

impl Add for ImplNotTraitWithBool {
type Output = Self;

fn add(self, other: Self) -> Self::Output {
self
}
}

#[derive(Debug)]
struct NonCopy;

Expand Down Expand Up @@ -94,7 +102,7 @@ fn main() {
assert_eq!(a!(), "".is_empty());
assert_eq!("".is_empty(), b!());
assert_eq!(a, true);
assert!(b);
assert!(!!b);
//~^ bool_assert_comparison

assert_ne!("a".len(), 1);
Expand Down Expand Up @@ -122,7 +130,7 @@ fn main() {
debug_assert_eq!(a!(), "".is_empty());
debug_assert_eq!("".is_empty(), b!());
debug_assert_eq!(a, true);
debug_assert!(b);
debug_assert!(!!b);
//~^ bool_assert_comparison

debug_assert_ne!("a".len(), 1);
Expand Down Expand Up @@ -167,7 +175,7 @@ fn main() {

use debug_assert_eq as renamed;
renamed!(a, true);
debug_assert!(b);
debug_assert!(!!b);
//~^ bool_assert_comparison

let non_copy = NonCopy;
Expand Down Expand Up @@ -199,4 +207,12 @@ fn main() {
//~^ bool_assert_comparison
debug_assert!(!"requires negation".is_empty());
//~^ bool_assert_comparison
assert!(!b);
//~^ bool_assert_comparison
assert!(!(!b));
//~^ bool_assert_comparison
assert!(!!(b + b));
//~^ bool_assert_comparison
assert!(!(b + b));
//~^ bool_assert_comparison
}
18 changes: 17 additions & 1 deletion tests/ui/bool_assert_comparison.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
#![warn(clippy::bool_assert_comparison)]

use std::ops::Not;
use std::ops::{Add, Not};

macro_rules! a {
() => {
Expand Down Expand Up @@ -62,6 +62,14 @@ impl Not for ImplNotTraitWithBool {
}
}

impl Add for ImplNotTraitWithBool {
type Output = Self;

fn add(self, other: Self) -> Self::Output {
self
}
}

#[derive(Debug)]
struct NonCopy;

Expand Down Expand Up @@ -199,4 +207,12 @@ fn main() {
//~^ bool_assert_comparison
debug_assert_eq!("requires negation".is_empty(), false);
//~^ bool_assert_comparison
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
}
Loading