Not linting irrefutable_let_patterns on let chains#146832
Not linting irrefutable_let_patterns on let chains#146832rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
11d320a to
450d0a3
Compare
This comment has been minimized.
This comment has been minimized.
450d0a3 to
0aae158
Compare
This comment has been minimized.
This comment has been minimized.
ce66065 to
e77a3c4
Compare
|
Some changes occurred in match checking cc @Nadrieril The Miri subtree was changed cc @rust-lang/miri |
|
It seems that only modifying |
All the lints seem to be removed in that test, but you'll want to ensure there are tests that cover the edges of what is still linted here. @rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
e77a3c4 to
6425c06
Compare
This comment has been minimized.
This comment has been minimized.
cdf1cde to
53c05ef
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
53c05ef to
e0f8693
Compare
This comment has been minimized.
This comment has been minimized.
e0f8693 to
e2a6695
Compare
This comment has been minimized.
This comment has been minimized.
inline rest of the check try fix ci errors inline in check_let
e2a6695 to
4f31ff8
Compare
|
I resolved the merge conflicts, can we proceed with merging this PR now? |
|
@rustbot ready |
|
@bors r+ |
|
📋 This PR cannot be approved because it currently has the following label: |
|
@bors r+ |
…uwer Rollup of 9 pull requests Successful merges: - #146832 (Not linting irrefutable_let_patterns on let chains) - #146972 (Support importing path-segment keyword with renaming) - #152241 (For panic=unwind on Wasm targets, define __cpp_exception tag) - #152527 (Remove -Zemit-thin-lto flag) - #152769 (Do not cancel try builds after first job failure) - #152907 (Add tests for delegation generics) - #152455 (Remove the translation `-Z` options and the `Translator` type. ) - #152813 (Skip the `use_existential_projection_new_instead` field in the `Debug` impl) - #152912 (Expose Span for all DefIds in rustc_public)
Rollup merge of #146832 - Natural-selection1:not-in-chains, r=petrochenkov Not linting irrefutable_let_patterns on let chains *[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/146832)* # Description this PR makes the lint `irrefutable_let_patterns` not check for `let chains`, only check for single `if let`, `while let`, and `if let guard`. # Motivation Since `let chains` were stabilized, the following code has become common: ```rust fn max() -> usize { 42 } fn main() { if let mx = max() && mx < usize::MAX { /* */ } } ``` This code naturally expresses "please call that function and then do something if the return value satisfies a condition". Putting the let binding outside the if would be bad as then it remains in scope after the if, which is not the intent. Current Output: ```bash warning: leading irrefutable pattern in let chain --> src/main.rs:7:8 | 7 | if let mx = max() && mx < usize::MAX { | ^^^^^^^^^^^^^^ | = note: this pattern will always match = help: consider moving it outside of the construct = note: `#[warn(irrefutable_let_patterns)]` on by default ``` Another common case is progressively destructuring a struct with enum fields, or an enum with struct variants: ```rust struct NameOfOuterStruct { middle: NameOfMiddleEnum, other: (), } enum NameOfMiddleEnum { Inner(NameOfInnerStruct), Other(()), } struct NameOfInnerStruct { id: u32, } fn test(outer: NameOfOuterStruct) { if let NameOfOuterStruct { middle, .. } = outer && let NameOfMiddleEnum::Inner(inner) = middle && let NameOfInnerStruct { id } = inner { /* */ } } ``` Current Output: ```bash warning: leading irrefutable pattern in let chain --> src\main.rs:17:8 | 17 | if let NameOfOuterStruct { middle, .. } = outer | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this pattern will always match = help: consider moving it outside of the construct = note: `#[warn(irrefutable_let_patterns)]` on by default warning: trailing irrefutable pattern in let chain --> src\main.rs:19:12 | 19 | && let NameOfInnerStruct { id } = inner | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this pattern will always match = help: consider moving it into the body ``` To avoid the warning, the readability would be much worse: ```rust fn test(outer: NameOfOuterStruct) { if let NameOfOuterStruct { middle: NameOfMiddleEnum::Inner(NameOfInnerStruct { id }), .. } = outer { /* */ } } ``` # related issue * #139369 # possible questions 1. Moving the irrefutable pattern at the head of the chain out of it would cause a variable that was intended to be temporary to remain in scope, so we remove it. However, should we keep the check for moving the irrefutable pattern at the tail into the body? 2. Should we still lint `entire chain is made up of irrefutable let`? --- This is my first time contributing non-documentation code to Rust. If there are any irregularities, please feel free to point them out. : )
…uwer Rollup of 9 pull requests Successful merges: - rust-lang/rust#146832 (Not linting irrefutable_let_patterns on let chains) - rust-lang/rust#146972 (Support importing path-segment keyword with renaming) - rust-lang/rust#152241 (For panic=unwind on Wasm targets, define __cpp_exception tag) - rust-lang/rust#152527 (Remove -Zemit-thin-lto flag) - rust-lang/rust#152769 (Do not cancel try builds after first job failure) - rust-lang/rust#152907 (Add tests for delegation generics) - rust-lang/rust#152455 (Remove the translation `-Z` options and the `Translator` type. ) - rust-lang/rust#152813 (Skip the `use_existential_projection_new_instead` field in the `Debug` impl) - rust-lang/rust#152912 (Expose Span for all DefIds in rustc_public)
…uwer Rollup of 9 pull requests Successful merges: - rust-lang/rust#146832 (Not linting irrefutable_let_patterns on let chains) - rust-lang/rust#146972 (Support importing path-segment keyword with renaming) - rust-lang/rust#152241 (For panic=unwind on Wasm targets, define __cpp_exception tag) - rust-lang/rust#152527 (Remove -Zemit-thin-lto flag) - rust-lang/rust#152769 (Do not cancel try builds after first job failure) - rust-lang/rust#152907 (Add tests for delegation generics) - rust-lang/rust#152455 (Remove the translation `-Z` options and the `Translator` type. ) - rust-lang/rust#152813 (Skip the `use_existential_projection_new_instead` field in the `Debug` impl) - rust-lang/rust#152912 (Expose Span for all DefIds in rustc_public)

View all comments
Description
this PR makes the lint
irrefutable_let_patternsnot check forlet chains,only check for single
if let,while let, andif let guard.Motivation
Since
let chainswere stabilized, the following code has become common:This code naturally expresses "please call that function and then do something if the return value satisfies a condition".
Putting the let binding outside the if would be bad as then it remains in scope after the if, which is not the intent.
Current Output:
Another common case is progressively destructuring a struct with enum fields, or an enum with struct variants:
Current Output:
To avoid the warning, the readability would be much worse:
related issue
possible questions
Moving the irrefutable pattern at the head of the chain out of it would cause a variable that was intended to be temporary to remain in scope, so we remove it.
However, should we keep the check for moving the irrefutable pattern at the tail into the body?
Should we still lint
entire chain is made up of irrefutable let?This is my first time contributing non-documentation code to Rust. If there are any irregularities, please feel free to point them out.
: )