Skip to content

Commit 5a82d16

Browse files
committed
Merge commit '9d6f41691ed9dbfaec2a2df2661c42451f2fe0d3' into clippy-subtree-update
2 parents a0569fa + 9d6f416 commit 5a82d16

File tree

187 files changed

+4182
-1047
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

187 files changed

+4182
-1047
lines changed

src/tools/clippy/.github/workflows/clippy.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,19 @@ env:
2626
NO_FMT_TEST: 1
2727
CARGO_INCREMENTAL: 0
2828

29+
concurrency:
30+
# For a given workflow, if we push to the same PR, cancel all previous builds on that PR.
31+
# If the push is not attached to a PR, we will cancel all builds on the same branch.
32+
group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
33+
cancel-in-progress: true
34+
2935
jobs:
3036
base:
3137
# NOTE: If you modify this job, make sure you copy the changes to clippy_bors.yml
3238
runs-on: ubuntu-latest
3339

3440
steps:
3541
# Setup
36-
- uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
37-
with:
38-
github_token: "${{ secrets.github_token }}"
39-
4042
- name: Checkout
4143
uses: actions/checkout@v4
4244

src/tools/clippy/.github/workflows/clippy_bors.yml

+5-20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ env:
1212
NO_FMT_TEST: 1
1313
CARGO_INCREMENTAL: 0
1414

15+
concurrency:
16+
# For a given workflow, if we push to the same branch, cancel all previous builds on that branch.
17+
group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
18+
cancel-in-progress: true
19+
1520
defaults:
1621
run:
1722
shell: bash
@@ -21,10 +26,6 @@ jobs:
2126
runs-on: ubuntu-latest
2227

2328
steps:
24-
- uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
25-
with:
26-
github_token: "${{ secrets.github_token }}"
27-
2829
- name: Checkout
2930
uses: actions/checkout@v4
3031
with:
@@ -67,10 +68,6 @@ jobs:
6768
# NOTE: If you modify this job, make sure you copy the changes to clippy.yml
6869
steps:
6970
# Setup
70-
- uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
71-
with:
72-
github_token: "${{ secrets.github_token }}"
73-
7471
- name: Checkout
7572
uses: actions/checkout@v4
7673

@@ -131,10 +128,6 @@ jobs:
131128

132129
steps:
133130
# Setup
134-
- uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
135-
with:
136-
github_token: "${{ secrets.github_token }}"
137-
138131
- name: Checkout
139132
uses: actions/checkout@v4
140133

@@ -155,10 +148,6 @@ jobs:
155148

156149
steps:
157150
# Setup
158-
- uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
159-
with:
160-
github_token: "${{ secrets.github_token }}"
161-
162151
- name: Checkout
163152
uses: actions/checkout@v4
164153

@@ -211,10 +200,6 @@ jobs:
211200

212201
steps:
213202
# Setup
214-
- uses: rust-lang/simpleinfra/github-actions/cancel-outdated-builds@master
215-
with:
216-
github_token: "${{ secrets.github_token }}"
217-
218203
- name: Checkout
219204
uses: actions/checkout@v4
220205

src/tools/clippy/CHANGELOG.md

+61-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,65 @@ document.
66

77
## Unreleased / Beta / In Rust Nightly
88

9-
[a859e5cc...master](https://github.com/rust-lang/rust-clippy/compare/a859e5cc...master)
9+
[66c29b97...master](https://github.com/rust-lang/rust-clippy/compare/66c29b97...master)
10+
11+
## Rust 1.77
12+
13+
Current stable, released 2024-03-18
14+
15+
[View all 93 merged pull requests](https://github.com/rust-lang/rust-clippy/pulls?q=merged%3A2023-12-16T18%3A20%3A00Z..2024-01-25T18%3A15%3A56Z+base%3Amaster)
16+
17+
### New Lints
18+
19+
* [`suspicious_open_options`]
20+
[#11608](https://github.com/rust-lang/rust-clippy/pull/11608)
21+
* [`option_as_ref_cloned`]
22+
[#12051](https://github.com/rust-lang/rust-clippy/pull/12051)
23+
* [`thread_local_initializer_can_be_made_const`]
24+
[#12026](https://github.com/rust-lang/rust-clippy/pull/12026)
25+
* [`str_split_at_newline`]
26+
[#11987](https://github.com/rust-lang/rust-clippy/pull/11987)
27+
* [`empty_enum_variants_with_brackets`]
28+
[#12047](https://github.com/rust-lang/rust-clippy/pull/12047)
29+
* [`manual_is_variant_and`]
30+
[#11865](https://github.com/rust-lang/rust-clippy/pull/11865)
31+
* [`pub_underscore_fields`]
32+
[#10283](https://github.com/rust-lang/rust-clippy/pull/10283)
33+
* [`eager_transmute`]
34+
[#11981](https://github.com/rust-lang/rust-clippy/pull/11981)
35+
* [`iter_filter_is_some`]
36+
[#12004](https://github.com/rust-lang/rust/pull/12004)
37+
* [`iter_filter_is_ok`]
38+
[#12004](https://github.com/rust-lang/rust/pull/12004)
39+
* [`result_filter_map`]
40+
[#11869](https://github.com/rust-lang/rust-clippy/pull/11869)
41+
* [`unconditional_recursion`]
42+
[#11938](https://github.com/rust-lang/rust-clippy/pull/11938)
43+
44+
### Enhancements
45+
46+
* [`multiple_crate_versions`]: Added the [`allowed-duplicate-crates`] configuration to allow specific crates
47+
[#12179](https://github.com/rust-lang/rust-clippy/pull/12179)
48+
* [`single_call_fn`]: No longer ignores `#[allow]` attributes
49+
[#12183](https://github.com/rust-lang/rust-clippy/pull/12183)
50+
* [`read_zero_byte_vec`]: Updated the heuristics used for linting
51+
[#11766](https://github.com/rust-lang/rust-clippy/pull/11766)
52+
53+
### ICE Fixes
54+
55+
* [`unit_arg`]: No longer crashes when checking for const in nested bodies
56+
[#11977](https://github.com/rust-lang/rust-clippy/pull/11977)
57+
* [`indexing_slicing`]: No longer crashes when the array index exceeds `usize`
58+
[#12266](https://github.com/rust-lang/rust-clippy/pull/12266)
59+
60+
### Others
61+
62+
* Warnings about invalid fields inside `clippy.toml` files now include suggestions for existing fields
63+
[#12180](https://github.com/rust-lang/rust-clippy/pull/12180)
1064

1165
## Rust 1.76
1266

13-
Current stable, released 2024-02-08
67+
Released 2024-02-08
1468

1569
[View all 85 merged pull requests](https://github.com/rust-lang/rust-clippy/pulls?q=merged%3A2023-11-02T20%3A23%3A40Z..2023-12-16T13%3A11%3A08Z+base%3Amaster)
1670

@@ -5110,6 +5164,7 @@ Released 2018-09-13
51105164
[`collection_is_never_read`]: https://rust-lang.github.io/rust-clippy/master/index.html#collection_is_never_read
51115165
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
51125166
[`comparison_to_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_to_empty
5167+
[`const_is_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_is_empty
51135168
[`const_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_static_lifetime
51145169
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
51155170
[`crate_in_macro_def`]: https://rust-lang.github.io/rust-clippy/master/index.html#crate_in_macro_def
@@ -5156,6 +5211,7 @@ Released 2018-09-13
51565211
[`drop_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#drop_ref
51575212
[`duplicate_mod`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicate_mod
51585213
[`duplicate_underscore_argument`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicate_underscore_argument
5214+
[`duplicated_attributes`]: https://rust-lang.github.io/rust-clippy/master/index.html#duplicated_attributes
51595215
[`duration_subsec`]: https://rust-lang.github.io/rust-clippy/master/index.html#duration_subsec
51605216
[`eager_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#eager_transmute
51615217
[`else_if_without_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#else_if_without_else
@@ -5279,6 +5335,7 @@ Released 2018-09-13
52795335
[`int_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#int_plus_one
52805336
[`integer_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_arithmetic
52815337
[`integer_division`]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_division
5338+
[`integer_division_remainder_used`]: https://rust-lang.github.io/rust-clippy/master/index.html#integer_division_remainder_used
52825339
[`into_iter_on_array`]: https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_array
52835340
[`into_iter_on_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_on_ref
52845341
[`into_iter_without_iter`]: https://rust-lang.github.io/rust-clippy/master/index.html#into_iter_without_iter
@@ -5376,6 +5433,7 @@ Released 2018-09-13
53765433
[`manual_swap`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_swap
53775434
[`manual_try_fold`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_try_fold
53785435
[`manual_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or
5436+
[`manual_unwrap_or_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or_default
53795437
[`manual_while_let_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_while_let_some
53805438
[`many_single_char_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#many_single_char_names
53815439
[`map_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
@@ -5813,6 +5871,7 @@ Released 2018-09-13
58135871
[`zero_divided_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#zero_divided_by_zero
58145872
[`zero_prefixed_literal`]: https://rust-lang.github.io/rust-clippy/master/index.html#zero_prefixed_literal
58155873
[`zero_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#zero_ptr
5874+
[`zero_repeat_side_effects`]: https://rust-lang.github.io/rust-clippy/master/index.html#zero_repeat_side_effects
58165875
[`zero_sized_map_values`]: https://rust-lang.github.io/rust-clippy/master/index.html#zero_sized_map_values
58175876
[`zero_width_space`]: https://rust-lang.github.io/rust-clippy/master/index.html#zero_width_space
58185877
[`zst_offset`]: https://rust-lang.github.io/rust-clippy/master/index.html#zst_offset

src/tools/clippy/Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clippy"
3-
version = "0.1.78"
3+
version = "0.1.79"
44
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
55
repository = "https://github.com/rust-lang/rust-clippy"
66
readme = "README.md"
@@ -24,26 +24,26 @@ path = "src/driver.rs"
2424
clippy_config = { path = "clippy_config" }
2525
clippy_lints = { path = "clippy_lints" }
2626
rustc_tools_util = "0.3.0"
27-
tempfile = { version = "3.2", optional = true }
27+
tempfile = { version = "3.3", optional = true }
2828
termize = "0.1"
2929
color-print = "0.3.4"
3030
anstream = "0.6.0"
3131

3232
[dev-dependencies]
3333
ui_test = "0.22.2"
3434
tester = "0.9"
35-
regex = "1.5"
35+
regex = "1.5.5"
3636
toml = "0.7.3"
3737
walkdir = "2.3"
3838
# This is used by the `collect-metadata` alias.
39-
filetime = "0.2"
39+
filetime = "0.2.9"
4040
itertools = "0.12"
4141

4242
# UI test dependencies
4343
clippy_utils = { path = "clippy_utils" }
4444
if_chain = "1.0"
45-
quote = "1.0"
46-
serde = { version = "1.0.125", features = ["derive"] }
45+
quote = "1.0.25"
46+
serde = { version = "1.0.145", features = ["derive"] }
4747
syn = { version = "2.0", features = ["full"] }
4848
futures = "0.3"
4949
parking_lot = "0.12"

src/tools/clippy/book/src/development/macro_expansions.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if expr.span.from_expansion() {
5252

5353
### `Span.ctxt` method
5454

55-
The `span`'s context, given by the method [`ctxt`] and returning [SpanContext],
55+
The `span`'s context, given by the method [`ctxt`] and returning [SyntaxContext],
5656
represents if the span is from a macro expansion and, if it is, which
5757
macro call expanded this span.
5858

@@ -155,4 +155,4 @@ if in_external_macro(cx.sess(), foo_span) {
155155
[`from_expansion`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/struct.Span.html#method.from_expansion
156156
[`in_external_macro`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_middle/lint/fn.in_external_macro.html
157157
[Span]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/struct.Span.html
158-
[SpanContext]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/hygiene/struct.SyntaxContext.html
158+
[SyntaxContext]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/hygiene/struct.SyntaxContext.html

src/tools/clippy/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)

src/tools/clippy/clippy.toml

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
avoid-breaking-exported-api = false
22

3-
# use the various `span_lint_*` methods instead, which also add a link to the docs
4-
disallowed-methods = [
5-
"rustc_lint::context::LintContext::span_lint",
6-
"rustc_middle::ty::context::TyCtxt::node_span_lint"
7-
]
3+
[[disallowed-methods]]
4+
path = "rustc_lint::context::LintContext::span_lint"
5+
reason = "this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint*` functions instead"
6+
7+
8+
[[disallowed-methods]]
9+
path = "rustc_middle::ty::context::TyCtxt::node_span_lint"
10+
reason = "this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint_hir*` functions instead"

src/tools/clippy/clippy_config/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clippy_config"
3-
version = "0.1.78"
3+
version = "0.1.79"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/tools/clippy/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 = ""]

src/tools/clippy/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 }

src/tools/clippy/clippy_dev/src/update_lints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,8 @@ fn gen_deprecated_lints_test(lints: &[DeprecatedLint]) -> String {
689689
fn gen_renamed_lints_test(lints: &[RenamedLint]) -> String {
690690
let mut seen_lints = HashSet::new();
691691
let mut res: String = GENERATED_FILE_COMMENT.into();
692+
693+
res.push_str("#![allow(clippy::duplicated_attributes)]\n");
692694
for lint in lints {
693695
if seen_lints.insert(&lint.new_name) {
694696
writeln!(res, "#![allow({})]", lint.new_name).unwrap();

src/tools/clippy/clippy_lints/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "clippy_lints"
3-
version = "0.1.78"
3+
version = "0.1.79"
44
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
55
repository = "https://github.com/rust-lang/rust-clippy"
66
readme = "README.md"

src/tools/clippy/clippy_lints/src/assigning_clones.rs

+25-4
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,7 +50,19 @@ 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<'_>) {
@@ -68,10 +81,12 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
6881
return;
6982
};
7083

71-
if is_ok_to_suggest(cx, lhs, &call) {
84+
if is_ok_to_suggest(cx, lhs, &call, &self.msrv) {
7285
suggest(cx, assign_expr, lhs, &call);
7386
}
7487
}
88+
89+
extract_msrv_attr!(LateContext);
7590
}
7691

7792
// Try to resolve the call to `Clone::clone` or `ToOwned::to_owned`.
@@ -135,7 +150,13 @@ fn extract_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Option<
135150

136151
// Return true if we find that the called method has a custom implementation and isn't derived or
137152
// provided by default by the corresponding trait.
138-
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+
139160
// If the left-hand side is a local variable, it might be uninitialized at this point.
140161
// In that case we do not want to suggest the lint.
141162
if let Some(local) = path_to_local(lhs) {

0 commit comments

Comments
 (0)