Skip to content

Commit 89aba8d

Browse files
committed
Auto merge of #12511 - humannum14916:assigning_clones_msrv, r=Alexendoo
`assigning_clones` should respect MSRV Fixes: #12502 This PR fixes the `assigning_clones` lint suggesting to use `clone_from` or `clone_into` on incompatible MSRVs. `assigning_clones` will suggest using either `clone_from` or `clone_into`, both of which were stabilized in 1.63. If the current MSRV is below 1.63, the lint should not trigger. changelog: [`assigning_clones`]: don't lint when the MSRV is below 1.63.
2 parents 3f338b3 + 7c0b2dd commit 89aba8d

File tree

8 files changed

+60
-11
lines changed

8 files changed

+60
-11
lines changed

book/src/lint_configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
602602
**Affected lints:**
603603
* [`almost_complete_range`](https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_range)
604604
* [`approx_constant`](https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant)
605+
* [`assigning_clones`](https://rust-lang.github.io/rust-clippy/master/index.html#assigning_clones)
605606
* [`borrow_as_ptr`](https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr)
606607
* [`cast_abs_to_unsigned`](https://rust-lang.github.io/rust-clippy/master/index.html#cast_abs_to_unsigned)
607608
* [`checked_conversions`](https://rust-lang.github.io/rust-clippy/master/index.html#checked_conversions)

clippy_config/src/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ define_Conf! {
262262
///
263263
/// Suppress lints whenever the suggested change would cause breakage for other crates.
264264
(avoid_breaking_exported_api: bool = true),
265-
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, OPTION_MAP_UNWRAP_OR, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR, INDEX_REFUTABLE_SLICE, MAP_CLONE, BORROW_AS_PTR, MANUAL_BITS, ERR_EXPECT, CAST_ABS_TO_UNSIGNED, UNINLINED_FORMAT_ARGS, MANUAL_CLAMP, MANUAL_LET_ELSE, UNCHECKED_DURATION_SUBTRACTION, COLLAPSIBLE_STR_REPLACE, SEEK_FROM_CURRENT, SEEK_REWIND, UNNECESSARY_LAZY_EVALUATIONS, TRANSMUTE_PTR_TO_REF, ALMOST_COMPLETE_RANGE, NEEDLESS_BORROW, DERIVABLE_IMPLS, MANUAL_IS_ASCII_CHECK, MANUAL_REM_EUCLID, MANUAL_RETAIN, TYPE_REPETITION_IN_BOUNDS, TUPLE_ARRAY_CONVERSIONS, MANUAL_TRY_FOLD, MANUAL_HASH_ONE, ITER_KV_MAP, MANUAL_C_STR_LITERALS.
265+
/// Lint: MANUAL_SPLIT_ONCE, MANUAL_STR_REPEAT, CLONED_INSTEAD_OF_COPIED, REDUNDANT_FIELD_NAMES, OPTION_MAP_UNWRAP_OR, REDUNDANT_STATIC_LIFETIMES, FILTER_MAP_NEXT, CHECKED_CONVERSIONS, MANUAL_RANGE_CONTAINS, USE_SELF, MEM_REPLACE_WITH_DEFAULT, MANUAL_NON_EXHAUSTIVE, OPTION_AS_REF_DEREF, MAP_UNWRAP_OR, MATCH_LIKE_MATCHES_MACRO, MANUAL_STRIP, MISSING_CONST_FOR_FN, UNNESTED_OR_PATTERNS, FROM_OVER_INTO, PTR_AS_PTR, IF_THEN_SOME_ELSE_NONE, APPROX_CONSTANT, DEPRECATED_CFG_ATTR, INDEX_REFUTABLE_SLICE, MAP_CLONE, BORROW_AS_PTR, MANUAL_BITS, ERR_EXPECT, CAST_ABS_TO_UNSIGNED, UNINLINED_FORMAT_ARGS, MANUAL_CLAMP, MANUAL_LET_ELSE, UNCHECKED_DURATION_SUBTRACTION, COLLAPSIBLE_STR_REPLACE, SEEK_FROM_CURRENT, SEEK_REWIND, UNNECESSARY_LAZY_EVALUATIONS, TRANSMUTE_PTR_TO_REF, ALMOST_COMPLETE_RANGE, NEEDLESS_BORROW, DERIVABLE_IMPLS, MANUAL_IS_ASCII_CHECK, MANUAL_REM_EUCLID, MANUAL_RETAIN, TYPE_REPETITION_IN_BOUNDS, TUPLE_ARRAY_CONVERSIONS, MANUAL_TRY_FOLD, MANUAL_HASH_ONE, ITER_KV_MAP, MANUAL_C_STR_LITERALS, ASSIGNING_CLONES.
266266
///
267267
/// The minimum rust version that the project supports. Defaults to the `rust-version` field in `Cargo.toml`
268268
#[default_text = ""]

clippy_config/src/msrvs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ msrv_aliases! {
2323
1,70,0 { OPTION_RESULT_IS_VARIANT_AND, BINARY_HEAP_RETAIN }
2424
1,68,0 { PATH_MAIN_SEPARATOR_STR }
2525
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
26+
1,63,0 { ASSIGNING_CLONES }
2627
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE }
2728
1,59,0 { THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST }
2829
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY }

clippy_lints/src/assigning_clones.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use clippy_config::msrvs::{self, Msrv};
12
use clippy_utils::diagnostics::span_lint_and_then;
23
use clippy_utils::macros::HirNode;
34
use clippy_utils::sugg::Sugg;
@@ -6,7 +7,7 @@ use rustc_errors::Applicability;
67
use rustc_hir::{self as hir, Expr, ExprKind, Node};
78
use rustc_lint::{LateContext, LateLintPass};
89
use rustc_middle::ty::{self, Instance, Mutability};
9-
use rustc_session::declare_lint_pass;
10+
use rustc_session::impl_lint_pass;
1011
use rustc_span::def_id::DefId;
1112
use rustc_span::symbol::sym;
1213
use rustc_span::ExpnKind;
@@ -49,10 +50,26 @@ declare_clippy_lint! {
4950
perf,
5051
"assigning the result of cloning may be inefficient"
5152
}
52-
declare_lint_pass!(AssigningClones => [ASSIGNING_CLONES]);
53+
54+
pub struct AssigningClones {
55+
msrv: Msrv,
56+
}
57+
58+
impl AssigningClones {
59+
#[must_use]
60+
pub fn new(msrv: Msrv) -> Self {
61+
Self { msrv }
62+
}
63+
}
64+
65+
impl_lint_pass!(AssigningClones => [ASSIGNING_CLONES]);
5366

5467
impl<'tcx> LateLintPass<'tcx> for AssigningClones {
5568
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+
5673
// Do not fire the lint in macros
5774
let expn_data = assign_expr.span().ctxt().outer_expn_data();
5875
match expn_data.kind {
@@ -72,6 +89,8 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
7289
suggest(cx, assign_expr, lhs, &call);
7390
}
7491
}
92+
93+
extract_msrv_attr!(LateContext);
7594
}
7695

7796
// Try to resolve the call to `Clone::clone` or `ToOwned::to_owned`.

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
11221122
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(msrv())));
11231123
store.register_late_pass(|_| Box::new(to_string_trait_impl::ToStringTraitImpl));
11241124
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
1125-
store.register_late_pass(|_| Box::new(assigning_clones::AssigningClones));
1125+
store.register_late_pass(move |_| Box::new(assigning_clones::AssigningClones::new(msrv())));
11261126
store.register_late_pass(|_| Box::new(zero_repeat_side_effects::ZeroRepeatSideEffects));
11271127
store.register_late_pass(|_| Box::new(manual_unwrap_or_default::ManualUnwrapOrDefault));
11281128
store.register_late_pass(|_| Box::new(integer_division_remainder_used::IntegerDivisionRemainderUsed));

tests/ui/assigning_clones.fixed

+11
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
128128
*a = b.clone();
129129
}
130130

131+
#[clippy::msrv = "1.62"]
132+
fn msrv_1_62(mut a: String, b: &str) {
133+
// Should not be linted, as clone_into wasn't stabilized until 1.63
134+
a = b.to_owned();
135+
}
136+
137+
#[clippy::msrv = "1.63"]
138+
fn msrv_1_63(mut a: String, b: &str) {
139+
b.clone_into(&mut a);
140+
}
141+
131142
macro_rules! clone_inside {
132143
($a:expr, $b: expr) => {
133144
$a = $b.clone();

tests/ui/assigning_clones.rs

+11
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ fn ignore_generic_clone<T: Clone>(a: &mut T, b: &T) {
128128
*a = b.clone();
129129
}
130130

131+
#[clippy::msrv = "1.62"]
132+
fn msrv_1_62(mut a: String, b: &str) {
133+
// Should not be linted, as clone_into wasn't stabilized until 1.63
134+
a = b.to_owned();
135+
}
136+
137+
#[clippy::msrv = "1.63"]
138+
fn msrv_1_63(mut a: String, b: &str) {
139+
a = b.to_owned();
140+
}
141+
131142
macro_rules! clone_inside {
132143
($a:expr, $b: expr) => {
133144
$a = $b.clone();

tests/ui/assigning_clones.stderr

+13-7
Original file line numberDiff line numberDiff line change
@@ -68,40 +68,46 @@ LL | a = b.clone();
6868
| ^^^^^^^^^^^^^ help: use `clone_from()`: `a.clone_from(&b)`
6969

7070
error: assigning the result of `ToOwned::to_owned()` may be inefficient
71-
--> tests/ui/assigning_clones.rs:145:5
71+
--> tests/ui/assigning_clones.rs:139:5
72+
|
73+
LL | a = b.to_owned();
74+
| ^^^^^^^^^^^^^^^^ help: use `clone_into()`: `b.clone_into(&mut a)`
75+
76+
error: assigning the result of `ToOwned::to_owned()` may be inefficient
77+
--> tests/ui/assigning_clones.rs:156:5
7278
|
7379
LL | *mut_string = ref_str.to_owned();
7480
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(mut_string)`
7581

7682
error: assigning the result of `ToOwned::to_owned()` may be inefficient
77-
--> tests/ui/assigning_clones.rs:149:5
83+
--> tests/ui/assigning_clones.rs:160:5
7884
|
7985
LL | mut_string = ref_str.to_owned();
8086
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut mut_string)`
8187

8288
error: assigning the result of `ToOwned::to_owned()` may be inefficient
83-
--> tests/ui/assigning_clones.rs:170:5
89+
--> tests/ui/assigning_clones.rs:181:5
8490
|
8591
LL | **mut_box_string = ref_str.to_owned();
8692
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
8793

8894
error: assigning the result of `ToOwned::to_owned()` may be inefficient
89-
--> tests/ui/assigning_clones.rs:174:5
95+
--> tests/ui/assigning_clones.rs:185:5
9096
|
9197
LL | **mut_box_string = ref_str.to_owned();
9298
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ref_str.clone_into(&mut (*mut_box_string))`
9399

94100
error: assigning the result of `ToOwned::to_owned()` may be inefficient
95-
--> tests/ui/assigning_clones.rs:178:5
101+
--> tests/ui/assigning_clones.rs:189:5
96102
|
97103
LL | *mut_thing = ToOwned::to_owned(ref_str);
98104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, mut_thing)`
99105

100106
error: assigning the result of `ToOwned::to_owned()` may be inefficient
101-
--> tests/ui/assigning_clones.rs:182:5
107+
--> tests/ui/assigning_clones.rs:193:5
102108
|
103109
LL | mut_thing = ToOwned::to_owned(ref_str);
104110
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `clone_into()`: `ToOwned::clone_into(ref_str, &mut mut_thing)`
105111

106-
error: aborting due to 17 previous errors
112+
error: aborting due to 18 previous errors
107113

0 commit comments

Comments
 (0)