2022-10-29 07:15:51 -05:00
|
|
|
use clippy_utils::higher::If;
|
2022-09-14 14:28:54 -05:00
|
|
|
use rustc_ast::LitKind;
|
2022-06-30 08:57:15 -05:00
|
|
|
use rustc_hir::{Block, ExprKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
|
2023-07-03 12:42:48 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use clippy_utils::sugg::Sugg;
|
|
|
|
use clippy_utils::{in_constant, is_else_clause, is_integer_literal};
|
2022-06-30 08:57:15 -05:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Instead of using an if statement to convert a bool to an int,
|
|
|
|
/// this lint suggests using a `from()` function or an `as` coercion.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
2022-11-10 12:42:20 -06:00
|
|
|
/// Coercion or `from()` is another way to convert bool to a number.
|
2022-06-30 08:57:15 -05:00
|
|
|
/// Both methods are guaranteed to return 1 for true, and 0 for false.
|
|
|
|
///
|
|
|
|
/// See https://doc.rust-lang.org/std/primitive.bool.html#impl-From%3Cbool%3E
|
|
|
|
///
|
|
|
|
/// ### Example
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2022-06-30 08:57:15 -05:00
|
|
|
/// # let condition = false;
|
|
|
|
/// if condition {
|
|
|
|
/// 1_i64
|
|
|
|
/// } else {
|
|
|
|
/// 0
|
|
|
|
/// };
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2022-06-30 08:57:15 -05:00
|
|
|
/// # let condition = false;
|
|
|
|
/// i64::from(condition);
|
|
|
|
/// ```
|
|
|
|
/// or
|
2023-10-23 08:49:18 -05:00
|
|
|
/// ```no_run
|
2022-06-30 08:57:15 -05:00
|
|
|
/// # let condition = false;
|
|
|
|
/// condition as i64;
|
|
|
|
/// ```
|
|
|
|
#[clippy::version = "1.65.0"]
|
|
|
|
pub BOOL_TO_INT_WITH_IF,
|
2022-11-10 12:42:20 -06:00
|
|
|
pedantic,
|
2022-06-30 08:57:15 -05:00
|
|
|
"using if to convert bool to int"
|
|
|
|
}
|
|
|
|
declare_lint_pass!(BoolToIntWithIf => [BOOL_TO_INT_WITH_IF]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf {
|
2022-10-28 07:45:16 -05:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
|
|
|
|
if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
|
|
|
|
check_if_else(cx, expr);
|
2022-06-30 08:57:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 07:45:16 -05:00
|
|
|
fn check_if_else<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
|
2023-11-02 11:12:25 -05:00
|
|
|
if let Some(If {
|
|
|
|
cond,
|
|
|
|
then,
|
|
|
|
r#else: Some(r#else),
|
|
|
|
}) = If::hir(expr)
|
2022-06-30 08:57:15 -05:00
|
|
|
&& let Some(then_lit) = int_literal(then)
|
2022-10-29 07:15:51 -05:00
|
|
|
&& let Some(else_lit) = int_literal(r#else)
|
2022-06-30 08:57:15 -05:00
|
|
|
{
|
2022-10-01 10:55:22 -05:00
|
|
|
let inverted = if is_integer_literal(then_lit, 1) && is_integer_literal(else_lit, 0) {
|
2022-09-14 00:50:47 -05:00
|
|
|
false
|
2022-10-01 10:55:22 -05:00
|
|
|
} else if is_integer_literal(then_lit, 0) && is_integer_literal(else_lit, 1) {
|
2022-09-14 00:50:47 -05:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
// Expression isn't boolean, exit
|
|
|
|
return;
|
|
|
|
};
|
2022-06-30 08:57:15 -05:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2022-09-14 14:28:54 -05:00
|
|
|
let snippet = {
|
2022-10-29 07:15:51 -05:00
|
|
|
let mut sugg = Sugg::hir_with_applicability(cx, cond, "..", &mut applicability);
|
2022-09-14 14:28:54 -05:00
|
|
|
if inverted {
|
|
|
|
sugg = !sugg;
|
|
|
|
}
|
|
|
|
sugg
|
2022-06-30 08:57:15 -05:00
|
|
|
};
|
|
|
|
|
2022-10-28 07:45:16 -05:00
|
|
|
let ty = cx.typeck_results().expr_ty(then_lit); // then and else must be of same type
|
2022-06-30 08:57:15 -05:00
|
|
|
|
|
|
|
let suggestion = {
|
2022-10-28 07:45:16 -05:00
|
|
|
let wrap_in_curly = is_else_clause(cx.tcx, expr);
|
2022-09-14 14:28:54 -05:00
|
|
|
let mut s = Sugg::NonParen(format!("{ty}::from({snippet})").into());
|
|
|
|
if wrap_in_curly {
|
|
|
|
s = s.blockify();
|
|
|
|
}
|
|
|
|
s
|
2022-06-30 08:57:15 -05:00
|
|
|
}; // when used in else clause if statement should be wrapped in curly braces
|
|
|
|
|
2022-09-14 14:28:54 -05:00
|
|
|
let into_snippet = snippet.clone().maybe_par();
|
|
|
|
let as_snippet = snippet.as_ty(ty);
|
2022-09-14 00:50:47 -05:00
|
|
|
|
2023-11-02 11:12:25 -05:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
2022-06-30 08:57:15 -05:00
|
|
|
BOOL_TO_INT_WITH_IF,
|
|
|
|
expr.span,
|
|
|
|
"boolean to int conversion using if",
|
|
|
|
|diag| {
|
2023-11-02 11:12:25 -05:00
|
|
|
diag.span_suggestion(expr.span, "replace with from", suggestion, applicability);
|
|
|
|
diag.note(format!(
|
|
|
|
"`{as_snippet}` or `{into_snippet}.into()` can also be valid options"
|
|
|
|
));
|
|
|
|
},
|
|
|
|
);
|
2022-06-30 08:57:15 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// If block contains only a int literal expression, return literal expression
|
|
|
|
fn int_literal<'tcx>(expr: &'tcx rustc_hir::Expr<'tcx>) -> Option<&'tcx rustc_hir::Expr<'tcx>> {
|
|
|
|
if let ExprKind::Block(block, _) = expr.kind
|
|
|
|
&& let Block {
|
2023-11-02 11:12:25 -05:00
|
|
|
stmts: [], // Shouldn't lint if statements with side effects
|
2022-06-30 08:57:15 -05:00
|
|
|
expr: Some(expr),
|
|
|
|
..
|
|
|
|
} = block
|
|
|
|
&& let ExprKind::Lit(lit) = &expr.kind
|
|
|
|
&& let LitKind::Int(_, _) = lit.node
|
|
|
|
{
|
|
|
|
Some(expr)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|