Skip to content

Commit 3be3a9c

Browse files
authored
Enhance needless_late_init to cover grouped assignments (#16746)
*[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust-clippy/pull/16746)* Closes #16330 Implemented as an enhancement to `needless_late_init`. Also fixes a FN when if/match is in block expr. changelog: [`needless_late_init`] fix FN for if/match in block expr changelog: [`needless_late_init`] extend to cover grouped assignments
2 parents 4755296 + 6d4ad63 commit 3be3a9c

14 files changed

Lines changed: 1029 additions & 215 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7574,6 +7574,7 @@ Released 2018-09-13
75747574
[`avoid-breaking-exported-api`]: https://doc.rust-lang.org/clippy/lint_configuration.html#avoid-breaking-exported-api
75757575
[`await-holding-invalid-types`]: https://doc.rust-lang.org/clippy/lint_configuration.html#await-holding-invalid-types
75767576
[`cargo-ignore-publish`]: https://doc.rust-lang.org/clippy/lint_configuration.html#cargo-ignore-publish
7577+
[`check-grouped-late-init`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-grouped-late-init
75777578
[`check-incompatible-msrv-in-tests`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-incompatible-msrv-in-tests
75787579
[`check-inconsistent-struct-field-initializers`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-inconsistent-struct-field-initializers
75797580
[`check-private-items`]: https://doc.rust-lang.org/clippy/lint_configuration.html#check-private-items

book/src/lint_configuration.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,37 @@ For internal testing only, ignores the current `publish` settings in the Cargo m
452452
* [`cargo_common_metadata`](https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata)
453453

454454

455+
## `check-grouped-late-init`
456+
Whether to check for grouped late initializations from multiple `let` statements.
457+
458+
#### Example
459+
```rust
460+
let a;
461+
let b;
462+
if true {
463+
a = 1;
464+
b = 2;
465+
} else {
466+
a = 3;
467+
b = 4;
468+
}
469+
```
470+
Use instead:
471+
```rust
472+
let (a, b) = if true {
473+
(1, 2)
474+
} else {
475+
(3, 4)
476+
};
477+
```
478+
479+
**Default Value:** `true`
480+
481+
---
482+
**Affected lints:**
483+
* [`needless_late_init`](https://rust-lang.github.io/rust-clippy/master/index.html#needless_late_init)
484+
485+
455486
## `check-incompatible-msrv-in-tests`
456487
Whether to check MSRV compatibility in `#[test]` and `#[cfg(test)]` code.
457488

clippy_config/src/conf.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,30 @@ define_Conf! {
554554
/// For internal testing only, ignores the current `publish` settings in the Cargo manifest.
555555
#[lints(cargo_common_metadata)]
556556
cargo_ignore_publish: bool = false,
557+
/// Whether to check for grouped late initializations from multiple `let` statements.
558+
///
559+
/// #### Example
560+
/// ```rust
561+
/// let a;
562+
/// let b;
563+
/// if true {
564+
/// a = 1;
565+
/// b = 2;
566+
/// } else {
567+
/// a = 3;
568+
/// b = 4;
569+
/// }
570+
/// ```
571+
/// Use instead:
572+
/// ```rust
573+
/// let (a, b) = if true {
574+
/// (1, 2)
575+
/// } else {
576+
/// (3, 4)
577+
/// };
578+
/// ```
579+
#[lints(needless_late_init)]
580+
check_grouped_late_init: bool = true,
557581
/// Whether to check MSRV compatibility in `#[test]` and `#[cfg(test)]` code.
558582
#[lints(incompatible_msrv)]
559583
check_incompatible_msrv_in_tests: bool = false,

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ rustc_lint::late_lint_methods!(
731731
UndocumentedUnsafeBlocks: undocumented_unsafe_blocks::UndocumentedUnsafeBlocks = undocumented_unsafe_blocks::UndocumentedUnsafeBlocks::new(conf),
732732
FormatArgs: format_args::FormatArgs<'tcx> = format_args::FormatArgs::new(tcx, conf, format_args.clone()),
733733
TrailingEmptyArray: trailing_empty_array::TrailingEmptyArray = trailing_empty_array::TrailingEmptyArray,
734-
NeedlessLateInit: needless_late_init::NeedlessLateInit = needless_late_init::NeedlessLateInit,
734+
NeedlessLateInit: needless_late_init::NeedlessLateInit<'tcx> = needless_late_init::NeedlessLateInit::new(conf),
735735
ReturnSelfNotMustUse: return_self_not_must_use::ReturnSelfNotMustUse = return_self_not_must_use::ReturnSelfNotMustUse,
736736
NumberedFields: init_numbered_fields::NumberedFields = init_numbered_fields::NumberedFields,
737737
ManualBits: manual_bits::ManualBits = manual_bits::ManualBits::new(conf),

0 commit comments

Comments
 (0)