Skip to content

Commit 9681b4a

Browse files
committed
Run if-to-let-chain clippy*/**/*.rs
https://github.com/Alexendoo/if-to-let-chain
1 parent 6be0f74 commit 9681b4a

File tree

238 files changed

+7895
-8761
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

238 files changed

+7895
-8761
lines changed

clippy_lints/src/allow_attributes.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,22 @@ declare_lint_pass!(AllowAttribute => [ALLOW_ATTRIBUTES]);
5252
impl LateLintPass<'_> for AllowAttribute {
5353
// Separate each crate's features.
5454
fn check_attribute<'cx>(&mut self, cx: &LateContext<'cx>, attr: &'cx Attribute) {
55-
if_chain! {
56-
if !in_external_macro(cx.sess(), attr.span);
57-
if cx.tcx.features().lint_reasons;
58-
if let AttrStyle::Outer = attr.style;
59-
if let Some(ident) = attr.ident();
60-
if ident.name == rustc_span::symbol::sym::allow;
61-
if !is_from_proc_macro(cx, &attr);
62-
then {
63-
span_lint_and_sugg(
64-
cx,
65-
ALLOW_ATTRIBUTES,
66-
ident.span,
67-
"#[allow] attribute found",
68-
"replace it with",
69-
"expect".into(),
70-
Applicability::MachineApplicable,
71-
);
72-
}
55+
if !in_external_macro(cx.sess(), attr.span)
56+
&& cx.tcx.features().lint_reasons
57+
&& let AttrStyle::Outer = attr.style
58+
&& let Some(ident) = attr.ident()
59+
&& ident.name == rustc_span::symbol::sym::allow
60+
&& !is_from_proc_macro(cx, &attr)
61+
{
62+
span_lint_and_sugg(
63+
cx,
64+
ALLOW_ATTRIBUTES,
65+
ident.span,
66+
"#[allow] attribute found",
67+
"replace it with",
68+
"expect".into(),
69+
Applicability::MachineApplicable,
70+
);
7371
}
7472
}
7573
}

clippy_lints/src/attrs.rs

+57-67
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,11 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
471471
return;
472472
}
473473
for item in items {
474-
if_chain! {
475-
if let NestedMetaItem::MetaItem(mi) = &item;
476-
if let MetaItemKind::NameValue(lit) = &mi.kind;
477-
if mi.has_name(sym::since);
478-
then {
479-
check_semver(cx, item.span(), lit);
480-
}
474+
if let NestedMetaItem::MetaItem(mi) = &item
475+
&& let MetaItemKind::NameValue(lit) = &mi.kind
476+
&& mi.has_name(sym::since)
477+
{
478+
check_semver(cx, item.span(), lit);
481479
}
482480
}
483481
}
@@ -580,15 +578,13 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
580578

581579
/// Returns the lint name if it is clippy lint.
582580
fn extract_clippy_lint(lint: &NestedMetaItem) -> Option<Symbol> {
583-
if_chain! {
584-
if let Some(meta_item) = lint.meta_item();
585-
if meta_item.path.segments.len() > 1;
586-
if let tool_name = meta_item.path.segments[0].ident;
587-
if tool_name.name == sym::clippy;
588-
then {
589-
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
590-
return Some(lint_name);
591-
}
581+
if let Some(meta_item) = lint.meta_item()
582+
&& meta_item.path.segments.len() > 1
583+
&& let tool_name = meta_item.path.segments[0].ident
584+
&& tool_name.name == sym::clippy
585+
{
586+
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
587+
return Some(lint_name);
592588
}
593589
None
594590
}
@@ -857,40 +853,38 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
857853
}
858854

859855
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) {
860-
if_chain! {
861-
if msrv.meets(msrvs::TOOL_ATTRIBUTES);
856+
if msrv.meets(msrvs::TOOL_ATTRIBUTES)
862857
// check cfg_attr
863-
if attr.has_name(sym::cfg_attr);
864-
if let Some(items) = attr.meta_item_list();
865-
if items.len() == 2;
858+
&& attr.has_name(sym::cfg_attr)
859+
&& let Some(items) = attr.meta_item_list()
860+
&& items.len() == 2
866861
// check for `rustfmt`
867-
if let Some(feature_item) = items[0].meta_item();
868-
if feature_item.has_name(sym::rustfmt);
862+
&& let Some(feature_item) = items[0].meta_item()
863+
&& feature_item.has_name(sym::rustfmt)
869864
// check for `rustfmt_skip` and `rustfmt::skip`
870-
if let Some(skip_item) = &items[1].meta_item();
871-
if skip_item.has_name(sym!(rustfmt_skip))
865+
&& let Some(skip_item) = &items[1].meta_item()
866+
&& (skip_item.has_name(sym!(rustfmt_skip))
872867
|| skip_item
873868
.path
874869
.segments
875870
.last()
876871
.expect("empty path in attribute")
877872
.ident
878873
.name
879-
== sym::skip;
874+
== sym::skip)
880875
// Only lint outer attributes, because custom inner attributes are unstable
881876
// Tracking issue: https://github.com/rust-lang/rust/issues/54726
882-
if attr.style == AttrStyle::Outer;
883-
then {
884-
span_lint_and_sugg(
885-
cx,
886-
DEPRECATED_CFG_ATTR,
887-
attr.span,
888-
"`cfg_attr` is deprecated for rustfmt and got replaced by tool attributes",
889-
"use",
890-
"#[rustfmt::skip]".to_string(),
891-
Applicability::MachineApplicable,
892-
);
893-
}
877+
&& attr.style == AttrStyle::Outer
878+
{
879+
span_lint_and_sugg(
880+
cx,
881+
DEPRECATED_CFG_ATTR,
882+
attr.span,
883+
"`cfg_attr` is deprecated for rustfmt and got replaced by tool attributes",
884+
"use",
885+
"#[rustfmt::skip]".to_string(),
886+
Applicability::MachineApplicable,
887+
);
894888
}
895889
}
896890

@@ -990,12 +984,10 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
990984
mismatched.extend(find_mismatched_target_os(list));
991985
},
992986
MetaItemKind::Word => {
993-
if_chain! {
994-
if let Some(ident) = meta.ident();
995-
if let Some(os) = find_os(ident.name.as_str());
996-
then {
997-
mismatched.push((os, ident.span));
998-
}
987+
if let Some(ident) = meta.ident()
988+
&& let Some(os) = find_os(ident.name.as_str())
989+
{
990+
mismatched.push((os, ident.span));
999991
}
1000992
},
1001993
MetaItemKind::NameValue(..) => {},
@@ -1006,30 +998,28 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
1006998
mismatched
1007999
}
10081000

1009-
if_chain! {
1010-
if attr.has_name(sym::cfg);
1011-
if let Some(list) = attr.meta_item_list();
1012-
let mismatched = find_mismatched_target_os(&list);
1013-
if !mismatched.is_empty();
1014-
then {
1015-
let mess = "operating system used in target family position";
1016-
1017-
span_lint_and_then(cx, MISMATCHED_TARGET_OS, attr.span, mess, |diag| {
1018-
// Avoid showing the unix suggestion multiple times in case
1019-
// we have more than one mismatch for unix-like systems
1020-
let mut unix_suggested = false;
1021-
1022-
for (os, span) in mismatched {
1023-
let sugg = format!("target_os = \"{os}\"");
1024-
diag.span_suggestion(span, "try", sugg, Applicability::MaybeIncorrect);
1025-
1026-
if !unix_suggested && is_unix(os) {
1027-
diag.help("did you mean `unix`?");
1028-
unix_suggested = true;
1029-
}
1001+
if attr.has_name(sym::cfg)
1002+
&& let Some(list) = attr.meta_item_list()
1003+
&& let mismatched = find_mismatched_target_os(&list)
1004+
&& !mismatched.is_empty()
1005+
{
1006+
let mess = "operating system used in target family position";
1007+
1008+
span_lint_and_then(cx, MISMATCHED_TARGET_OS, attr.span, mess, |diag| {
1009+
// Avoid showing the unix suggestion multiple times in case
1010+
// we have more than one mismatch for unix-like systems
1011+
let mut unix_suggested = false;
1012+
1013+
for (os, span) in mismatched {
1014+
let sugg = format!("target_os = \"{os}\"");
1015+
diag.span_suggestion(span, "try", sugg, Applicability::MaybeIncorrect);
1016+
1017+
if !unix_suggested && is_unix(os) {
1018+
diag.help("did you mean `unix`?");
1019+
unix_suggested = true;
10301020
}
1031-
});
1032-
}
1021+
}
1022+
});
10331023
}
10341024
}
10351025

clippy_lints/src/blocks_in_if_conditions.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,13 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
114114
let _: Option<!> = for_each_expr(cond, |e| {
115115
if let ExprKind::Closure(closure) = e.kind {
116116
// do not lint if the closure is called using an iterator (see #1141)
117-
if_chain! {
118-
if let Some(parent) = get_parent_expr(cx, e);
119-
if let ExprKind::MethodCall(_, self_arg, _, _) = &parent.kind;
120-
let caller = cx.typeck_results().expr_ty(self_arg);
121-
if let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
122-
if implements_trait(cx, caller, iter_id, &[]);
123-
then {
124-
return ControlFlow::Continue(Descend::No);
125-
}
117+
if let Some(parent) = get_parent_expr(cx, e)
118+
&& let ExprKind::MethodCall(_, self_arg, _, _) = &parent.kind
119+
&& let caller = cx.typeck_results().expr_ty(self_arg)
120+
&& let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator)
121+
&& implements_trait(cx, caller, iter_id, &[])
122+
{
123+
return ControlFlow::Continue(Descend::No);
126124
}
127125

128126
let body = cx.tcx.hir().body(closure.body);

clippy_lints/src/booleans.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,15 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
152152
return Ok(Bool::Term(n as u8));
153153
}
154154

155-
if_chain! {
156-
if let ExprKind::Binary(e_binop, e_lhs, e_rhs) = &e.kind;
157-
if implements_ord(self.cx, e_lhs);
158-
if let ExprKind::Binary(expr_binop, expr_lhs, expr_rhs) = &expr.kind;
159-
if negate(e_binop.node) == Some(expr_binop.node);
160-
if eq_expr_value(self.cx, e_lhs, expr_lhs);
161-
if eq_expr_value(self.cx, e_rhs, expr_rhs);
162-
then {
163-
#[expect(clippy::cast_possible_truncation)]
164-
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
165-
}
155+
if let ExprKind::Binary(e_binop, e_lhs, e_rhs) = &e.kind
156+
&& implements_ord(self.cx, e_lhs)
157+
&& let ExprKind::Binary(expr_binop, expr_lhs, expr_rhs) = &expr.kind
158+
&& negate(e_binop.node) == Some(expr_binop.node)
159+
&& eq_expr_value(self.cx, e_lhs, expr_lhs)
160+
&& eq_expr_value(self.cx, e_rhs, expr_rhs)
161+
{
162+
#[expect(clippy::cast_possible_truncation)]
163+
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
166164
}
167165
}
168166
let n = self.terminals.len();

clippy_lints/src/borrow_deref_ref.rs

+52-54
Original file line numberDiff line numberDiff line change
@@ -49,69 +49,67 @@ declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]);
4949

5050
impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
5151
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &rustc_hir::Expr<'tcx>) {
52-
if_chain! {
53-
if !e.span.from_expansion();
54-
if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind;
55-
if !addrof_target.span.from_expansion();
56-
if let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind;
57-
if !deref_target.span.from_expansion();
58-
if !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) );
59-
let ref_ty = cx.typeck_results().expr_ty(deref_target);
60-
if let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind();
61-
if !is_from_proc_macro(cx, e);
62-
then{
52+
if !e.span.from_expansion()
53+
&& let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind
54+
&& !addrof_target.span.from_expansion()
55+
&& let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind
56+
&& !deref_target.span.from_expansion()
57+
&& !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) )
58+
&& let ref_ty = cx.typeck_results().expr_ty(deref_target)
59+
&& let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind()
60+
&& !is_from_proc_macro(cx, e)
61+
{
6362

64-
if let Some(parent_expr) = get_parent_expr(cx, e){
65-
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) &&
66-
!is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) {
67-
return;
68-
}
63+
if let Some(parent_expr) = get_parent_expr(cx, e){
64+
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) &&
65+
!is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) {
66+
return;
67+
}
6968

70-
// modification to `&mut &*x` is different from `&mut x`
71-
if matches!(deref_target.kind, ExprKind::Path(..)
72-
| ExprKind::Field(..)
73-
| ExprKind::Index(..)
74-
| ExprKind::Unary(UnOp::Deref, ..))
75-
&& matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) {
76-
return;
77-
}
69+
// modification to `&mut &*x` is different from `&mut x`
70+
if matches!(deref_target.kind, ExprKind::Path(..)
71+
| ExprKind::Field(..)
72+
| ExprKind::Index(..)
73+
| ExprKind::Unary(UnOp::Deref, ..))
74+
&& matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) {
75+
return;
7876
}
77+
}
7978

80-
span_lint_and_then(
81-
cx,
82-
BORROW_DEREF_REF,
83-
e.span,
84-
"deref on an immutable reference",
85-
|diag| {
86-
diag.span_suggestion(
87-
e.span,
88-
"if you would like to reborrow, try removing `&*`",
89-
snippet_opt(cx, deref_target.span).unwrap(),
90-
Applicability::MachineApplicable
91-
);
79+
span_lint_and_then(
80+
cx,
81+
BORROW_DEREF_REF,
82+
e.span,
83+
"deref on an immutable reference",
84+
|diag| {
85+
diag.span_suggestion(
86+
e.span,
87+
"if you would like to reborrow, try removing `&*`",
88+
snippet_opt(cx, deref_target.span).unwrap(),
89+
Applicability::MachineApplicable
90+
);
9291

93-
// has deref trait -> give 2 help
94-
// doesn't have deref trait -> give 1 help
95-
if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait(){
96-
if !implements_trait(cx, *inner_ty, deref_trait_id, &[]) {
97-
return;
98-
}
92+
// has deref trait -> give 2 help
93+
// doesn't have deref trait -> give 1 help
94+
if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait(){
95+
if !implements_trait(cx, *inner_ty, deref_trait_id, &[]) {
96+
return;
9997
}
98+
}
10099

101-
diag.span_suggestion(
102-
e.span,
103-
"if you would like to deref, try using `&**`",
104-
format!(
105-
"&**{}",
106-
&snippet_opt(cx, deref_target.span).unwrap(),
107-
),
108-
Applicability::MaybeIncorrect
109-
);
100+
diag.span_suggestion(
101+
e.span,
102+
"if you would like to deref, try using `&**`",
103+
format!(
104+
"&**{}",
105+
&snippet_opt(cx, deref_target.span).unwrap(),
106+
),
107+
Applicability::MaybeIncorrect
108+
);
110109

111-
}
112-
);
110+
}
111+
);
113112

114-
}
115113
}
116114
}
117115
}

0 commit comments

Comments
 (0)