2023-01-27 14:09:08 -06:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2022-01-13 06:18:19 -06:00
|
|
|
use clippy_utils::macros::{find_assert_eq_args, root_macro_call_first_node};
|
2023-01-27 14:09:08 -06:00
|
|
|
use clippy_utils::ty::{implements_trait, is_copy};
|
2021-09-08 09:31:47 -05:00
|
|
|
use rustc_ast::ast::LitKind;
|
2021-04-22 04:31:13 -05:00
|
|
|
use rustc_errors::Applicability;
|
2021-09-08 09:31:47 -05:00
|
|
|
use rustc_hir::{Expr, ExprKind, Lit};
|
2023-01-27 14:09:08 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::ty::{self, Ty};
|
2021-04-22 04:31:13 -05:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-09-08 09:31:47 -05:00
|
|
|
use rustc_span::symbol::Ident;
|
2021-04-22 04:31:13 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// This lint warns about boolean comparisons in assert-like macros.
|
2021-04-22 04:31:13 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It is shorter to use the equivalent.
|
2021-04-22 04:31:13 -05:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2021-04-22 04:31:13 -05:00
|
|
|
/// ```rust
|
|
|
|
/// assert_eq!("a".is_empty(), false);
|
|
|
|
/// assert_ne!("a".is_empty(), true);
|
2022-06-16 10:39:06 -05:00
|
|
|
/// ```
|
2021-04-22 04:31:13 -05:00
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2021-04-22 04:31:13 -05:00
|
|
|
/// assert!(!"a".is_empty());
|
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.53.0"]
|
2021-04-22 04:31:13 -05:00
|
|
|
pub BOOL_ASSERT_COMPARISON,
|
|
|
|
style,
|
|
|
|
"Using a boolean as comparison value in an assert_* macro when there is no need"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(BoolAssertComparison => [BOOL_ASSERT_COMPARISON]);
|
|
|
|
|
2021-09-08 09:31:47 -05:00
|
|
|
fn is_bool_lit(e: &Expr<'_>) -> bool {
|
2021-04-22 04:31:13 -05:00
|
|
|
matches!(
|
|
|
|
e.kind,
|
|
|
|
ExprKind::Lit(Lit {
|
2021-09-08 09:31:47 -05:00
|
|
|
node: LitKind::Bool(_),
|
2021-04-22 04:31:13 -05:00
|
|
|
..
|
|
|
|
})
|
|
|
|
) && !e.span.from_expansion()
|
|
|
|
}
|
|
|
|
|
2023-01-27 14:09:08 -06:00
|
|
|
fn is_impl_not_trait_with_bool_out<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
2021-09-08 09:31:47 -05:00
|
|
|
cx.tcx
|
|
|
|
.lang_items()
|
|
|
|
.not_trait()
|
|
|
|
.filter(|trait_id| implements_trait(cx, ty, *trait_id, &[]))
|
|
|
|
.and_then(|trait_id| {
|
|
|
|
cx.tcx.associated_items(trait_id).find_by_name_and_kind(
|
|
|
|
cx.tcx,
|
|
|
|
Ident::from_str("Output"),
|
|
|
|
ty::AssocKind::Type,
|
|
|
|
trait_id,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map_or(false, |assoc_item| {
|
2022-11-17 07:00:35 -06:00
|
|
|
let proj = cx.tcx.mk_projection(assoc_item.def_id, cx.tcx.mk_substs_trait(ty, []));
|
2021-09-08 09:31:47 -05:00
|
|
|
let nty = cx.tcx.normalize_erasing_regions(cx.param_env, proj);
|
|
|
|
|
|
|
|
nty.is_bool()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for BoolAssertComparison {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-01-13 06:18:19 -06:00
|
|
|
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
|
|
|
|
let macro_name = cx.tcx.item_name(macro_call.def_id);
|
|
|
|
if !matches!(
|
|
|
|
macro_name.as_str(),
|
|
|
|
"assert_eq" | "debug_assert_eq" | "assert_ne" | "debug_assert_ne"
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let Some ((a, b, _)) = find_assert_eq_args(cx, expr, macro_call.expn) else { return };
|
2023-01-27 14:09:08 -06:00
|
|
|
|
|
|
|
let a_span = a.span.source_callsite();
|
|
|
|
let b_span = b.span.source_callsite();
|
|
|
|
|
|
|
|
let (lit_span, non_lit_expr) = match (is_bool_lit(a), is_bool_lit(b)) {
|
|
|
|
// assert_eq!(true, b)
|
|
|
|
// ^^^^^^
|
|
|
|
(true, false) => (a_span.until(b_span), b),
|
|
|
|
// assert_eq!(a, true)
|
|
|
|
// ^^^^^^
|
|
|
|
(false, true) => (b_span.with_lo(a_span.hi()), a),
|
2022-01-13 06:18:19 -06:00
|
|
|
// If there are two boolean arguments, we definitely don't understand
|
|
|
|
// what's going on, so better leave things as is...
|
|
|
|
//
|
|
|
|
// Or there is simply no boolean and then we can leave things as is!
|
2023-01-27 14:09:08 -06:00
|
|
|
_ => return,
|
|
|
|
};
|
2021-09-08 09:31:47 -05:00
|
|
|
|
2023-01-27 14:09:08 -06:00
|
|
|
let non_lit_ty = cx.typeck_results().expr_ty(non_lit_expr);
|
|
|
|
|
|
|
|
if !is_impl_not_trait_with_bool_out(cx, non_lit_ty) {
|
2022-01-13 06:18:19 -06:00
|
|
|
// At this point the expression which is not a boolean
|
|
|
|
// literal does not implement Not trait with a bool output,
|
|
|
|
// so we cannot suggest to rewrite our code
|
|
|
|
return;
|
2021-04-22 04:31:13 -05:00
|
|
|
}
|
2022-01-13 06:18:19 -06:00
|
|
|
|
2023-01-27 14:09:08 -06:00
|
|
|
if !is_copy(cx, non_lit_ty) {
|
|
|
|
// Only lint with types that are `Copy` because `assert!(x)` takes
|
|
|
|
// ownership of `x` whereas `assert_eq(x, true)` does not
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-13 06:18:19 -06:00
|
|
|
let macro_name = macro_name.as_str();
|
|
|
|
let non_eq_mac = ¯o_name[..macro_name.len() - 3];
|
2023-01-27 14:09:08 -06:00
|
|
|
span_lint_and_then(
|
2022-01-13 06:18:19 -06:00
|
|
|
cx,
|
|
|
|
BOOL_ASSERT_COMPARISON,
|
|
|
|
macro_call.span,
|
2022-10-06 02:44:38 -05:00
|
|
|
&format!("used `{macro_name}!` with a literal bool"),
|
2023-01-27 14:09:08 -06:00
|
|
|
|diag| {
|
|
|
|
// assert_eq!(...)
|
|
|
|
// ^^^^^^^^^
|
|
|
|
let name_span = cx.sess().source_map().span_until_char(macro_call.span, '!');
|
|
|
|
|
|
|
|
diag.multipart_suggestion(
|
|
|
|
format!("replace it with `{non_eq_mac}!(..)`"),
|
|
|
|
vec![(name_span, non_eq_mac.to_string()), (lit_span, String::new())],
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
2022-01-13 06:18:19 -06:00
|
|
|
);
|
2021-04-22 04:31:13 -05:00
|
|
|
}
|
|
|
|
}
|