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

new lint to detect infinite loop #11829

Merged
merged 4 commits into from
Dec 12, 2023
Merged

new lint to detect infinite loop #11829

merged 4 commits into from
Dec 12, 2023

Conversation

J-ZhengLi
Copy link
Member

@J-ZhengLi J-ZhengLi commented Nov 17, 2023

closes: #11438

changelog: add new lint to detect infinite loop

I'll change the lint name. Should I name it infinite_loop or infinite_loops is fine? Ahhhh, English is hard...

@rustbot
Copy link
Collaborator

rustbot commented Nov 17, 2023

r? @Manishearth

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 17, 2023
@J-ZhengLi J-ZhengLi marked this pull request as draft November 17, 2023 07:57
@matthiaskrgr
Copy link
Member

What about loops that have todo!() / panic!() / unreachable() / assert!() in them? We have at least a test imo 🙂

@Manishearth
Copy link
Member

r? @matthiaskrgr

might not have time to get around to this soon

@rustbot rustbot assigned matthiaskrgr and unassigned Manishearth Nov 20, 2023
@J-ZhengLi J-ZhengLi force-pushed the issue11438 branch 2 times, most recently from 1516cda to 1d15de5 Compare November 21, 2023 02:15
@J-ZhengLi J-ZhengLi marked this pull request as ready for review November 21, 2023 02:16
@J-ZhengLi J-ZhengLi marked this pull request as draft November 21, 2023 03:17
@J-ZhengLi J-ZhengLi marked this pull request as ready for review November 21, 2023 07:25
@J-ZhengLi J-ZhengLi force-pushed the issue11438 branch 3 times, most recently from 6ae6d02 to 3e9a6d1 Compare November 21, 2023 08:17
and add more test cases
clippy_lints/src/loops/mod.rs Outdated Show resolved Hide resolved
#[clippy::version = "1.75.0"]
pub INFINITE_LOOP,
restriction,
"possibly unintended infinite loops"
Copy link
Member

Choose a reason for hiding this comment

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

loops -> loop?
I think this reads a bit better.

Copy link
Member

Choose a reason for hiding this comment

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

I also wondering about weird fn like

fn loopfn() -> Result<(), i32> {
    Ok(loop {})
}

afaik something like -> Result <!, i32> is not stabilized yet.

Copy link
Member Author

@J-ZhengLi J-ZhengLi Nov 28, 2023

Choose a reason for hiding this comment

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

I also wondering about weird fn like

Hmmm, Sorry I wasn't aware such cases, currently it would just prints help message suggesting adding a break condition...
What would be an ideal solution here 🤔 ?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, was on holiday :)

If it only suggests adding break/return, I think it should be fine then.

Another scenario that just came to my mind now:

loop {
continue;
}

This does have some kind of "control flow" but in this case it still loops indefinitely.
The linkt would lint here as well, right? (which is the correct solution I think)

Copy link
Member Author

Choose a reason for hiding this comment

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

Happy holiday 😄

As of your concern, yes it will lint, right now the visitor only looks for following occurrence in a loop block:

  1. A return or a break
  2. A function which never return, which was inspired be one of your early suggestion such as the one in panic!, unreachable!, also std::process::exit.

And if non of these could be find then... well, triggers the warning.

@J-ZhengLi J-ZhengLi force-pushed the issue11438 branch 2 times, most recently from 3093d53 to 758d0e8 Compare November 28, 2023 02:28
& apply review suggestions;
@matthiaskrgr
Copy link
Member

Thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Dec 12, 2023

📌 Commit 758d0e8 has been approved by matthiaskrgr

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Dec 12, 2023

⌛ Testing commit 758d0e8 with merge 2e96c74...

@bors
Copy link
Contributor

bors commented Dec 12, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: matthiaskrgr
Pushing 2e96c74 to master...

@bors bors merged commit 2e96c74 into rust-lang:master Dec 12, 2023
8 checks passed
bors added a commit that referenced this pull request Dec 23, 2023
…=xFrednet

fix typo in infinite loop lint

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: This fixes a small typo introduced in #11829
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.

Detects usage of infinite loops without indicating that function never returns
6 participants