-
Notifications
You must be signed in to change notification settings - Fork 13.8k
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
b7c2339
to
5fda6c1
Compare
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 same for |
@rustbot author |
Reminder, once the PR becomes ready for a review, use |
☔ The latest upstream changes (presumably #144469) made this pull request unmergeable. Please resolve the merge conflicts. |
@ChAoSUnItY any updates on this? thanks |
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. |
sure 🤔 why does using the approach for blocks using |
There seems to be other logics or conditions that makes this not working. At least that's what I've encountered in June. |
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 👍 |
ae418c4
to
2b85785
Compare
cc @Amanieu, @folkertdev, @sayantn |
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. |
This comment has been minimized.
This comment has been minimized.
Weird, I seem to be unable undo accidental stdarch changes by following rustc dev guide? |
Yeah submodules can be extremely annoying when rebasing. I generally resort to just dissolving the commit with |
2b85785
to
012f029
Compare
@rustbot ready |
There was a problem hiding this 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
// if ctx == UnusedDelimsCtx::FunctionArg { | ||
// println!("{:?}", self.followed_by_block.last().copied().unwrap_or(false)) | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? 😁
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to remove 😅
// - 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 `{` |
There was a problem hiding this comment.
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
ormatch
, and the internal expression may contain a{
is that right?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
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 @rustbot author |
Fix #141783.