Skip to content

Fixes manual_flatten removes the useless if let #14861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

donkomura
Copy link
Contributor

@donkomura donkomura commented May 21, 2025

Closes #14692

The suggestion of manual_flatten does not includes the replacement of if let so far despite of .flatten() suggestion.
This PR eliminates a redundant if let.

changelog: [manual_flatten] the suggestion removes if let

@donkomura donkomura changed the title flatten and remove the if let pat [manual flatten] flatten and remove the useless if let May 21, 2025
@donkomura donkomura force-pushed the manual_flatten_snippet branch from e9606eb to ac06a73 Compare May 21, 2025 14:24
@donkomura donkomura changed the title [manual flatten] flatten and remove the useless if let Fixes manual_flatten removes the useless if let May 21, 2025
Comment on lines 59 to 66
// If suggestion is not a one-liner, it won't be shown inline within the error message. In that
// case, it will be shown in the extra `help` message at the end, which is why the first
// `help_msg` needs to refer to the correct relative position of the suggestion.
let help_msg = if sugg.contains('\n') {
"remove the `if let` statement in the for loop and then..."
} else {
"...and remove the `if let` statement in the for loop"
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion always have multiple lines, so we can become the help message simplify.
(or should we suggest the one-liner and separate the suggestion that includes removing if let?)

@donkomura donkomura marked this pull request as ready for review May 21, 2025 14:38
@rustbot
Copy link
Collaborator

rustbot commented May 21, 2025

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label May 21, 2025
Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is supposed to now be auto-fixable, then //@no-rustfix should be removed from the test file.

However, the current suggestion don't appear to be valid unless the binding from the Some() or Ok() is a single identifier with the same name as the identifier from the for loop.

For example, given this code:

    for x in [Some(3)] {
        if let Some(y)  = x {
            println!("{y}");
        }
    }

the suggestion will not compile:

    for x in [Some(3)].into_iter().flatten() {
        println!("{y}");
    }

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 21, 2025
Comment on lines +124 to +126
LL ~ for m in z.flatten() {
LL + println!("{}", m);
LL + }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd fixed it to out valid code.

@donkomura donkomura requested a review from samueltardieu May 22, 2025 15:54
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 22, 2025
Copy link
Contributor

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting good.

//@no-rustfix is still in place and should be removed, so that autofixes can be tested as well.

Also, the following code makes the lint fail to give a good error message and fix the problem:

    macro_rules! inner {
        ($id:ident / $new:pat => $action:expr) => { if let Some($new) = $id { $action; } }
    }

     for ab in [Some((1, 2)), Some((3, 4))] {
         inner!(ab / (c, d) => println!("{c}-{d}"));
    }

because it gets confused by the macro. The lint should probably not trigger if the for loop, the if let, or its test, don't all come from the same context.

cx,
let_pats.first().unwrap().span.source_callsite(),
"_",
&mut applicability,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you initialize applicability with MachineApplicable if the suggestion is complete?

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 23, 2025
@Alexendoo Alexendoo linked an issue May 24, 2025 that may be closed by this pull request
@donkomura donkomura force-pushed the manual_flatten_snippet branch from 3a54cb7 to d4b1e62 Compare May 26, 2025 14:01
@donkomura
Copy link
Contributor Author

Thank you for pointing that out. I made corrections to prevent the macro from triggering a lint. I also adjusted the Applicability.

@donkomura donkomura requested a review from samueltardieu May 26, 2025 14:43
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 26, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 26, 2025

☔ The latest upstream changes (possibly 3927a61) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

manual_flatten doesn't follow its suggestion to remove if let statement leading to broken code
3 participants