Handle false positive with map_clone

This commit is contained in:
maekawatoshiki 2024-02-13 03:01:56 +09:00
parent 660b058ba2
commit 20e4c74521
3 changed files with 38 additions and 4 deletions

View File

@ -86,8 +86,11 @@ pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_
}
}
},
hir::ExprKind::Call(call, [_]) => {
if let hir::ExprKind::Path(qpath) = call.kind {
hir::ExprKind::Call(call, args) => {
if let hir::ExprKind::Path(qpath) = call.kind
&& let [arg] = args
&& ident_eq(name, arg)
{
handle_path(cx, call, &qpath, e, recv);
}
},
@ -119,6 +122,9 @@ fn handle_path(
&& let args = args.as_slice()
&& let Some(ty) = args.iter().find_map(|generic_arg| generic_arg.as_type())
&& ty.is_ref()
&& let ty::Ref(_, ty, Mutability::Not) = ty.kind()
&& let ty::FnDef(_, lst) = cx.typeck_results().expr_ty(arg).kind()
&& lst.iter().all(|l| l.as_type() == Some(*ty))
{
lint_path(cx, e.span, recv.span, is_copy(cx, ty.peel_refs()));
}

View File

@ -6,7 +6,8 @@
clippy::redundant_clone,
clippy::redundant_closure,
clippy::useless_asref,
clippy::useless_vec
clippy::useless_vec,
clippy::empty_loop
)]
fn main() {
@ -117,4 +118,17 @@ fn main() {
let y = x.as_ref().map(|x| String::clone(x));
let x: Result<String, ()> = Ok(String::new());
let y = x.as_ref().map(|x| String::clone(x));
// Issue #12271
{
// Don't lint these
let x: Option<&u8> = None;
let y = x.map(|x| String::clone(loop {}));
let x: Option<&u8> = None;
let y = x.map(|x| u8::clone(loop {}));
let x: Vec<&u8> = vec![];
let y = x.into_iter().map(|x| String::clone(loop {}));
let x: Vec<&u8> = vec![];
let y = x.into_iter().map(|x| u8::clone(loop {}));
}
}

View File

@ -6,7 +6,8 @@
clippy::redundant_clone,
clippy::redundant_closure,
clippy::useless_asref,
clippy::useless_vec
clippy::useless_vec,
clippy::empty_loop
)]
fn main() {
@ -117,4 +118,17 @@ fn main() {
let y = x.as_ref().map(|x| String::clone(x));
let x: Result<String, ()> = Ok(String::new());
let y = x.as_ref().map(|x| String::clone(x));
// Issue #12271
{
// Don't lint these
let x: Option<&u8> = None;
let y = x.map(|x| String::clone(loop {}));
let x: Option<&u8> = None;
let y = x.map(|x| u8::clone(loop {}));
let x: Vec<&u8> = vec![];
let y = x.into_iter().map(|x| String::clone(loop {}));
let x: Vec<&u8> = vec![];
let y = x.into_iter().map(|x| u8::clone(loop {}));
}
}