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

Preserve brackets around if-lets and skip while-lets #131035

Merged
merged 2 commits into from
Sep 30, 2024

Conversation

dingxiangfei2009
Copy link
Contributor

@dingxiangfei2009 dingxiangfei2009 commented Sep 29, 2024

r? @jieyouxu

Tracked by #124085

Fresh out of #129466, we have discovered 9 crates that the lint did not successfully migrate because the span of if let includes the surrounding brackets (..) like the following, which surprised me a bit.

if (if let .. { .. } else { .. }) {
// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// the span somehow includes the surrounding brackets
}

There is one crate that failed the migration because some suggestion spans cross the macro expansion boundaries. Surely there is no way to patch them with match rewrite. To handle this case, we will instead require all spans to be tested for admissibility as suggestion spans.

Besides, there are 4 false negative cases discovered with desugared-while let. We don't need to lint them, because the else branch surely contains exactly one statement because the drop order is not changed whatsoever in this case.

while let Some(value) = droppy().get() {
..
}
// is desugared into
loop {
    if let Some(value) = droppy().get() {
        ..
    } else {
        break;
        // here can be nothing observable in this block
    } 
}

I believe this is the one and only false positive that I have found. I think we have finally nailed all the corner cases this time.

@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 Sep 29, 2024
@dingxiangfei2009
Copy link
Contributor Author

dingxiangfei2009 commented Sep 29, 2024

On a side note, I think the migration suggestions on unsafe attributes are broken. If we run edition migration, the unsafe and the surrounding brackets are misplaced, leading to code rejection. To reproduce the issue, check out the crate hictor @ 0.1.6 with hash ccf408f4326a858c00dd845a64a86b16f360a801.

@dingxiangfei2009
Copy link
Contributor Author

@rustbot labels +A-edition-2024

@jieyouxu
Copy link
Member

There is one crate that failed the migration because some suggestion spans cross the macro expansion boundaries. Surely there is no way to patch them with match rewrite. To handle this case, we will instead require all spans to be tested for admissibility as suggestion spans.

Yes, I believe in general we want to check all spans (incl. their subspans) for Span::from_expansion to be false (modulo cases where proc-macros produce spans that have no distinguishing syntax context) before making a suggestion.

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Thanks, this LGTM in general, I have some cases that we may want to add a test or two for (trying really hard to come up with something that might break this logic).

compiler/rustc_lint/src/if_let_rescope.rs Show resolved Hide resolved
compiler/rustc_lint/src/if_let_rescope.rs Outdated Show resolved Hide resolved
tests/ui/drop/lint-if-let-rescope.rs Show resolved Hide resolved
@jieyouxu
Copy link
Member

@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 30, 2024
@dingxiangfei2009
Copy link
Contributor Author

@rustbot ready

  • applied suggestions with additional UI test

@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 30, 2024
@dingxiangfei2009
Copy link
Contributor Author

There is one crate that failed the migration because some suggestion spans cross the macro expansion boundaries. Surely there is no way to patch them with match rewrite. To handle this case, we will instead require all spans to be tested for admissibility as suggestion spans.

Yes, I believe in general we want to check all spans (incl. their subspans) for Span::from_expansion to be false (modulo cases where proc-macros produce spans that have no distinguishing syntax context) before making a suggestion.

Now we are checking all the span. We bail out of emitting a suggestion because I think the fix will not be successful if any of them is not a part of a continuous user source span.

Copy link
Member

@jieyouxu jieyouxu left a comment

Choose a reason for hiding this comment

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

Thanks, you can r=me after PR CI is green.

@jieyouxu
Copy link
Member

@bors delegate+

@bors
Copy link
Contributor

bors commented Sep 30, 2024

✌️ @dingxiangfei2009, you can now approve this pull request!

If @jieyouxu told you to "r=me" after making some further change, please make that change, then do @bors r=@jieyouxu

@dingxiangfei2009
Copy link
Contributor Author

@bors r=@jieyouxu

Onto the merge train we go.

@bors
Copy link
Contributor

bors commented Sep 30, 2024

📌 Commit ed5443f has been approved by jieyouxu

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 30, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 30, 2024
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#131023 (Copy correct path to clipboard for modules/keywords/primitives)
 - rust-lang#131035 (Preserve brackets around if-lets and skip while-lets)
 - rust-lang#131038 (Fix `adt_const_params` leaking `{type error}` in error msg)
 - rust-lang#131053 (Improve `--print=check-cfg` documentation)
 - rust-lang#131056 (enable compiler fingerprint logs in verbose mode)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit dc1ccc5 into rust-lang:master Sep 30, 2024
6 checks passed
@rustbot rustbot added this to the 1.83.0 milestone Sep 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Sep 30, 2024
Rollup merge of rust-lang#131035 - dingxiangfei2009:tweak-if-let-rescope-lint, r=jieyouxu

Preserve brackets around if-lets and skip while-lets

r? `@jieyouxu`

Tracked by rust-lang#124085

Fresh out of rust-lang#129466, we have discovered 9 crates that the lint did not successfully migrate because the span of `if let` includes the surrounding brackets `(..)` like the following, which surprised me a bit.

```rust
if (if let .. { .. } else { .. }) {
// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// the span somehow includes the surrounding brackets
}
```

There is one crate that failed the migration because some suggestion spans cross the macro expansion boundaries. Surely there is no way to patch them with `match` rewrite. To handle this case, we will instead require all spans to be tested for admissibility as suggestion spans.

Besides, there are 4 false negative cases discovered with desugared-`while let`. We don't need to lint them, because the `else` branch surely contains exactly one statement because the drop order is not changed whatsoever in this case.

```rust
while let Some(value) = droppy().get() {
..
}
// is desugared into
loop {
    if let Some(value) = droppy().get() {
        ..
    } else {
        break;
        // here can be nothing observable in this block
    }
}
```

I believe this is the one and only false positive that I have found. I think we have finally nailed all the corner cases this time.
@dingxiangfei2009 dingxiangfei2009 deleted the tweak-if-let-rescope-lint branch September 30, 2024 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2024 Area: The 2024 edition S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

4 participants