Skip to content
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

Fix infinite loop in cast_sign_loss when peeling unwrap method calls #12508

Merged
merged 1 commit into from
Mar 22, 2024

Conversation

y21
Copy link
Member

@y21 y21 commented Mar 18, 2024

Fixes #12506

The lint wants to peel method calls but didn't actually reassign the expression, leading to an infinite loop.


changelog: Fix infinite loop in [cast_sign_loss] when having two chained .unwrap() calls

@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2024

r? @llogiq

rustbot has assigned @llogiq.
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 Mar 18, 2024
@@ -134,11 +134,12 @@ fn expr_sign<'cx>(cx: &LateContext<'cx>, expr: &Expr<'_>, ty: impl Into<Option<T
// Peel unwrap(), expect(), etc.
while let Some(&found_name) = METHODS_UNWRAP.iter().find(|&name| &method_name == name)
&& let Some(arglist) = method_chain_args(expr, &[found_name])
Copy link
Member Author

Choose a reason for hiding this comment

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

actually now that I'm taking a closer look at the lint code, this method_chain_args call seems redundant? It doesn't really seem to do anything in this context, except for returning the expression back that was passed in, as far as I can tell.
The line right above it already checks that it's a call to one of the unwrap family of methods.

I removed it locally and the tests are still passing, would be good to have someone else look over this before I actually remove it tho, maybe it's relevant for something else

Copy link
Contributor

@llogiq llogiq Mar 20, 2024

Choose a reason for hiding this comment

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

The only additional check it does is whether the method path or any of the rags come from a macro/expansion. If so, the call will return None.

I don't think that should matter in the context of this lint.

Copy link
Member Author

@y21 y21 Mar 20, 2024

Choose a reason for hiding this comment

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

do you mean that we should just keep it then? I guess we can. this just seemed odd to me because the function is generally useful for finding method chains, but is being used for a very different purpose here and doesn't actually look for any chains the way it's used (perhaps confusingly, since the loop is what ends up looking for chains) and does a bunch of unnecessary work like building a vec

@llogiq
Copy link
Contributor

llogiq commented Mar 20, 2024

Thank you!

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 20, 2024

📌 Commit 3930f8b has been approved by llogiq

It is now in the queue for this repository.

bors added a commit that referenced this pull request Mar 20, 2024
Fix infinite loop in `cast_sign_loss` when peeling unwrap method calls

Fixes #12506

The lint wants to peel method calls but didn't actually reassign the expression, leading to an infinite loop.

changelog: Fix infinite loop in [`cast_sign_loss`] when having two chained `.unwrap()` calls
@bors
Copy link
Collaborator

bors commented Mar 20, 2024

⌛ Testing commit 3930f8b with merge 3fa5ce8...

@bors
Copy link
Collaborator

bors commented Mar 20, 2024

💔 Test failed - checks-action_test

@samueltardieu
Copy link
Contributor

@llogiq Any idea of what went wrong?

@y21
Copy link
Member Author

y21 commented Mar 22, 2024

Probably just a spurious ci error. That happened on another PR and retrying worked. But I also wanted to talk about that one method call first before merging while we're at it, and it looks like its waiting for that atm. I'd also be fine with just leaving it if we want this fixed asap

@llogiq
Copy link
Contributor

llogiq commented Mar 22, 2024

@bors retry

@bors
Copy link
Collaborator

bors commented Mar 22, 2024

⌛ Testing commit 3930f8b with merge f2020c8...

@bors
Copy link
Collaborator

bors commented Mar 22, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing f2020c8 to master...

@bors bors merged commit f2020c8 into rust-lang:master Mar 22, 2024
5 checks passed
@y21 y21 added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Mar 22, 2024
@y21
Copy link
Member Author

y21 commented Mar 22, 2024

Nominated for beta backport since the bug made it to beta and looks like something that people might accidentally run into

@stevenengler
Copy link
Contributor

Nominated for beta backport since the bug made it to beta and looks like something that people might accidentally run into

FWIW, here's the actual real-world code that caused me to open #12506:

fn page_size() -> usize {
    nix::unistd::sysconf(nix::unistd::SysconfVar::PAGE_SIZE)
        .unwrap()
        .unwrap() as usize
}

The lack of error checking is because we just use this in a test case.

@flip1995 flip1995 added beta-accepted Accepted for backporting to the compiler in the beta channel. and removed beta-nominated Nominated for backporting to the compiler in the beta channel. labels Apr 25, 2024
@flip1995
Copy link
Member

rust-lang/rust#124369

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 27, 2024
…ulacrum

[beta] Clippy backport

r? `@Mark-Simulacrum`

Backports:

- rust-lang/rust-clippy#12486
- rust-lang/rust-clippy#12572
- rust-lang/rust-clippy#12508
- rust-lang/rust-clippy#12617

The first one is a bit bigger as usual for a backport. But it fixes a major issue with this lint that we overlooked. So I think this is worth it. After that was merged into nightly, there were no new issues opened about this lint, so IMO this is safe to backport to `beta` and put into stable.
@xFrednet xFrednet removed the beta-accepted Accepted for backporting to the compiler in the beta channel. label Apr 29, 2024
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.

Clippy hangs on .unwrap().unwrap() as usize on nightly
8 participants