Skip to content

Commit 9a4dd10

Browse files
committed
Auto merge of #11750 - Alexendoo:let-chains, r=flip1995
Replace if_chain with let chains Closes #9353 Let chains are now supported by rustfmt 🎉 The PR is split into two commits 1. The result of running [`if-to-let-chain clippy*/**/*.rs`](https://github.com/Alexendoo/if-to-let-chain) 2. The manual clean up: fixing some errors/formatting, dogfood lints, removing the if_chain internal lint r? `@flip1995` changelog: none
2 parents 6be0f74 + 13b4bb1 commit 9a4dd10

File tree

245 files changed

+8179
-9438
lines changed

Some content is hidden

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

245 files changed

+8179
-9438
lines changed

Diff for: clippy_lints/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ cargo_metadata = "0.15.3"
1414
clippy_config = { path = "../clippy_config" }
1515
clippy_utils = { path = "../clippy_utils" }
1616
declare_clippy_lint = { path = "../declare_clippy_lint" }
17-
if_chain = "1.0"
1817
itertools = "0.10.1"
1918
quine-mc_cluskey = "0.2"
2019
regex-syntax = "0.7"

Diff for: 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
}

Diff for: clippy_lints/src/attrs.rs

+57-68
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sug
55
use clippy_utils::is_from_proc_macro;
66
use clippy_utils::macros::{is_panic, macro_backtrace};
77
use clippy_utils::source::{first_line_of_span, is_present_in_source, snippet_opt, without_block_comments};
8-
use if_chain::if_chain;
98
use rustc_ast::token::{Token, TokenKind};
109
use rustc_ast::tokenstream::TokenTree;
1110
use rustc_ast::{
@@ -471,13 +470,11 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
471470
return;
472471
}
473472
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-
}
473+
if let NestedMetaItem::MetaItem(mi) = &item
474+
&& let MetaItemKind::NameValue(lit) = &mi.kind
475+
&& mi.has_name(sym::since)
476+
{
477+
check_semver(cx, item.span(), lit);
481478
}
482479
}
483480
}
@@ -580,15 +577,13 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
580577

581578
/// Returns the lint name if it is clippy lint.
582579
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-
}
580+
if let Some(meta_item) = lint.meta_item()
581+
&& meta_item.path.segments.len() > 1
582+
&& let tool_name = meta_item.path.segments[0].ident
583+
&& tool_name.name == sym::clippy
584+
{
585+
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
586+
return Some(lint_name);
592587
}
593588
None
594589
}
@@ -857,40 +852,38 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
857852
}
858853

859854
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msrv) {
860-
if_chain! {
861-
if msrv.meets(msrvs::TOOL_ATTRIBUTES);
855+
if msrv.meets(msrvs::TOOL_ATTRIBUTES)
862856
// 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;
857+
&& attr.has_name(sym::cfg_attr)
858+
&& let Some(items) = attr.meta_item_list()
859+
&& items.len() == 2
866860
// check for `rustfmt`
867-
if let Some(feature_item) = items[0].meta_item();
868-
if feature_item.has_name(sym::rustfmt);
861+
&& let Some(feature_item) = items[0].meta_item()
862+
&& feature_item.has_name(sym::rustfmt)
869863
// 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))
864+
&& let Some(skip_item) = &items[1].meta_item()
865+
&& (skip_item.has_name(sym!(rustfmt_skip))
872866
|| skip_item
873867
.path
874868
.segments
875869
.last()
876870
.expect("empty path in attribute")
877871
.ident
878872
.name
879-
== sym::skip;
873+
== sym::skip)
880874
// Only lint outer attributes, because custom inner attributes are unstable
881875
// 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-
}
876+
&& attr.style == AttrStyle::Outer
877+
{
878+
span_lint_and_sugg(
879+
cx,
880+
DEPRECATED_CFG_ATTR,
881+
attr.span,
882+
"`cfg_attr` is deprecated for rustfmt and got replaced by tool attributes",
883+
"use",
884+
"#[rustfmt::skip]".to_string(),
885+
Applicability::MachineApplicable,
886+
);
894887
}
895888
}
896889

@@ -990,12 +983,10 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
990983
mismatched.extend(find_mismatched_target_os(list));
991984
},
992985
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-
}
986+
if let Some(ident) = meta.ident()
987+
&& let Some(os) = find_os(ident.name.as_str())
988+
{
989+
mismatched.push((os, ident.span));
999990
}
1000991
},
1001992
MetaItemKind::NameValue(..) => {},
@@ -1006,30 +997,28 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
1006997
mismatched
1007998
}
1008999

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-
}
1000+
if attr.has_name(sym::cfg)
1001+
&& let Some(list) = attr.meta_item_list()
1002+
&& let mismatched = find_mismatched_target_os(&list)
1003+
&& !mismatched.is_empty()
1004+
{
1005+
let mess = "operating system used in target family position";
1006+
1007+
span_lint_and_then(cx, MISMATCHED_TARGET_OS, attr.span, mess, |diag| {
1008+
// Avoid showing the unix suggestion multiple times in case
1009+
// we have more than one mismatch for unix-like systems
1010+
let mut unix_suggested = false;
1011+
1012+
for (os, span) in mismatched {
1013+
let sugg = format!("target_os = \"{os}\"");
1014+
diag.span_suggestion(span, "try", sugg, Applicability::MaybeIncorrect);
1015+
1016+
if !unix_suggested && is_unix(os) {
1017+
diag.help("did you mean `unix`?");
1018+
unix_suggested = true;
10301019
}
1031-
});
1032-
}
1020+
}
1021+
});
10331022
}
10341023
}
10351024

Diff for: clippy_lints/src/blocks_in_if_conditions.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use clippy_utils::ty::implements_trait;
44
use clippy_utils::visitors::{for_each_expr, Descend};
55
use clippy_utils::{get_parent_expr, higher};
66
use core::ops::ControlFlow;
7-
use if_chain::if_chain;
87
use rustc_errors::Applicability;
98
use rustc_hir::{BlockCheckMode, Expr, ExprKind};
109
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -114,15 +113,13 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
114113
let _: Option<!> = for_each_expr(cond, |e| {
115114
if let ExprKind::Closure(closure) = e.kind {
116115
// 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-
}
116+
if let Some(parent) = get_parent_expr(cx, e)
117+
&& let ExprKind::MethodCall(_, self_arg, _, _) = &parent.kind
118+
&& let caller = cx.typeck_results().expr_ty(self_arg)
119+
&& let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator)
120+
&& implements_trait(cx, caller, iter_id, &[])
121+
{
122+
return ControlFlow::Continue(Descend::No);
126123
}
127124

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

Diff for: clippy_lints/src/booleans.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::eq_expr_value;
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
5-
use if_chain::if_chain;
65
use rustc_ast::ast::LitKind;
76
use rustc_errors::Applicability;
87
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
@@ -152,17 +151,15 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
152151
return Ok(Bool::Term(n as u8));
153152
}
154153

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-
}
154+
if let ExprKind::Binary(e_binop, e_lhs, e_rhs) = &e.kind
155+
&& implements_ord(self.cx, e_lhs)
156+
&& let ExprKind::Binary(expr_binop, expr_lhs, expr_rhs) = &expr.kind
157+
&& negate(e_binop.node) == Some(expr_binop.node)
158+
&& eq_expr_value(self.cx, e_lhs, expr_lhs)
159+
&& eq_expr_value(self.cx, e_rhs, expr_rhs)
160+
{
161+
#[expect(clippy::cast_possible_truncation)]
162+
return Ok(Bool::Not(Box::new(Bool::Term(n as u8))));
166163
}
167164
}
168165
let n = self.terminals.len();

0 commit comments

Comments
 (0)