Skip to content

Commit 2c629cc

Browse files
committed
Auto merge of #11504 - Alexendoo:type-lints-closures, r=xFrednet
Ignore closures for some type lints Fixes #11417 `hir_ty_to_ty` is used in a couple of the `!is_local` lints, which doesn't play nicely inside bodies changelog: none
2 parents 6734e96 + a2a31a0 commit 2c629cc

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
315315
fn check_fn(
316316
&mut self,
317317
cx: &LateContext<'_>,
318-
_: FnKind<'_>,
318+
fn_kind: FnKind<'_>,
319319
decl: &FnDecl<'_>,
320320
_: &Body<'_>,
321321
_: Span,
@@ -340,6 +340,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
340340
CheckTyContext {
341341
is_in_trait_impl,
342342
is_exported,
343+
in_body: matches!(fn_kind, FnKind::Closure),
343344
..CheckTyContext::default()
344345
},
345346
);
@@ -427,7 +428,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
427428
cx,
428429
ty,
429430
CheckTyContext {
430-
is_local: true,
431+
in_body: true,
431432
..CheckTyContext::default()
432433
},
433434
);
@@ -481,7 +482,7 @@ impl Types {
481482
}
482483

483484
match hir_ty.kind {
484-
TyKind::Path(ref qpath) if !context.is_local => {
485+
TyKind::Path(ref qpath) if !context.in_body => {
485486
let hir_id = hir_ty.hir_id;
486487
let res = cx.qpath_res(qpath, hir_id);
487488
if let Some(def_id) = res.opt_def_id() {
@@ -581,8 +582,8 @@ impl Types {
581582
#[derive(Clone, Copy, Default)]
582583
struct CheckTyContext {
583584
is_in_trait_impl: bool,
584-
/// `true` for types on local variables.
585-
is_local: bool,
585+
/// `true` for types on local variables and in closure signatures.
586+
in_body: bool,
586587
/// `true` for types that are part of the public API.
587588
is_exported: bool,
588589
is_nested_call: bool,

tests/ui/redundant_allocation.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,9 @@ mod box_fat_ptr {
159159
//~| NOTE: `Box<Box<DynSized>>` is already on the heap, `Rc<Box<Box<DynSized>>>` makes
160160
}
161161

162+
// https://github.com/rust-lang/rust-clippy/issues/11417
163+
fn type_in_closure() {
164+
let _ = |_: &mut Box<Box<dyn ToString>>| {};
165+
}
166+
162167
fn main() {}

tests/ui/vec_box_sized.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ mod inner_mod {
4949
}
5050
}
5151

52+
// https://github.com/rust-lang/rust-clippy/issues/11417
53+
fn in_closure() {
54+
let _ = |_: Vec<Box<dyn ToString>>| {};
55+
}
56+
5257
fn main() {}

tests/ui/vec_box_sized.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ mod inner_mod {
4949
}
5050
}
5151

52+
// https://github.com/rust-lang/rust-clippy/issues/11417
53+
fn in_closure() {
54+
let _ = |_: Vec<Box<dyn ToString>>| {};
55+
}
56+
5257
fn main() {}

0 commit comments

Comments
 (0)