Skip to content

Commit f7ef9a6

Browse files
Fix dogfood and add code comments
1 parent 103e888 commit f7ef9a6

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

clippy_lints/src/methods/map_clone.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ use rustc_span::{sym, Span};
1515

1616
use super::MAP_CLONE;
1717

18+
// If this `map` is called on an `Option` or a `Result` and the previous call is `as_ref`, we don't
19+
// run this lint because it would overlap with `useless_asref` which provides a better suggestion
20+
// in this case.
1821
fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_id: DefId) -> bool {
1922
if is_diag_trait_item(cx, method_id, sym::Iterator) {
2023
return true;
2124
}
22-
if !cx.tcx.impl_of_method(method_id).map_or(false, |id| {
25+
// We check if it's an `Option` or a `Result`.
26+
if let Some(id) = cx.tcx.impl_of_method(method_id) {
2327
let identity = cx.tcx.type_of(id).instantiate_identity();
24-
is_type_diagnostic_item(cx, identity, sym::Option) || is_type_diagnostic_item(cx, identity, sym::Result)
25-
}) {
28+
if !is_type_diagnostic_item(cx, identity, sym::Option) && !is_type_diagnostic_item(cx, identity, sym::Result) {
29+
return false;
30+
}
31+
} else {
2632
return false;
2733
}
2834
// We check if the previous method call is `as_ref`.
@@ -32,7 +38,7 @@ fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_id: DefId) ->
3238
return path2.ident.name != sym::as_ref || path1.ident.name != sym::map;
3339
}
3440

35-
return true;
41+
true
3642
}
3743

3844
pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, msrv: &Msrv) {

clippy_lints/src/methods/useless_asref.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str,
9090
&& segment.ident.name == sym::map
9191
// And that it only has one argument.
9292
&& let [arg] = args
93+
&& is_calling_clone(cx, arg)
9394
{
94-
if is_calling_clone(cx, arg) {
95-
lint_as_ref_clone(cx, expr.span.with_hi(parent.span.hi()), recvr, call_name);
96-
}
95+
lint_as_ref_clone(cx, expr.span.with_hi(parent.span.hi()), recvr, call_name);
9796
}
9897
}
9998
}

0 commit comments

Comments
 (0)