Fix dogfood and add code comments

This commit is contained in:
Guillaume Gomez 2024-01-09 17:58:09 +01:00
parent 103e8881c6
commit f7ef9a6f1f
2 changed files with 12 additions and 7 deletions

View File

@ -15,14 +15,20 @@ use rustc_span::{sym, Span};
use super::MAP_CLONE;
// If this `map` is called on an `Option` or a `Result` and the previous call is `as_ref`, we don't
// run this lint because it would overlap with `useless_asref` which provides a better suggestion
// in this case.
fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_id: DefId) -> bool {
if is_diag_trait_item(cx, method_id, sym::Iterator) {
return true;
}
if !cx.tcx.impl_of_method(method_id).map_or(false, |id| {
// We check if it's an `Option` or a `Result`.
if let Some(id) = cx.tcx.impl_of_method(method_id) {
let identity = cx.tcx.type_of(id).instantiate_identity();
is_type_diagnostic_item(cx, identity, sym::Option) || is_type_diagnostic_item(cx, identity, sym::Result)
}) {
if !is_type_diagnostic_item(cx, identity, sym::Option) && !is_type_diagnostic_item(cx, identity, sym::Result) {
return false;
}
} else {
return false;
}
// 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) ->
return path2.ident.name != sym::as_ref || path1.ident.name != sym::map;
}
return true;
true
}
pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, msrv: &Msrv) {

View File

@ -90,10 +90,9 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str,
&& segment.ident.name == sym::map
// And that it only has one argument.
&& let [arg] = args
&& is_calling_clone(cx, arg)
{
if is_calling_clone(cx, arg) {
lint_as_ref_clone(cx, expr.span.with_hi(parent.span.hi()), recvr, call_name);
}
lint_as_ref_clone(cx, expr.span.with_hi(parent.span.hi()), recvr, call_name);
}
}
}