Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5f7b3c5

Browse files
committed
Only enforce MSRV check on .clone_into() suggestions
1 parent 7c0b2dd commit 5f7b3c5

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

clippy_lints/src/assigning_clones.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ impl_lint_pass!(AssigningClones => [ASSIGNING_CLONES]);
6666

6767
impl<'tcx> LateLintPass<'tcx> for AssigningClones {
6868
fn check_expr(&mut self, cx: &LateContext<'tcx>, assign_expr: &'tcx hir::Expr<'_>) {
69-
if !self.msrv.meets(msrvs::ASSIGNING_CLONES) {
70-
return;
71-
}
72-
7369
// Do not fire the lint in macros
7470
let expn_data = assign_expr.span().ctxt().outer_expn_data();
7571
match expn_data.kind {
@@ -85,7 +81,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
8581
return;
8682
};
8783

88-
if is_ok_to_suggest(cx, lhs, &call) {
84+
if is_ok_to_suggest(cx, lhs, &call, &self.msrv) {
8985
suggest(cx, assign_expr, lhs, &call);
9086
}
9187
}
@@ -154,7 +150,13 @@ fn extract_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<
154150

155151
// Return true if we find that the called method has a custom implementation and isn't derived or
156152
// provided by default by the corresponding trait.
157-
fn is_ok_to_suggest<'tcx>(cx: &LateContext<'tcx>, lhs: &Expr<'tcx>, call: &CallCandidate<'tcx>) -> bool {
153+
fn is_ok_to_suggest<'tcx>(cx: &LateContext<'tcx>, lhs: &Expr<'tcx>, call: &CallCandidate<'tcx>, msrv: &Msrv) -> bool {
154+
// For calls to .to_owned we suggest using .clone_into(), which was only stablilized in 1.63.
155+
// If the current MSRV is below that, don't suggest the lint.
156+
if !msrv.meets(msrvs::ASSIGNING_CLONES) && matches!(call.target, TargetTrait::ToOwned) {
157+
return false;
158+
}
159+
158160
// If the left-hand side is a local variable, it might be uninitialized at this point.
159161
// In that case we do not want to suggest the lint.
160162
if let Some(local) = path_to_local(lhs) {

0 commit comments

Comments
 (0)