Skip to content

Fix linting false positive when block used as value #141987

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 2 commits into
base: master
Choose a base branch
from

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
declare_lint_pass!(UnusedBraces => [UNUSED_BRACES]);
#[derive(Default)]
pub(crate) struct UnusedBraces {
parent_followed_by_block: Vec<bool>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
parent_followed_by_block: Vec<bool>,
followed_by_block: Vec<bool>,

and please add a short comment why this is used

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 think this should be informative enough?

// Used for tracking parent expressions that would immediately followed
// by block. Storing false here indicates that expression itself is Block
// expression. This is meant for to prevent report false positive cases where
// expressions with stronger block bind being linted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Storing false here indicates that expression itself is Block expression.

that's for the current impl, I think storing false should mean: while we may be inside of an expression which has a trailing block expression, there exists an expression between that one and the current which acts like a block, but may also be a function call or a try-block


impl UnusedBraces {
#[inline]
fn should_mark_block(e: &ast::Expr) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn should_mark_block(e: &ast::Expr) -> bool {
fn impacts_followed_by_block(e: &ast::Expr) -> bool {

@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

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. 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
4 participants