Also handle Result type for map_clone lint

This commit is contained in:
Guillaume Gomez 2024-01-09 16:35:10 +01:00
parent cdd96bc662
commit 8791a28c4a
4 changed files with 20 additions and 1 deletions

View File

@ -18,6 +18,7 @@ pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
&& (cx.tcx.impl_of_method(method_id).map_or(false, |id| {
is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Option)
|| is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Result)
}) || is_diag_trait_item(cx, method_id, sym::Iterator))
{
match arg.kind {

View File

@ -70,4 +70,10 @@ fn main() {
//~^ ERROR: you are explicitly cloning with `.map()`
let y = x.cloned();
//~^ ERROR: you are explicitly cloning with `.map()`
// Testing with `Result` now.
let x: Result<String, ()> = Ok(String::new());
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
let y = x.cloned();
//~^ ERROR: you are explicitly cloning with `.map()`
}

View File

@ -70,4 +70,10 @@ fn main() {
//~^ ERROR: you are explicitly cloning with `.map()`
let y = x.map(String::clone);
//~^ ERROR: you are explicitly cloning with `.map()`
// Testing with `Result` now.
let x: Result<String, ()> = Ok(String::new());
let x = x.as_ref(); // We do this to prevent triggering the `useless_asref` lint.
let y = x.map(|x| String::clone(x));
//~^ ERROR: you are explicitly cloning with `.map()`
}

View File

@ -55,5 +55,11 @@ error: you are explicitly cloning with `.map()`
LL | let y = x.map(String::clone);
| ^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
error: aborting due to 9 previous errors
error: you are explicitly cloning with `.map()`
--> $DIR/map_clone.rs:77:13
|
LL | let y = x.map(|x| String::clone(x));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling the dedicated `cloned` method: `x.cloned()`
error: aborting due to 10 previous errors