Skip to content

Commit 5445362

Browse files
committed
Clean up after if chain removal
1 parent f4d7778 commit 5445362

File tree

231 files changed

+1455
-1849
lines changed

Some content is hidden

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

231 files changed

+1455
-1849
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/attrs.rs

-1
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::{

Diff for: clippy_lints/src/blocks_in_if_conditions.rs

-1
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};

Diff for: clippy_lints/src/booleans.rs

-1
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};

Diff for: clippy_lints/src/borrow_deref_ref.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,25 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
5454
&& !addrof_target.span.from_expansion()
5555
&& let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind
5656
&& !deref_target.span.from_expansion()
57-
&& !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) )
57+
&& !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..))
5858
&& let ref_ty = cx.typeck_results().expr_ty(deref_target)
5959
&& let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind()
6060
&& !is_from_proc_macro(cx, e)
6161
{
62-
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) {
62+
if let Some(parent_expr) = get_parent_expr(cx, e) {
63+
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..))
64+
&& !is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id)
65+
{
6666
return;
6767
}
6868

6969
// 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;
70+
if matches!(
71+
deref_target.kind,
72+
ExprKind::Path(..) | ExprKind::Field(..) | ExprKind::Index(..) | ExprKind::Unary(UnOp::Deref, ..)
73+
) && matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _))
74+
{
75+
return;
7676
}
7777
}
7878

@@ -86,12 +86,12 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
8686
e.span,
8787
"if you would like to reborrow, try removing `&*`",
8888
snippet_opt(cx, deref_target.span).unwrap(),
89-
Applicability::MachineApplicable
89+
Applicability::MachineApplicable,
9090
);
9191

9292
// has deref trait -> give 2 help
9393
// doesn't have deref trait -> give 1 help
94-
if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait(){
94+
if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait() {
9595
if !implements_trait(cx, *inner_ty, deref_trait_id, &[]) {
9696
return;
9797
}
@@ -100,16 +100,11 @@ impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
100100
diag.span_suggestion(
101101
e.span,
102102
"if you would like to deref, try using `&**`",
103-
format!(
104-
"&**{}",
105-
&snippet_opt(cx, deref_target.span).unwrap(),
106-
),
107-
Applicability::MaybeIncorrect
103+
format!("&**{}", &snippet_opt(cx, deref_target.span).unwrap()),
104+
Applicability::MaybeIncorrect,
108105
);
109-
110-
}
106+
},
111107
);
112-
113108
}
114109
}
115110
}

Diff for: clippy_lints/src/cargo/multiple_crate_versions.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use cargo_metadata::{DependencyKind, Metadata, Node, Package, PackageId};
44
use clippy_utils::diagnostics::span_lint;
5-
use if_chain::if_chain;
65
use itertools::Itertools;
76
use rustc_hir::def_id::LOCAL_CRATE;
87
use rustc_lint::LateContext;
@@ -16,9 +15,13 @@ pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata) {
1615
packages.sort_by(|a, b| a.name.cmp(&b.name));
1716

1817
if let Some(resolve) = &metadata.resolve
19-
&& let Some(local_id) = packages
20-
.iter()
21-
.find_map(|p| if p.name == local_name.as_str() { Some(&p.id) } else { None })
18+
&& let Some(local_id) = packages.iter().find_map(|p| {
19+
if p.name == local_name.as_str() {
20+
Some(&p.id)
21+
} else {
22+
None
23+
}
24+
})
2225
{
2326
for (name, group) in &packages.iter().group_by(|p| p.name.clone()) {
2427
let group: Vec<&Package> = group.collect();

Diff for: clippy_lints/src/cargo/wildcard_dependencies.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use cargo_metadata::Metadata;
22
use clippy_utils::diagnostics::span_lint;
3-
use if_chain::if_chain;
43
use rustc_lint::LateContext;
54
use rustc_span::source_map::DUMMY_SP;
65

Diff for: clippy_lints/src/casts/cast_sign_loss.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::span_lint;
33
use clippy_utils::{method_chain_args, sext};
4-
use if_chain::if_chain;
54
use rustc_hir::{Expr, ExprKind};
65
use rustc_lint::LateContext;
76
use rustc_middle::ty::{self, Ty};

Diff for: clippy_lints/src/casts/cast_slice_different_sizes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source;
4-
use if_chain::if_chain;
54
use rustc_ast::Mutability;
65
use rustc_hir::{Expr, ExprKind, Node};
76
use rustc_lint::LateContext;

Diff for: clippy_lints/src/casts/cast_slice_from_raw_parts.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_context;
4-
use if_chain::if_chain;
54
use rustc_errors::Applicability;
65
use rustc_hir::def_id::DefId;
76
use rustc_hir::{Expr, ExprKind};
@@ -37,7 +36,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
3736
{
3837
let func = match rpk {
3938
RawPartsKind::Immutable => "from_raw_parts",
40-
RawPartsKind::Mutable => "from_raw_parts_mut"
39+
RawPartsKind::Mutable => "from_raw_parts_mut",
4140
};
4241
let span = expr.span;
4342
let mut applicability = Applicability::MachineApplicable;
@@ -50,7 +49,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
5049
&format!("casting the result of `{func}` to {cast_to}"),
5150
"replace with",
5251
format!("core::ptr::slice_{func}({ptr}, {len})"),
53-
applicability
52+
applicability,
5453
);
5554
}
5655
}

Diff for: clippy_lints/src/casts/char_lit_as_u8.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_applicability;
3-
use if_chain::if_chain;
43
use rustc_ast::LitKind;
54
use rustc_errors::Applicability;
65
use rustc_hir::{Expr, ExprKind};
@@ -34,6 +33,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
3433
applicability,
3534
);
3635
}
37-
});
36+
},
37+
);
3838
}
3939
}

Diff for: clippy_lints/src/casts/ptr_cast_constness.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::sugg::Sugg;
4-
use if_chain::if_chain;
54
use rustc_errors::Applicability;
65
use rustc_hir::{Expr, Mutability};
76
use rustc_lint::LateContext;
@@ -18,10 +17,18 @@ pub(super) fn check<'tcx>(
1817
msrv: &Msrv,
1918
) {
2019
if msrv.meets(msrvs::POINTER_CAST_CONSTNESS)
21-
&& let ty::RawPtr(TypeAndMut { mutbl: from_mutbl, ty: from_ty }) = cast_from.kind()
22-
&& let ty::RawPtr(TypeAndMut { mutbl: to_mutbl, ty: to_ty }) = cast_to.kind()
23-
&& matches!((from_mutbl, to_mutbl),
24-
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not))
20+
&& let ty::RawPtr(TypeAndMut {
21+
mutbl: from_mutbl,
22+
ty: from_ty,
23+
}) = cast_from.kind()
24+
&& let ty::RawPtr(TypeAndMut {
25+
mutbl: to_mutbl,
26+
ty: to_ty,
27+
}) = cast_to.kind()
28+
&& matches!(
29+
(from_mutbl, to_mutbl),
30+
(Mutability::Not, Mutability::Mut) | (Mutability::Mut, Mutability::Not)
31+
)
2532
&& from_ty == to_ty
2633
{
2734
let sugg = Sugg::hir(cx, cast_expr, "_");

Diff for: clippy_lints/src/casts/unnecessary_cast.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::numeric_literal::NumericLiteral;
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::visitors::{for_each_expr, Visitable};
55
use clippy_utils::{get_parent_expr, get_parent_node, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
6-
use if_chain::if_chain;
76
use rustc_ast::{LitFloatType, LitIntType, LitKind};
87
use rustc_errors::Applicability;
98
use rustc_hir::def::{DefKind, Res};
@@ -52,7 +51,9 @@ pub(super) fn check<'tcx>(
5251
cx,
5352
UNNECESSARY_CAST,
5453
expr.span,
55-
&format!("casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"),
54+
&format!(
55+
"casting raw pointers to the same type and constness is unnecessary (`{cast_from}` -> `{cast_to}`)"
56+
),
5657
"try",
5758
cast_str.clone(),
5859
Applicability::MaybeIncorrect,
@@ -87,8 +88,8 @@ pub(super) fn check<'tcx>(
8788
if let ExprKind::Cast(_, cast_to) = expr.kind
8889
&& let TyKind::Path(QPath::Resolved(_, path)) = &cast_to.kind
8990
&& let Res::PrimTy(_) = path.res
90-
{}
91-
else {
91+
{
92+
} else {
9293
return false;
9394
}
9495

@@ -108,10 +109,13 @@ pub(super) fn check<'tcx>(
108109
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node)
109110
&& let from_nbits = 128 - n.leading_zeros()
110111
&& let to_nbits = fp_ty_mantissa_nbits(cast_to)
111-
&& from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal()
112+
&& from_nbits != 0
113+
&& to_nbits != 0
114+
&& from_nbits <= to_nbits
115+
&& num_lit.is_decimal()
112116
{
113117
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
114-
return true
118+
return true;
115119
}
116120

117121
match lit.node {

Diff for: clippy_lints/src/checked_conversions.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use clippy_config::msrvs::{self, Msrv};
44
use clippy_utils::diagnostics::span_lint_and_sugg;
55
use clippy_utils::source::snippet_with_applicability;
66
use clippy_utils::{in_constant, is_integer_literal, SpanlessEq};
7-
use if_chain::if_chain;
87
use rustc_errors::Applicability;
98
use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, QPath, TyKind};
109
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -58,7 +57,6 @@ impl<'tcx> LateLintPass<'tcx> for CheckedConversions {
5857
let result = if !in_constant(cx, item.hir_id)
5958
&& !in_external_macro(cx.sess(), item.span)
6059
&& let ExprKind::Binary(op, left, right) = &item.kind
61-
6260
{
6361
match op.node {
6462
BinOpKind::Ge | BinOpKind::Le => single_check(item),
@@ -192,12 +190,11 @@ impl ConversionType {
192190
/// Check for `expr <= (to_type::MAX as from_type)`
193191
fn check_upper_bound<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<Conversion<'tcx>> {
194192
if let ExprKind::Binary(ref op, left, right) = &expr.kind
195-
&& let Some((candidate, check)) = normalize_le_ge(op, left, right)
196-
&& let Some((from, to)) = get_types_from_cast(check, INTS, "max_value", "MAX")
197-
198-
{
199-
Conversion::try_new(candidate, from, to)
200-
} else {
193+
&& let Some((candidate, check)) = normalize_le_ge(op, left, right)
194+
&& let Some((from, to)) = get_types_from_cast(check, INTS, "max_value", "MAX")
195+
{
196+
Conversion::try_new(candidate, from, to)
197+
} else {
201198
None
202199
}
203200
}
@@ -243,7 +240,6 @@ fn get_types_from_cast<'a>(
243240
// to_type::max_value(), from_type
244241
&& let TyKind::Path(ref from_type_path) = &from_type.kind
245242
&& let Some(from_sym) = int_ty_to_sym(from_type_path)
246-
247243
{
248244
Some((limit, from_sym))
249245
} else {
@@ -257,7 +253,6 @@ fn get_types_from_cast<'a>(
257253
// `from_type::from`
258254
&& let ExprKind::Path(ref path) = &from_func.kind
259255
&& let Some(from_sym) = get_implementing_type(path, INTS, "from")
260-
261256
{
262257
Some((limit, from_sym))
263258
} else {

Diff for: clippy_lints/src/collapsible_if.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
1616
use clippy_utils::source::{snippet, snippet_block, snippet_block_with_applicability};
1717
use clippy_utils::sugg::Sugg;
18-
use if_chain::if_chain;
1918
use rustc_ast::ast;
2019
use rustc_errors::Applicability;
2120
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -131,7 +130,11 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, then_span: Span, else_:
131130
// Prevent "elseif"
132131
// Check that the "else" is followed by whitespace
133132
let up_to_else = then_span.between(block.span);
134-
let requires_space = if let Some(c) = snippet(cx, up_to_else, "..").chars().last() { !c.is_whitespace() } else { false };
133+
let requires_space = if let Some(c) = snippet(cx, up_to_else, "..").chars().last() {
134+
!c.is_whitespace()
135+
} else {
136+
false
137+
};
135138

136139
let mut applicability = Applicability::MachineApplicable;
137140
span_lint_and_sugg(
@@ -160,21 +163,27 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: &
160163
&& let ctxt = expr.span.ctxt()
161164
&& inner.span.ctxt() == ctxt
162165
{
163-
span_lint_and_then(cx, COLLAPSIBLE_IF, expr.span, "this `if` statement can be collapsed", |diag| {
164-
let mut app = Applicability::MachineApplicable;
165-
let lhs = Sugg::ast(cx, check, "..", ctxt, &mut app);
166-
let rhs = Sugg::ast(cx, check_inner, "..", ctxt, &mut app);
167-
diag.span_suggestion(
168-
expr.span,
169-
"collapse nested if block",
170-
format!(
171-
"if {} {}",
172-
lhs.and(&rhs),
173-
snippet_block(cx, content.span, "..", Some(expr.span)),
174-
),
175-
app, // snippet
176-
);
177-
});
166+
span_lint_and_then(
167+
cx,
168+
COLLAPSIBLE_IF,
169+
expr.span,
170+
"this `if` statement can be collapsed",
171+
|diag| {
172+
let mut app = Applicability::MachineApplicable;
173+
let lhs = Sugg::ast(cx, check, "..", ctxt, &mut app);
174+
let rhs = Sugg::ast(cx, check_inner, "..", ctxt, &mut app);
175+
diag.span_suggestion(
176+
expr.span,
177+
"collapse nested if block",
178+
format!(
179+
"if {} {}",
180+
lhs.and(&rhs),
181+
snippet_block(cx, content.span, "..", Some(expr.span)),
182+
),
183+
app, // snippet
184+
);
185+
},
186+
);
178187
}
179188
}
180189

0 commit comments

Comments
 (0)