misc: use Symbols instead of &strs

This commit is contained in:
Ada Alakbarova 2025-10-08 16:38:57 +02:00
parent 973e596eb0
commit 3833ac3be0
No known key found for this signature in database
2 changed files with 52 additions and 24 deletions

View file

@ -8,6 +8,7 @@ use rustc_hir as hir;
use rustc_hir::def::Res;
use rustc_lint::LateContext;
use rustc_middle::ty::layout::LayoutOf;
use rustc_span::Symbol;
pub fn check(
cx: &LateContext<'_>,
@ -15,7 +16,7 @@ pub fn check(
arith_lhs: &hir::Expr<'_>,
arith_rhs: &hir::Expr<'_>,
unwrap_arg: &hir::Expr<'_>,
arith: &str,
arith: Symbol,
) {
let ty = cx.typeck_results().expr_ty(arith_lhs);
if !ty.is_integral() {
@ -26,35 +27,43 @@ pub fn check(
return;
};
if ty.is_signed() {
let Some(checked_arith) = CheckedArith::new(arith) else {
return;
};
{
use self::MinMax::{Max, Min};
use self::Sign::{Neg, Pos};
use CheckedArith::{Add, Mul, Sub};
let Some(sign) = lit_sign(arith_rhs) else {
return;
};
if ty.is_signed() {
let Some(sign) = lit_sign(arith_rhs) else {
return;
};
match (arith, sign, mm) {
("add", Pos, Max) | ("add", Neg, Min) | ("sub", Neg, Max) | ("sub", Pos, Min) => (),
// "mul" is omitted because lhs can be negative.
_ => return,
}
} else {
match (mm, arith) {
(MinMax::Max, "add" | "mul") | (MinMax::Min, "sub") => (),
_ => return,
match (&checked_arith, sign, mm) {
(Add, Pos, Max) | (Add, Neg, Min) | (Sub, Neg, Max) | (Sub, Pos, Min) => (),
// "mul" is omitted because lhs can be negative.
_ => return,
}
} else {
match (mm, &checked_arith) {
(Max, Add | Mul) | (Min, Sub) => (),
_ => return,
}
}
}
let mut applicability = Applicability::MachineApplicable;
let saturating_arith = checked_arith.as_saturating();
span_lint_and_sugg(
cx,
super::MANUAL_SATURATING_ARITHMETIC,
expr.span,
"manual saturating arithmetic",
format!("consider using `saturating_{arith}`"),
format!("consider using `{saturating_arith}`"),
format!(
"{}.saturating_{arith}({})",
"{}.{saturating_arith}({})",
snippet_with_applicability(cx, arith_lhs.span, "..", &mut applicability),
snippet_with_applicability(cx, arith_rhs.span, "..", &mut applicability),
),
@ -62,6 +71,32 @@ pub fn check(
);
}
enum CheckedArith {
Add,
Sub,
Mul,
}
impl CheckedArith {
fn new(sym: Symbol) -> Option<Self> {
let res = match sym {
sym::checked_add => Self::Add,
sym::checked_sub => Self::Sub,
sym::checked_mul => Self::Mul,
_ => return None,
};
Some(res)
}
fn as_saturating(&self) -> &'static str {
match self {
Self::Add => "saturating_add",
Self::Sub => "saturating_sub",
Self::Mul => "saturating_mul",
}
}
}
#[derive(PartialEq, Eq)]
enum MinMax {
Min,

View file

@ -5507,14 +5507,7 @@ impl Methods {
(sym::unwrap_or, [u_arg]) => {
match method_call(recv) {
Some((arith @ (sym::checked_add | sym::checked_sub | sym::checked_mul), lhs, [rhs], _, _)) => {
manual_saturating_arithmetic::check(
cx,
expr,
lhs,
rhs,
u_arg,
&arith.as_str()[const { "checked_".len() }..],
);
manual_saturating_arithmetic::check(cx, expr, lhs, rhs, u_arg, arith);
},
Some((sym::map, m_recv, [m_arg], span, _)) => {
option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span, self.msrv);