Skip to content

Commit ba8da7a

Browse files
useless_conversion: move all the impl to the same lint pass (#15274)
Needed to split the lints crate. changelog: none
2 parents fdf37fc + 7796773 commit ba8da7a

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5286,10 +5286,6 @@ impl Methods {
52865286
}
52875287
map_identity::check(cx, expr, recv, m_arg, name, span);
52885288
manual_inspect::check(cx, expr, m_arg, name, span, self.msrv);
5289-
crate::useless_conversion::check_function_application(cx, expr, recv, m_arg);
5290-
},
5291-
(sym::map_break | sym::map_continue, [m_arg]) => {
5292-
crate::useless_conversion::check_function_application(cx, expr, recv, m_arg);
52935289
},
52945290
(sym::map_or, [def, map]) => {
52955291
option_map_or_none::check(cx, expr, recv, def, map);

clippy_lints/src/useless_conversion.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,33 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
176176
}
177177
},
178178

179+
ExprKind::MethodCall(path, recv, [arg], _) => {
180+
if matches!(
181+
path.ident.name,
182+
sym::map | sym::map_err | sym::map_break | sym::map_continue
183+
) && has_eligible_receiver(cx, recv, e)
184+
&& (is_trait_item(cx, arg, sym::Into) || is_trait_item(cx, arg, sym::From))
185+
&& let ty::FnDef(_, args) = cx.typeck_results().expr_ty(arg).kind()
186+
&& let &[from_ty, to_ty] = args.into_type_list(cx.tcx).as_slice()
187+
&& same_type_and_consts(from_ty, to_ty)
188+
{
189+
span_lint_and_then(
190+
cx,
191+
USELESS_CONVERSION,
192+
e.span.with_lo(recv.span.hi()),
193+
format!("useless conversion to the same type: `{from_ty}`"),
194+
|diag| {
195+
diag.suggest_remove_item(
196+
cx,
197+
e.span.with_lo(recv.span.hi()),
198+
"consider removing",
199+
Applicability::MachineApplicable,
200+
);
201+
},
202+
);
203+
}
204+
},
205+
179206
ExprKind::MethodCall(name, recv, [], _) => {
180207
if is_trait_method(cx, e, sym::Into) && name.ident.name == sym::into {
181208
let a = cx.typeck_results().expr_ty(e);
@@ -412,32 +439,6 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
412439
}
413440
}
414441

415-
/// Check if `arg` is a `Into::into` or `From::from` applied to `receiver` to give `expr`, through a
416-
/// higher-order mapping function.
417-
pub fn check_function_application(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<'_>) {
418-
if has_eligible_receiver(cx, recv, expr)
419-
&& (is_trait_item(cx, arg, sym::Into) || is_trait_item(cx, arg, sym::From))
420-
&& let ty::FnDef(_, args) = cx.typeck_results().expr_ty(arg).kind()
421-
&& let &[from_ty, to_ty] = args.into_type_list(cx.tcx).as_slice()
422-
&& same_type_and_consts(from_ty, to_ty)
423-
{
424-
span_lint_and_then(
425-
cx,
426-
USELESS_CONVERSION,
427-
expr.span.with_lo(recv.span.hi()),
428-
format!("useless conversion to the same type: `{from_ty}`"),
429-
|diag| {
430-
diag.suggest_remove_item(
431-
cx,
432-
expr.span.with_lo(recv.span.hi()),
433-
"consider removing",
434-
Applicability::MachineApplicable,
435-
);
436-
},
437-
);
438-
}
439-
}
440-
441442
fn has_eligible_receiver(cx: &LateContext<'_>, recv: &Expr<'_>, expr: &Expr<'_>) -> bool {
442443
if is_inherent_method_call(cx, expr) {
443444
matches!(

0 commit comments

Comments
 (0)