Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 061004a

Browse files
committedSep 29, 2024·
Auto merge of #13469 - nyurik:apply-ref-option, r=llogiq
Convert `&Option<T>` to `Option<&T>` Run `ref_option` (#13336) on the Clippy's own code, quiet a few hits. Per mentioned video, this may actually improve performance as well. Switch lint to `pedantic` ---- changelog: [`ref_option`]: upgrade lint to `pedantic`
2 parents 7b566c2 + f7d5d9d commit 061004a

File tree

7 files changed

+122
-83
lines changed

7 files changed

+122
-83
lines changed
 

‎clippy_lints/src/cargo/common_metadata.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ pub(super) fn check(cx: &LateContext<'_>, metadata: &Metadata, ignore_publish: b
1010
// only run the lint if publish is `None` (`publish = true` or skipped entirely)
1111
// or if the vector isn't empty (`publish = ["something"]`)
1212
if package.publish.as_ref().filter(|publish| publish.is_empty()).is_none() || ignore_publish {
13-
if is_empty_str(&package.description) {
13+
if is_empty_str(package.description.as_ref()) {
1414
missing_warning(cx, package, "package.description");
1515
}
1616

17-
if is_empty_str(&package.license) && is_empty_str(&package.license_file) {
17+
if is_empty_str(package.license.as_ref()) && is_empty_str(package.license_file.as_ref()) {
1818
missing_warning(cx, package, "either package.license or package.license_file");
1919
}
2020

21-
if is_empty_str(&package.repository) {
21+
if is_empty_str(package.repository.as_ref()) {
2222
missing_warning(cx, package, "package.repository");
2323
}
2424

25-
if is_empty_str(&package.readme) {
25+
if is_empty_str(package.readme.as_ref()) {
2626
missing_warning(cx, package, "package.readme");
2727
}
2828

29-
if is_empty_vec(&package.keywords) {
29+
if is_empty_vec(package.keywords.as_ref()) {
3030
missing_warning(cx, package, "package.keywords");
3131
}
3232

33-
if is_empty_vec(&package.categories) {
33+
if is_empty_vec(package.categories.as_ref()) {
3434
missing_warning(cx, package, "package.categories");
3535
}
3636
}
@@ -42,8 +42,8 @@ fn missing_warning(cx: &LateContext<'_>, package: &cargo_metadata::Package, fiel
4242
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
4343
}
4444

45-
fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: &Option<T>) -> bool {
46-
value.as_ref().map_or(true, |s| s.as_ref().is_empty())
45+
fn is_empty_str<T: AsRef<std::ffi::OsStr>>(value: Option<&T>) -> bool {
46+
value.map_or(true, |s| s.as_ref().is_empty())
4747
}
4848

4949
fn is_empty_vec(value: &[String]) -> bool {

‎clippy_lints/src/functions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ declare_clippy_lint! {
443443
/// ```
444444
#[clippy::version = "1.82.0"]
445445
pub REF_OPTION,
446-
nursery,
446+
pedantic,
447447
"function signature uses `&Option<T>` instead of `Option<&T>`"
448448
}
449449

‎clippy_lints/src/unnested_or_patterns.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,15 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
275275
|k, ps1, idx| matches!(
276276
k,
277277
TupleStruct(qself2, path2, ps2)
278-
if eq_maybe_qself(qself1, qself2) && eq_path(path1, path2) && eq_pre_post(ps1, ps2, idx)
278+
if eq_maybe_qself(qself1.as_ref(), qself2.as_ref())
279+
&& eq_path(path1, path2) && eq_pre_post(ps1, ps2, idx)
279280
),
280281
|k| always_pat!(k, TupleStruct(_, _, ps) => ps),
281282
),
282283
// Transform a record pattern `S { fp_0, ..., fp_n }`.
283-
Struct(qself1, path1, fps1, rest1) => extend_with_struct_pat(qself1, path1, fps1, *rest1, start, alternatives),
284+
Struct(qself1, path1, fps1, rest1) => {
285+
extend_with_struct_pat(qself1.as_ref(), path1, fps1, *rest1, start, alternatives)
286+
},
284287
};
285288

286289
alternatives[focus_idx].kind = focus_kind;
@@ -292,7 +295,7 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
292295
/// So when we fixate on some `ident_k: pat_k`, we try to find `ident_k` in the other pattern
293296
/// and check that all `fp_i` where `i ∈ ((0...n) \ k)` between two patterns are equal.
294297
fn extend_with_struct_pat(
295-
qself1: &Option<P<ast::QSelf>>,
298+
qself1: Option<&P<ast::QSelf>>,
296299
path1: &ast::Path,
297300
fps1: &mut [ast::PatField],
298301
rest1: ast::PatFieldsRest,
@@ -307,7 +310,7 @@ fn extend_with_struct_pat(
307310
|k| {
308311
matches!(k, Struct(qself2, path2, fps2, rest2)
309312
if rest1 == *rest2 // If one struct pattern has `..` so must the other.
310-
&& eq_maybe_qself(qself1, qself2)
313+
&& eq_maybe_qself(qself1, qself2.as_ref())
311314
&& eq_path(path1, path2)
312315
&& fps1.len() == fps2.len()
313316
&& fps1.iter().enumerate().all(|(idx_1, fp1)| {

‎clippy_utils/src/ast_utils.rs

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,27 @@ pub fn eq_pat(l: &Pat, r: &Pat) -> bool {
3737
(_, Paren(r)) => eq_pat(l, r),
3838
(Wild, Wild) | (Rest, Rest) => true,
3939
(Lit(l), Lit(r)) => eq_expr(l, r),
40-
(Ident(b1, i1, s1), Ident(b2, i2, s2)) => b1 == b2 && eq_id(*i1, *i2) && both(s1, s2, |l, r| eq_pat(l, r)),
40+
(Ident(b1, i1, s1), Ident(b2, i2, s2)) => {
41+
b1 == b2 && eq_id(*i1, *i2) && both(s1.as_deref(), s2.as_deref(), eq_pat)
42+
},
4143
(Range(lf, lt, le), Range(rf, rt, re)) => {
42-
eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt) && eq_range_end(&le.node, &re.node)
44+
eq_expr_opt(lf.as_ref(), rf.as_ref())
45+
&& eq_expr_opt(lt.as_ref(), rt.as_ref())
46+
&& eq_range_end(&le.node, &re.node)
4347
},
4448
(Box(l), Box(r))
4549
| (Ref(l, Mutability::Not), Ref(r, Mutability::Not))
4650
| (Ref(l, Mutability::Mut), Ref(r, Mutability::Mut)) => eq_pat(l, r),
4751
(Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, |l, r| eq_pat(l, r)),
48-
(Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
52+
(Path(lq, lp), Path(rq, rp)) => both(lq.as_ref(), rq.as_ref(), eq_qself) && eq_path(lp, rp),
4953
(TupleStruct(lqself, lp, lfs), TupleStruct(rqself, rp, rfs)) => {
50-
eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r))
54+
eq_maybe_qself(lqself.as_ref(), rqself.as_ref()) && eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r))
5155
},
5256
(Struct(lqself, lp, lfs, lr), Struct(rqself, rp, rfs, rr)) => {
53-
lr == rr && eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && unordered_over(lfs, rfs, eq_field_pat)
57+
lr == rr
58+
&& eq_maybe_qself(lqself.as_ref(), rqself.as_ref())
59+
&& eq_path(lp, rp)
60+
&& unordered_over(lfs, rfs, eq_field_pat)
5461
},
5562
(Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
5663
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
@@ -79,7 +86,7 @@ pub fn eq_qself(l: &P<QSelf>, r: &P<QSelf>) -> bool {
7986
l.position == r.position && eq_ty(&l.ty, &r.ty)
8087
}
8188

82-
pub fn eq_maybe_qself(l: &Option<P<QSelf>>, r: &Option<P<QSelf>>) -> bool {
89+
pub fn eq_maybe_qself(l: Option<&P<QSelf>>, r: Option<&P<QSelf>>) -> bool {
8390
match (l, r) {
8491
(Some(l), Some(r)) => eq_qself(l, r),
8592
(None, None) => true,
@@ -92,7 +99,7 @@ pub fn eq_path(l: &Path, r: &Path) -> bool {
9299
}
93100

94101
pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
95-
eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r))
102+
eq_id(l.ident, r.ident) && both(l.args.as_ref(), r.args.as_ref(), |l, r| eq_generic_args(l, r))
96103
}
97104

98105
pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
@@ -122,7 +129,7 @@ pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool {
122129
}
123130
}
124131

125-
pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool {
132+
pub fn eq_expr_opt(l: Option<&P<Expr>>, r: Option<&P<Expr>>) -> bool {
126133
both(l, r, |l, r| eq_expr(l, r))
127134
}
128135

@@ -169,8 +176,12 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
169176
(Lit(l), Lit(r)) => l == r,
170177
(Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
171178
(Let(lp, le, _, _), Let(rp, re, _, _)) => eq_pat(lp, rp) && eq_expr(le, re),
172-
(If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
173-
(While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
179+
(If(lc, lt, le), If(rc, rt, re)) => {
180+
eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref())
181+
},
182+
(While(lc, lt, ll), While(rc, rt, rl)) => {
183+
eq_label(ll.as_ref(), rl.as_ref()) && eq_expr(lc, rc) && eq_block(lt, rt)
184+
},
174185
(
175186
ForLoop {
176187
pat: lp,
@@ -186,13 +197,13 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
186197
label: rl,
187198
kind: rk,
188199
},
189-
) => eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt) && lk == rk,
190-
(Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll, rl) && eq_block(lt, rt),
191-
(Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
200+
) => eq_label(ll.as_ref(), rl.as_ref()) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt) && lk == rk,
201+
(Loop(lt, ll, _), Loop(rt, rl, _)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lt, rt),
202+
(Block(lb, ll), Block(rb, rl)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_block(lb, rb),
192203
(TryBlock(l), TryBlock(r)) => eq_block(l, r),
193-
(Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r),
194-
(Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re),
195-
(Continue(ll), Continue(rl)) => eq_label(ll, rl),
204+
(Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l.as_ref(), r.as_ref()),
205+
(Break(ll, le), Break(rl, re)) => eq_label(ll.as_ref(), rl.as_ref()) && eq_expr_opt(le.as_ref(), re.as_ref()),
206+
(Continue(ll), Continue(rl)) => eq_label(ll.as_ref(), rl.as_ref()),
196207
(Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2, _), Index(r1, r2, _)) => {
197208
eq_expr(l1, r1) && eq_expr(l2, r2)
198209
},
@@ -227,12 +238,14 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
227238
&& eq_expr(le, re)
228239
},
229240
(Gen(lc, lb, lk, _), Gen(rc, rb, rk, _)) => lc == rc && eq_block(lb, rb) && lk == rk,
230-
(Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
241+
(Range(lf, lt, ll), Range(rf, rt, rl)) => {
242+
ll == rl && eq_expr_opt(lf.as_ref(), rf.as_ref()) && eq_expr_opt(lt.as_ref(), rt.as_ref())
243+
},
231244
(AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
232-
(Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
245+
(Path(lq, lp), Path(rq, rp)) => both(lq.as_ref(), rq.as_ref(), eq_qself) && eq_path(lp, rp),
233246
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
234247
(Struct(lse), Struct(rse)) => {
235-
eq_maybe_qself(&lse.qself, &rse.qself)
248+
eq_maybe_qself(lse.qself.as_ref(), rse.qself.as_ref())
236249
&& eq_path(&lse.path, &rse.path)
237250
&& eq_struct_rest(&lse.rest, &rse.rest)
238251
&& unordered_over(&lse.fields, &rse.fields, eq_field)
@@ -264,12 +277,12 @@ pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
264277
pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
265278
l.is_placeholder == r.is_placeholder
266279
&& eq_pat(&l.pat, &r.pat)
267-
&& eq_expr_opt(&l.body, &r.body)
268-
&& eq_expr_opt(&l.guard, &r.guard)
280+
&& eq_expr_opt(l.body.as_ref(), r.body.as_ref())
281+
&& eq_expr_opt(l.guard.as_ref(), r.guard.as_ref())
269282
&& over(&l.attrs, &r.attrs, eq_attr)
270283
}
271284

272-
pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool {
285+
pub fn eq_label(l: Option<&Label>, r: Option<&Label>) -> bool {
273286
both(l, r, |l, r| eq_id(l.ident, r.ident))
274287
}
275288

@@ -282,7 +295,7 @@ pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
282295
match (&l.kind, &r.kind) {
283296
(Let(l), Let(r)) => {
284297
eq_pat(&l.pat, &r.pat)
285-
&& both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
298+
&& both(l.ty.as_ref(), r.ty.as_ref(), |l, r| eq_ty(l, r))
286299
&& eq_local_kind(&l.kind, &r.kind)
287300
&& over(&l.attrs, &r.attrs, eq_attr)
288301
},
@@ -329,7 +342,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
329342
expr: re,
330343
safety: rs,
331344
}),
332-
) => lm == rm && ls == rs && eq_ty(lt, rt) && eq_expr_opt(le, re),
345+
) => lm == rm && ls == rs && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
333346
(
334347
Const(box ConstItem {
335348
defaultness: ld,
@@ -343,7 +356,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
343356
ty: rt,
344357
expr: re,
345358
}),
346-
) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le, re),
359+
) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
347360
(
348361
Fn(box ast::Fn {
349362
defaultness: ld,
@@ -358,7 +371,10 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
358371
body: rb,
359372
}),
360373
) => {
361-
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
374+
eq_defaultness(*ld, *rd)
375+
&& eq_fn_sig(lf, rf)
376+
&& eq_generics(lg, rg)
377+
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
362378
},
363379
(Mod(lu, lmk), Mod(ru, rmk)) => {
364380
lu == ru
@@ -371,7 +387,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
371387
}
372388
},
373389
(ForeignMod(l), ForeignMod(r)) => {
374-
both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
390+
both(l.abi.as_ref(), r.abi.as_ref(), eq_str_lit)
391+
&& over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
375392
},
376393
(
377394
TyAlias(box ast::TyAlias {
@@ -392,7 +409,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
392409
eq_defaultness(*ld, *rd)
393410
&& eq_generics(lg, rg)
394411
&& over(lb, rb, eq_generic_bound)
395-
&& both(lt, rt, |l, r| eq_ty(l, r))
412+
&& both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
396413
},
397414
(Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg),
398415
(Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
@@ -448,7 +465,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
448465
&& eq_defaultness(*ld, *rd)
449466
&& matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
450467
&& eq_generics(lg, rg)
451-
&& both(lot, rot, |l, r| eq_path(&l.path, &r.path))
468+
&& both(lot.as_ref(), rot.as_ref(), |l, r| eq_path(&l.path, &r.path))
452469
&& eq_ty(lst, rst)
453470
&& over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
454471
},
@@ -474,7 +491,7 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
474491
expr: re,
475492
safety: rs,
476493
}),
477-
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re) && ls == rs,
494+
) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()) && ls == rs,
478495
(
479496
Fn(box ast::Fn {
480497
defaultness: ld,
@@ -489,7 +506,10 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
489506
body: rb,
490507
}),
491508
) => {
492-
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
509+
eq_defaultness(*ld, *rd)
510+
&& eq_fn_sig(lf, rf)
511+
&& eq_generics(lg, rg)
512+
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
493513
},
494514
(
495515
TyAlias(box ast::TyAlias {
@@ -510,7 +530,7 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
510530
eq_defaultness(*ld, *rd)
511531
&& eq_generics(lg, rg)
512532
&& over(lb, rb, eq_generic_bound)
513-
&& both(lt, rt, |l, r| eq_ty(l, r))
533+
&& both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
514534
},
515535
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
516536
_ => false,
@@ -533,7 +553,7 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
533553
ty: rt,
534554
expr: re,
535555
}),
536-
) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le, re),
556+
) => eq_defaultness(*ld, *rd) && eq_generics(lg, rg) && eq_ty(lt, rt) && eq_expr_opt(le.as_ref(), re.as_ref()),
537557
(
538558
Fn(box ast::Fn {
539559
defaultness: ld,
@@ -548,7 +568,10 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
548568
body: rb,
549569
}),
550570
) => {
551-
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
571+
eq_defaultness(*ld, *rd)
572+
&& eq_fn_sig(lf, rf)
573+
&& eq_generics(lg, rg)
574+
&& both(lb.as_ref(), rb.as_ref(), |l, r| eq_block(l, r))
552575
},
553576
(
554577
Type(box TyAlias {
@@ -569,7 +592,7 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
569592
eq_defaultness(*ld, *rd)
570593
&& eq_generics(lg, rg)
571594
&& over(lb, rb, eq_generic_bound)
572-
&& both(lt, rt, |l, r| eq_ty(l, r))
595+
&& both(lt.as_ref(), rt.as_ref(), |l, r| eq_ty(l, r))
573596
},
574597
(MacCall(l), MacCall(r)) => eq_mac_call(l, r),
575598
_ => false,
@@ -582,7 +605,9 @@ pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
582605
&& eq_vis(&l.vis, &r.vis)
583606
&& eq_id(l.ident, r.ident)
584607
&& eq_variant_data(&l.data, &r.data)
585-
&& both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
608+
&& both(l.disr_expr.as_ref(), r.disr_expr.as_ref(), |l, r| {
609+
eq_expr(&l.value, &r.value)
610+
})
586611
}
587612

588613
pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
@@ -600,7 +625,7 @@ pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
600625
l.is_placeholder == r.is_placeholder
601626
&& over(&l.attrs, &r.attrs, eq_attr)
602627
&& eq_vis(&l.vis, &r.vis)
603-
&& both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
628+
&& both(l.ident.as_ref(), r.ident.as_ref(), |l, r| eq_id(*l, *r))
604629
&& eq_ty(&l.ty, &r.ty)
605630
}
606631

@@ -664,7 +689,7 @@ pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
664689
use UseTreeKind::*;
665690
match (l, r) {
666691
(Glob, Glob) => true,
667-
(Simple(l), Simple(r)) => both(l, r, |l, r| eq_id(*l, *r)),
692+
(Simple(l), Simple(r)) => both(l.as_ref(), r.as_ref(), |l, r| eq_id(*l, *r)),
668693
(Nested { items: l, .. }, Nested { items: r, .. }) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
669694
_ => false,
670695
}
@@ -726,7 +751,7 @@ pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
726751
(Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
727752
(Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
728753
(Ref(ll, l), Ref(rl, r)) => {
729-
both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
754+
both(ll.as_ref(), rl.as_ref(), |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
730755
},
731756
(BareFn(l), BareFn(r)) => {
732757
l.safety == r.safety
@@ -735,7 +760,7 @@ pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
735760
&& eq_fn_decl(&l.decl, &r.decl)
736761
},
737762
(Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
738-
(Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
763+
(Path(lq, lp), Path(rq, rp)) => both(lq.as_ref(), rq.as_ref(), eq_qself) && eq_path(lp, rp),
739764
(TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
740765
(ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
741766
(Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
@@ -771,7 +796,7 @@ pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
771796
&& over(&l.bounds, &r.bounds, eq_generic_bound)
772797
&& match (&l.kind, &r.kind) {
773798
(Lifetime, Lifetime) => true,
774-
(Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
799+
(Type { default: l }, Type { default: r }) => both(l.as_ref(), r.as_ref(), |l, r| eq_ty(l, r)),
775800
(
776801
Const {
777802
ty: lt,
@@ -783,7 +808,7 @@ pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
783808
kw_span: _,
784809
default: rd,
785810
},
786-
) => eq_ty(lt, rt) && both(ld, rd, eq_anon_const),
811+
) => eq_ty(lt, rt) && both(ld.as_ref(), rd.as_ref(), eq_anon_const),
787812
_ => false,
788813
}
789814
&& over(&l.attrs, &r.attrs, eq_attr)

‎clippy_utils/src/hir_utils.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ impl HirEqInterExpr<'_, '_, '_> {
121121

122122
// eq_pat adds the HirIds to the locals map. We therefore call it last to make sure that
123123
// these only get added if the init and type is equal.
124-
both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
125-
&& both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
126-
&& both(&l.els, &r.els, |l, r| self.eq_block(l, r))
124+
both(l.init.as_ref(), r.init.as_ref(), |l, r| self.eq_expr(l, r))
125+
&& both(l.ty.as_ref(), r.ty.as_ref(), |l, r| self.eq_ty(l, r))
126+
&& both(l.els.as_ref(), r.els.as_ref(), |l, r| self.eq_block(l, r))
127127
&& self.eq_pat(l.pat, r.pat)
128128
},
129129
(&StmtKind::Expr(l), &StmtKind::Expr(r)) | (&StmtKind::Semi(l), &StmtKind::Semi(r)) => self.eq_expr(l, r),
@@ -142,7 +142,9 @@ impl HirEqInterExpr<'_, '_, '_> {
142142
if lspan.ctxt != SyntaxContext::root() && rspan.ctxt != SyntaxContext::root() {
143143
// Don't try to check in between statements inside macros.
144144
return over(left.stmts, right.stmts, |left, right| self.eq_stmt(left, right))
145-
&& both(&left.expr, &right.expr, |left, right| self.eq_expr(left, right));
145+
&& both(left.expr.as_ref(), right.expr.as_ref(), |left, right| {
146+
self.eq_expr(left, right)
147+
});
146148
}
147149
if lspan.ctxt != rspan.ctxt {
148150
return false;
@@ -285,8 +287,8 @@ impl HirEqInterExpr<'_, '_, '_> {
285287
})
286288
},
287289
(&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
288-
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
289-
&& both(le, re, |l, r| self.eq_expr(l, r))
290+
both(li.label.as_ref(), ri.label.as_ref(), |l, r| l.ident.name == r.ident.name)
291+
&& both(le.as_ref(), re.as_ref(), |l, r| self.eq_expr(l, r))
290292
},
291293
(&ExprKind::Call(l_fun, l_args), &ExprKind::Call(r_fun, r_args)) => {
292294
self.inner.allow_side_effects && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
@@ -297,29 +299,33 @@ impl HirEqInterExpr<'_, '_, '_> {
297299
(&ExprKind::Closure(_l), &ExprKind::Closure(_r)) => false,
298300
(&ExprKind::ConstBlock(lb), &ExprKind::ConstBlock(rb)) => self.eq_body(lb.body, rb.body),
299301
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
300-
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
302+
both(li.label.as_ref(), ri.label.as_ref(), |l, r| l.ident.name == r.ident.name)
301303
},
302304
(&ExprKind::DropTemps(le), &ExprKind::DropTemps(re)) => self.eq_expr(le, re),
303305
(&ExprKind::Field(l_f_exp, ref l_f_ident), &ExprKind::Field(r_f_exp, ref r_f_ident)) => {
304306
l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
305307
},
306308
(&ExprKind::Index(la, li, _), &ExprKind::Index(ra, ri, _)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
307309
(&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => {
308-
self.eq_expr(lc, rc) && self.eq_expr(lt, rt) && both(le, re, |l, r| self.eq_expr(l, r))
310+
self.eq_expr(lc, rc) && self.eq_expr(lt, rt)
311+
&& both(le.as_ref(), re.as_ref(), |l, r| self.eq_expr(l, r))
309312
},
310313
(&ExprKind::Let(l), &ExprKind::Let(r)) => {
311-
self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init)
314+
self.eq_pat(l.pat, r.pat)
315+
&& both(l.ty.as_ref(), r.ty.as_ref(), |l, r| self.eq_ty(l, r))
316+
&& self.eq_expr(l.init, r.init)
312317
},
313318
(ExprKind::Lit(l), ExprKind::Lit(r)) => l.node == r.node,
314319
(&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => {
315-
lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
320+
lls == rls && self.eq_block(lb, rb)
321+
&& both(ll.as_ref(), rl.as_ref(), |l, r| l.ident.name == r.ident.name)
316322
},
317323
(&ExprKind::Match(le, la, ref ls), &ExprKind::Match(re, ra, ref rs)) => {
318324
(ls == rs || (matches!((ls, rs), (TryDesugar(_), TryDesugar(_)))))
319325
&& self.eq_expr(le, re)
320326
&& over(la, ra, |l, r| {
321327
self.eq_pat(l.pat, r.pat)
322-
&& both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r))
328+
&& both(l.guard.as_ref(), r.guard.as_ref(), |l, r| self.eq_expr(l, r))
323329
&& self.eq_expr(l.body, r.body)
324330
})
325331
},
@@ -339,10 +345,10 @@ impl HirEqInterExpr<'_, '_, '_> {
339345
(&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => {
340346
self.eq_expr(le, re) && self.eq_array_length(ll, rl)
341347
},
342-
(ExprKind::Ret(l), ExprKind::Ret(r)) => both(l, r, |l, r| self.eq_expr(l, r)),
348+
(ExprKind::Ret(l), ExprKind::Ret(r)) => both(l.as_ref(), r.as_ref(), |l, r| self.eq_expr(l, r)),
343349
(&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => {
344350
self.eq_qpath(l_path, r_path)
345-
&& both(lo, ro, |l, r| self.eq_expr(l, r))
351+
&& both(lo.as_ref(), ro.as_ref(), |l, r| self.eq_expr(l, r))
346352
&& over(lf, rf, |l, r| self.eq_expr_field(l, r))
347353
},
348354
(&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
@@ -450,7 +456,7 @@ impl HirEqInterExpr<'_, '_, '_> {
450456
self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
451457
},
452458
(&PatKind::Binding(lb, li, _, ref lp), &PatKind::Binding(rb, ri, _, ref rp)) => {
453-
let eq = lb == rb && both(lp, rp, |l, r| self.eq_pat(l, r));
459+
let eq = lb == rb && both(lp.as_ref(), rp.as_ref(), |l, r| self.eq_pat(l, r));
454460
if eq {
455461
self.locals.insert(li, ri);
456462
}
@@ -460,13 +466,15 @@ impl HirEqInterExpr<'_, '_, '_> {
460466
(&PatKind::Lit(l), &PatKind::Lit(r)) => self.eq_expr(l, r),
461467
(&PatKind::Tuple(l, ls), &PatKind::Tuple(r, rs)) => ls == rs && over(l, r, |l, r| self.eq_pat(l, r)),
462468
(&PatKind::Range(ref ls, ref le, li), &PatKind::Range(ref rs, ref re, ri)) => {
463-
both(ls, rs, |a, b| self.eq_expr(a, b)) && both(le, re, |a, b| self.eq_expr(a, b)) && (li == ri)
469+
both(ls.as_ref(), rs.as_ref(), |a, b| self.eq_expr(a, b))
470+
&& both(le.as_ref(), re.as_ref(), |a, b| self.eq_expr(a, b))
471+
&& (li == ri)
464472
},
465473
(&PatKind::Ref(le, ref lm), &PatKind::Ref(re, ref rm)) => lm == rm && self.eq_pat(le, re),
466474
(&PatKind::Slice(ls, ref li, le), &PatKind::Slice(rs, ref ri, re)) => {
467475
over(ls, rs, |l, r| self.eq_pat(l, r))
468476
&& over(le, re, |l, r| self.eq_pat(l, r))
469-
&& both(li, ri, |l, r| self.eq_pat(l, r))
477+
&& both(li.as_ref(), ri.as_ref(), |l, r| self.eq_pat(l, r))
470478
},
471479
(&PatKind::Wild, &PatKind::Wild) => true,
472480
_ => false,
@@ -476,7 +484,7 @@ impl HirEqInterExpr<'_, '_, '_> {
476484
fn eq_qpath(&mut self, left: &QPath<'_>, right: &QPath<'_>) -> bool {
477485
match (left, right) {
478486
(&QPath::Resolved(ref lty, lpath), &QPath::Resolved(ref rty, rpath)) => {
479-
both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
487+
both(lty.as_ref(), rty.as_ref(), |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
480488
},
481489
(&QPath::TypeRelative(lty, lseg), &QPath::TypeRelative(rty, rseg)) => {
482490
self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
@@ -510,7 +518,10 @@ impl HirEqInterExpr<'_, '_, '_> {
510518
pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
511519
// The == of idents doesn't work with different contexts,
512520
// we have to be explicit about hygiene
513-
left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
521+
left.ident.name == right.ident.name
522+
&& both(left.args.as_ref(), right.args.as_ref(), |l, r| {
523+
self.eq_path_parameters(l, r)
524+
})
514525
}
515526

516527
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
@@ -649,7 +660,7 @@ fn swap_binop<'a>(
649660

650661
/// Checks if the two `Option`s are both `None` or some equal values as per
651662
/// `eq_fn`.
652-
pub fn both<X>(l: &Option<X>, r: &Option<X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
663+
pub fn both<X>(l: Option<&X>, r: Option<&X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
653664
l.as_ref()
654665
.map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
655666
}

‎lintcheck/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Crate {
6868
total_crates_to_lint: usize,
6969
config: &LintcheckConfig,
7070
lint_levels_args: &[String],
71-
server: &Option<LintcheckServer>,
71+
server: Option<&LintcheckServer>,
7272
) -> Vec<ClippyCheckOutput> {
7373
// advance the atomic index by one
7474
let index = target_dir_index.fetch_add(1, Ordering::SeqCst);
@@ -359,7 +359,7 @@ fn lintcheck(config: LintcheckConfig) {
359359
crates.len(),
360360
&config,
361361
&lint_level_args,
362-
&server,
362+
server.as_ref(),
363363
)
364364
})
365365
.collect();

‎src/driver.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ fn test_has_arg() {
7474
assert!(!has_arg(args, "--bar"));
7575
}
7676

77-
fn track_clippy_args(psess: &mut ParseSess, args_env_var: &Option<String>) {
78-
psess.env_depinfo.get_mut().insert((
79-
Symbol::intern("CLIPPY_ARGS"),
80-
args_env_var.as_deref().map(Symbol::intern),
81-
));
77+
fn track_clippy_args(psess: &mut ParseSess, args_env_var: Option<&str>) {
78+
psess
79+
.env_depinfo
80+
.get_mut()
81+
.insert((Symbol::intern("CLIPPY_ARGS"), args_env_var.map(Symbol::intern)));
8282
}
8383

8484
/// Track files that may be accessed at runtime in `file_depinfo` so that cargo will re-run clippy
@@ -122,7 +122,7 @@ impl rustc_driver::Callbacks for RustcCallbacks {
122122
fn config(&mut self, config: &mut interface::Config) {
123123
let clippy_args_var = self.clippy_args_var.take();
124124
config.psess_created = Some(Box::new(move |psess| {
125-
track_clippy_args(psess, &clippy_args_var);
125+
track_clippy_args(psess, clippy_args_var.as_deref());
126126
}));
127127
}
128128
}
@@ -139,7 +139,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
139139
let previous = config.register_lints.take();
140140
let clippy_args_var = self.clippy_args_var.take();
141141
config.psess_created = Some(Box::new(move |psess| {
142-
track_clippy_args(psess, &clippy_args_var);
142+
track_clippy_args(psess, clippy_args_var.as_deref());
143143
track_files(psess);
144144

145145
// Trigger a rebuild if CLIPPY_CONF_DIR changes. The value must be a valid string so

0 commit comments

Comments
 (0)
Please sign in to comment.