Apply collapsible_if to Clippy itself

Since Clippy uses the `let_chains` feature, there are many occasions to
collapse `if` and `if let` statements.
This commit is contained in:
Samuel Tardieu 2025-03-26 20:38:58 +01:00
parent cd70152470
commit 79c69112dc
135 changed files with 1870 additions and 1913 deletions

View file

@ -107,48 +107,48 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
Applicability::MachineApplicable,
);
}
if let ExprKind::Binary(op, left, right) = expr.kind {
if matches!(op.node, BinOpKind::Eq | BinOpKind::Ne) {
let data = [
(left, self.symbol_str_expr(left, cx)),
(right, self.symbol_str_expr(right, cx)),
];
match data {
// both operands are a symbol string
[(_, Some(left)), (_, Some(right))] => {
if let ExprKind::Binary(op, left, right) = expr.kind
&& matches!(op.node, BinOpKind::Eq | BinOpKind::Ne)
{
let data = [
(left, self.symbol_str_expr(left, cx)),
(right, self.symbol_str_expr(right, cx)),
];
match data {
// both operands are a symbol string
[(_, Some(left)), (_, Some(right))] => {
span_lint_and_sugg(
cx,
UNNECESSARY_SYMBOL_STR,
expr.span,
"unnecessary `Symbol` to string conversion",
"try",
format!(
"{} {} {}",
left.as_symbol_snippet(cx),
op.node.as_str(),
right.as_symbol_snippet(cx),
),
Applicability::MachineApplicable,
);
},
// one of the operands is a symbol string
[(expr, Some(symbol)), _] | [_, (expr, Some(symbol))] => {
// creating an owned string for comparison
if matches!(symbol, SymbolStrExpr::Expr { is_to_owned: true, .. }) {
span_lint_and_sugg(
cx,
UNNECESSARY_SYMBOL_STR,
expr.span,
"unnecessary `Symbol` to string conversion",
"unnecessary string allocation",
"try",
format!(
"{} {} {}",
left.as_symbol_snippet(cx),
op.node.as_str(),
right.as_symbol_snippet(cx),
),
format!("{}.as_str()", symbol.as_symbol_snippet(cx)),
Applicability::MachineApplicable,
);
},
// one of the operands is a symbol string
[(expr, Some(symbol)), _] | [_, (expr, Some(symbol))] => {
// creating an owned string for comparison
if matches!(symbol, SymbolStrExpr::Expr { is_to_owned: true, .. }) {
span_lint_and_sugg(
cx,
UNNECESSARY_SYMBOL_STR,
expr.span,
"unnecessary string allocation",
"try",
format!("{}.as_str()", symbol.as_symbol_snippet(cx)),
Applicability::MachineApplicable,
);
}
},
// nothing found
[(_, None), (_, None)] => {},
}
}
},
// nothing found
[(_, None), (_, None)] => {},
}
}
}

View file

@ -80,22 +80,22 @@ pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool {
.copied();
for item_def_id in lang_items.iter().map(|(_, def_id)| def_id).chain(incoherent_impls) {
let lang_item_path = cx.get_def_path(item_def_id);
if path_syms.starts_with(&lang_item_path) {
if let [item] = &path_syms[lang_item_path.len()..] {
if matches!(
cx.tcx.def_kind(item_def_id),
DefKind::Mod | DefKind::Enum | DefKind::Trait
) {
for child in cx.tcx.module_children(item_def_id) {
if child.ident.name == *item {
return true;
}
if path_syms.starts_with(&lang_item_path)
&& let [item] = &path_syms[lang_item_path.len()..]
{
if matches!(
cx.tcx.def_kind(item_def_id),
DefKind::Mod | DefKind::Enum | DefKind::Trait
) {
for child in cx.tcx.module_children(item_def_id) {
if child.ident.name == *item {
return true;
}
} else {
for child in cx.tcx.associated_item_def_ids(item_def_id) {
if cx.tcx.item_name(*child) == *item {
return true;
}
}
} else {
for child in cx.tcx.associated_item_def_ids(item_def_id) {
if cx.tcx.item_name(*child) == *item {
return true;
}
}
}

View file

@ -205,12 +205,10 @@ pub(super) fn is_lint_ref_type(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> bool {
mutbl: Mutability::Not,
},
) = ty.kind
&& let TyKind::Path(ref path) = inner.kind
&& let Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, inner.hir_id)
{
if let TyKind::Path(ref path) = inner.kind {
if let Res::Def(DefKind::Struct, def_id) = cx.qpath_res(path, inner.hir_id) {
return match_def_path(cx, def_id, &paths::LINT);
}
}
return match_def_path(cx, def_id, &paths::LINT);
}
false

View file

@ -281,10 +281,10 @@ fn path_from_array(exprs: &[Expr<'_>]) -> Option<Vec<String>> {
exprs
.iter()
.map(|expr| {
if let ExprKind::Lit(lit) = &expr.kind {
if let LitKind::Str(sym, _) = lit.node {
return Some((*sym.as_str()).to_owned());
}
if let ExprKind::Lit(lit) = &expr.kind
&& let LitKind::Str(sym, _) = lit.node
{
return Some((*sym.as_str()).to_owned());
}
None

View file

@ -21,28 +21,26 @@ declare_lint_pass!(UnsortedClippyUtilsPaths => [UNSORTED_CLIPPY_UTILS_PATHS]);
impl EarlyLintPass for UnsortedClippyUtilsPaths {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
if let Some(utils) = krate.items.iter().find(|item| item.ident.name.as_str() == "utils") {
if let ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) = utils.kind {
if let Some(paths) = items.iter().find(|item| item.ident.name.as_str() == "paths") {
if let ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) = paths.kind {
let mut last_name: Option<&str> = None;
for item in items {
let name = item.ident.as_str();
if let Some(last_name) = last_name {
if *last_name > *name {
span_lint(
cx,
UNSORTED_CLIPPY_UTILS_PATHS,
item.span,
"this constant should be before the previous constant due to lexical \
if let Some(utils) = krate.items.iter().find(|item| item.ident.name.as_str() == "utils")
&& let ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) = utils.kind
&& let Some(paths) = items.iter().find(|item| item.ident.name.as_str() == "paths")
&& let ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) = paths.kind
{
let mut last_name: Option<&str> = None;
for item in items {
let name = item.ident.as_str();
if let Some(last_name) = last_name
&& *last_name > *name
{
span_lint(
cx,
UNSORTED_CLIPPY_UTILS_PATHS,
item.span,
"this constant should be before the previous constant due to lexical \
ordering",
);
}
}
last_name = Some(name);
}
}
);
}
last_name = Some(name);
}
}
}