Skip to content

Commit

Permalink
Ignore closures for some type lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexendoo committed Sep 14, 2023
1 parent b27fc10 commit a2a31a0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
11 changes: 6 additions & 5 deletions clippy_lints/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
fn check_fn(
&mut self,
cx: &LateContext<'_>,
_: FnKind<'_>,
fn_kind: FnKind<'_>,
decl: &FnDecl<'_>,
_: &Body<'_>,
_: Span,
Expand All @@ -340,6 +340,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
CheckTyContext {
is_in_trait_impl,
is_exported,
in_body: matches!(fn_kind, FnKind::Closure),
..CheckTyContext::default()
},
);
Expand Down Expand Up @@ -427,7 +428,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
cx,
ty,
CheckTyContext {
is_local: true,
in_body: true,
..CheckTyContext::default()
},
);
Expand Down Expand Up @@ -481,7 +482,7 @@ impl Types {
}

match hir_ty.kind {
TyKind::Path(ref qpath) if !context.is_local => {
TyKind::Path(ref qpath) if !context.in_body => {
let hir_id = hir_ty.hir_id;
let res = cx.qpath_res(qpath, hir_id);
if let Some(def_id) = res.opt_def_id() {
Expand Down Expand Up @@ -581,8 +582,8 @@ impl Types {
#[derive(Clone, Copy, Default)]
struct CheckTyContext {
is_in_trait_impl: bool,
/// `true` for types on local variables.
is_local: bool,
/// `true` for types on local variables and in closure signatures.
in_body: bool,
/// `true` for types that are part of the public API.
is_exported: bool,
is_nested_call: bool,
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/redundant_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,9 @@ mod box_fat_ptr {
//~| NOTE: `Box<Box<DynSized>>` is already on the heap, `Rc<Box<Box<DynSized>>>` makes
}

// https://github.com/rust-lang/rust-clippy/issues/11417
fn type_in_closure() {
let _ = |_: &mut Box<Box<dyn ToString>>| {};
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/vec_box_sized.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ mod inner_mod {
}
}

// https://github.com/rust-lang/rust-clippy/issues/11417
fn in_closure() {
let _ = |_: Vec<Box<dyn ToString>>| {};
}

fn main() {}
5 changes: 5 additions & 0 deletions tests/ui/vec_box_sized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ mod inner_mod {
}
}

// https://github.com/rust-lang/rust-clippy/issues/11417
fn in_closure() {
let _ = |_: Vec<Box<dyn ToString>>| {};
}

fn main() {}

0 comments on commit a2a31a0

Please sign in to comment.