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

Enhance needless continue to detect loop {continue;} #7477

Merged
merged 4 commits into from
Jul 26, 2021

Conversation

F3real
Copy link
Contributor

@F3real F3real commented Jul 20, 2021

Fixes #7417

changelog: Report [needless_continue] in loop { continue; } case

@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @giraffate (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 20, 2021
Comment on lines 373 to 381
if !loop_block.stmts.is_empty();
if let ast::StmtKind::Expr(ref statement)
| ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind;
if let ast::ExprKind::Continue(_) = statement.kind;
then {
span_lint_and_help(
cx,
NEEDLESS_CONTINUE,
loop_block.stmts.last().unwrap().span,
Copy link
Member

@flip1995 flip1995 Jul 23, 2021

Choose a reason for hiding this comment

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

Formatting looks off here and I don't like the unwrapping. If loop_block.stmts.last() already is Some(_) you don't have to check if it is empty first. So this could be written as:

Suggested change
if !loop_block.stmts.is_empty();
if let ast::StmtKind::Expr(ref statement)
| ast::StmtKind::Semi(ref statement) = loop_block.stmts.last().unwrap().kind;
if let ast::ExprKind::Continue(_) = statement.kind;
then {
span_lint_and_help(
cx,
NEEDLESS_CONTINUE,
loop_block.stmts.last().unwrap().span,
if let Some(last_stmt) = loop_block.stmts.last();
if let ast::StmtKind::Expr(expr) | ast::StmtKind::Semi(expr) = &last_stmt.kind;
if let ast::ExprKind::Continue(_) = expr.kind;
then {
span_lint_and_help(
cx,
NEEDLESS_CONTINUE,
last_stmt.span,

Also import StmtKind and ExprKind at the top of the file instead of full qualifying it here. nvm this should be done for the whole file, but this is something for another time and another PR.

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 agree it is definitely better without unwrap(), I wasn't sure how to avoid it.

The rust fmt just suggested to break line with pattern matching into two lines, but I also wasn't keen on the end result. Is there some other auto formatter that you use as default?

Copy link
Member

Choose a reason for hiding this comment

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

Nope, we use rustfmt. But rustfmt (currently) can't format code in macros. Since this code is in an if_chain! macro, rustfmt doesn't know how to format it. What rustfmt does though is to check if a line is too long regardless if it is in a macro or not. You then get a error message something like "this line is too long, but I don't know how to format it" from rustfmt.

@flip1995
Copy link
Member

@bors r+

Thanks for the contribution and implementing my additional suggestions!

@bors
Copy link
Collaborator

bors commented Jul 26, 2021

📌 Commit 045dbb5 has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Jul 26, 2021

⌛ Testing commit 045dbb5 with merge d6cb097...

bors added a commit that referenced this pull request Jul 26, 2021
Enhance needless continue to detect loop {continue;}

Fixes #7417

changelog: Report [`needless_continue`] in `loop { continue; }` case
@bors
Copy link
Collaborator

bors commented Jul 26, 2021

💥 Test timed out

@flip1995
Copy link
Member

@bors retry

@bors
Copy link
Collaborator

bors commented Jul 26, 2021

⌛ Testing commit 045dbb5 with merge 4b1b1b4...

bors added a commit that referenced this pull request Jul 26, 2021
Enhance needless continue to detect loop {continue;}

Fixes #7417

changelog: Report [`needless_continue`] in `loop { continue; }` case
@bors
Copy link
Collaborator

bors commented Jul 26, 2021

💥 Test timed out

@flip1995
Copy link
Member

@bors retry

@bors
Copy link
Collaborator

bors commented Jul 26, 2021

⌛ Testing commit 045dbb5 with merge 02d70f3...

@bors
Copy link
Collaborator

bors commented Jul 26, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 02d70f3 to master...

@bors bors merged commit 02d70f3 into rust-lang:master Jul 26, 2021
@F3real F3real deleted the needless_continue branch July 26, 2021 12:53
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.

Lint on loop { continue }
5 participants