Skip to content

Conversation

ChAoSUnItY
Copy link
Contributor

@ChAoSUnItY ChAoSUnItY commented Jun 3, 2025

Fix #141783.

@rustbot
Copy link
Collaborator

rustbot commented Jun 3, 2025

r? @lcnr

rustbot has assigned @lcnr.
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 S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 3, 2025
@ChAoSUnItY ChAoSUnItY changed the title Fix linting false positive when block used as val Fix linting false positive when block used as value Jun 3, 2025
@rust-log-analyzer

This comment has been minimized.

@ChAoSUnItY ChAoSUnItY force-pushed the fix/unused_braces branch from b7c2339 to 5fda6c1 Compare June 4, 2025 08:54
@lcnr
Copy link
Contributor

lcnr commented Jun 5, 2025

we also need this for unused parens:

#![allow(unused)]
#![warn(unused_parens)]
struct Foo;
fn main() {
    loop {
        if break (Foo) {}
    };
}

please add this as a test, also test if fn_call({ return }) {}. This should lint (i.e. recursing into function calls should update followed_by_block to false

same for try { { return } }. In general, anything that "wraps" its nested expr should set followed_by_block to false

@lcnr
Copy link
Contributor

lcnr commented Jul 3, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 3, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 3, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@bors
Copy link
Collaborator

bors commented Jul 28, 2025

☔ The latest upstream changes (presumably #144469) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@ChAoSUnItY any updates on this? thanks

@ChAoSUnItY
Copy link
Contributor Author

Sorry for late reply, I'm currently busy on other projects, and I am unable to find a proper solution towards the unused parens lint, but the unsued braces lint seems to be fine. Will it be better to let this Pull Request dedicates to only unused braces lint fix? So later we can open unused parens in another issue as original issue only mentioned unused braces lint error.

@lcnr
Copy link
Contributor

lcnr commented Sep 23, 2025

Will it be better to let this Pull Request dedicates to only unused braces lint fix? So later we can open unused parens in another issue as original issue only mentioned unused braces lint error.

sure 🤔 why does using the approach for blocks using followed_by_block not work for parens?

@ChAoSUnItY
Copy link
Contributor Author

There seems to be other logics or conditions that makes this not working. At least that's what I've encountered in June.

@lcnr
Copy link
Contributor

lcnr commented Sep 24, 2025

alright, would be nice if you still had the changes/a commit from back then so that I could look it at.

I think even just fixing it for blocks is good for now 👍

@rustbot
Copy link
Collaborator

rustbot commented Sep 24, 2025

stdarch is developed in its own repository. If possible, consider making this change to rust-lang/stdarch instead.

cc @Amanieu, @folkertdev, @sayantn

@rustbot
Copy link
Collaborator

rustbot commented Sep 24, 2025

This PR was rebased onto a different master 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.

@rust-log-analyzer

This comment has been minimized.

@ChAoSUnItY
Copy link
Contributor Author

Weird, I seem to be unable undo accidental stdarch changes by following rustc dev guide?

@folkertdev
Copy link
Contributor

Yeah submodules can be extremely annoying when rebasing. I generally resort to just dissolving the commit with git reset HEAD^ and then carefully using git add to add only the files I actually want to update. Then another ./x test or similar should update the stdarch repo to the correct commit again.

@ChAoSUnItY
Copy link
Contributor Author

@rustbot ready

@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 (such as code changes or more information) from the author. labels Sep 24, 2025
Copy link
Contributor

@lcnr lcnr 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 a bit weird. Why does check_unused_delims_expr still take an explicit followed_by_block argument? I feel like this arg should be redundant by now

View changes since this review

Comment on lines +1559 to +1561
// if ctx == UnusedDelimsCtx::FunctionArg {
// println!("{:?}", self.followed_by_block.last().copied().unwrap_or(false))
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

? 😁

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 forgot to remove 😅

Comment on lines +1535 to 1540
// - however, this does not lint if block is immediately followed by parent
// expression's block, e.g. `if` and `match`, which may cause false positive.
// ```
// if return { return } {} else {}
// ```
// - `followed_by_block` is true and the internal expr may contain a `{`
Copy link
Contributor

Choose a reason for hiding this comment

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

these two bullet points should be unified. separately, why is your added comment indented?

I think it's

  • the current expression is may immediately be followed by a parents expression's block, e.g. from an if or match, and the internal expression may contain a {

is that right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this should be the intended way to describe this scenario

if let [stmt] = inner.stmts.as_slice()
&& let ast::StmtKind::Expr(ref expr) = stmt.kind
&& !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
&& ((ctx == UnusedDelimsCtx::FunctionArg || ctx == UnusedDelimsCtx::MethodArg)
Copy link
Contributor

Choose a reason for hiding this comment

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

why this check for ctx?

Copy link
Contributor Author

@ChAoSUnItY ChAoSUnItY Sep 25, 2025

Choose a reason for hiding this comment

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

This is meant to check f({ return }) so it could be lint into f(return), as the callee, UnusedDelimLint::check_expr will directly enter UnusedBraces::check_unused_delims_expr, and I think there's no good and concise way to push a false into UnusedBraces::followed_by_block from UnusedDelimLint without massively refactoring the whole structural logic.

Copy link
Contributor

@lcnr lcnr Sep 25, 2025

Choose a reason for hiding this comment

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

ah, that sucks :/ in a sense the followed_by_block fn arg should be sth like

enum FollowedByBlock {
    Yes,
    Inherit,
    No,
}

and we only check the last self.followed_by_block entry if it's Inherit while the parens handler either maps Inherit to false, resultin gin false positives, or conservatively to true?

Copy link
Contributor

Choose a reason for hiding this comment

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

or hmm, wait, couldn't we have a

trait UnusedDelimLint {
    fn followed_by_block(&mut self) -> &mut Vec<bool>;
}

and also set + unset stuff in UnusedDelimLint::check_expr?

Copy link
Contributor Author

@ChAoSUnItY ChAoSUnItY Sep 25, 2025

Choose a reason for hiding this comment

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

This kind of modification makes things messy in my opinion, I have to admit that this is a good idea though.

Although I have noticed that unused_parens is working fine in this case, which is f((return)) will get lint into f(return), not sure if I missed some critical logics here?

Copy link
Contributor

Choose a reason for hiding this comment

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

function args always get checked with followed_by_block: false (see UnusedDelimLint::check_expr). This would be FollowedByBlock::No.

@lcnr
Copy link
Contributor

lcnr commented Sep 26, 2025

I would really like for you to leave this in a nicer to read state than you found it 🤔 though that probably is a more significant change. Would be very happy if you could deduplicate the checks based on fn-arg is_followed_by_block vs self.is_followed_by_block, but request a review if that ends up being very difficult.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive unused_braces
7 participants