Skip to content
Open
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
33 changes: 18 additions & 15 deletions clippy_lints/src/bool_assert_comparison.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
use clippy_utils::source::walk_span_to_context;
use clippy_utils::sugg::Sugg;
use clippy_utils::sym;
use clippy_utils::ty::{implements_trait, is_copy};
Expand Down Expand Up @@ -130,22 +131,24 @@ impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {

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

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()));
let mut applicability = Applicability::MachineApplicable;
let sugg = Sugg::hir_with_context(cx, non_lit_expr, macro_call.span.ctxt(), "..", &mut applicability);
let sugg = if bool_value ^ eq_macro {
!sugg.maybe_paren()
} else if ty::Bool == *non_lit_ty.kind() {
sugg
} else {
!!sugg.maybe_paren()
};
let non_lit_expr_span =
walk_span_to_context(non_lit_expr.span, macro_call.span.ctxt()).unwrap_or(non_lit_expr.span);
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,
);
},
);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/bool_assert_comparison.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,16 @@ fn main() {
assert!(!(b + b));
//~^ bool_assert_comparison
}

fn issue16279() {
macro_rules! is_empty {
($x:expr) => {
$x.is_empty()
};
}

assert!(!is_empty!("a"));
//~^ bool_assert_comparison
assert!(is_empty!(""));
//~^ bool_assert_comparison
}
13 changes: 13 additions & 0 deletions tests/ui/bool_assert_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,16 @@ fn main() {
assert_eq!(b + b, false);
//~^ bool_assert_comparison
}

fn issue16279() {
macro_rules! is_empty {
($x:expr) => {
$x.is_empty()
};
}

assert_eq!(is_empty!("a"), false);
//~^ bool_assert_comparison
assert_eq!(is_empty!(""), true);
//~^ bool_assert_comparison
}
26 changes: 25 additions & 1 deletion tests/ui/bool_assert_comparison.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,29 @@ LL - assert_eq!(b + b, false);
LL + assert!(!(b + b));
|

error: aborting due to 37 previous errors
error: used `assert_eq!` with a literal bool
--> tests/ui/bool_assert_comparison.rs:227:5
|
LL | assert_eq!(is_empty!("a"), false);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace it with `assert!(..)`
|
LL - assert_eq!(is_empty!("a"), false);
LL + assert!(!is_empty!("a"));
|

error: used `assert_eq!` with a literal bool
--> tests/ui/bool_assert_comparison.rs:229:5
|
LL | assert_eq!(is_empty!(""), true);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: replace it with `assert!(..)`
|
LL - assert_eq!(is_empty!(""), true);
LL + assert!(is_empty!(""));
|

error: aborting due to 39 previous errors