-
Notifications
You must be signed in to change notification settings - Fork 13.3k
handle paren in macro expand for let-init-else expr #134034
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
Conversation
This seems like a far reaching change because of a buggy lint. |
If we follow this thought, we need to check the original AST for
|
This is an issue with any context that restricts braces, right? fn main() {
macro_rules! x {
() => { true }
}
if (x! {}) {} // warning: unnecessary parentheses around `if` condition
} See |
|
Perhaps the lint should look at span of the expression inside the parentheses, check what macro expansion it comes from, and suppress the warning in suspicious cases. IIRC @dtolnay was also recently working on pretty-printing / linting (un)necessary parentheses, maybe he has more suggestions. |
d4bc9e6
to
1874951
Compare
Update: It now stores the span of necessary parenthesis nodes and uses it when checking for |
@rustbot ready |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry for the long delay. I think the current version is still too much. |
@bvanjoi |
The root cause of this issue is that information gets lost during macro expansion. For example, the expression |
The logic is now straightforward: if both the parenthesized expression and its inner expression originate from the same context, we continue checking. Otherwise, we stop. |
@rustbot ready |
Thanks! |
handle paren in macro expand for let-init-else expr Fixes rust-lang#131655 This PR modifies the codegen logic of the macro expansion within `let-init-else` expression: - Before: The expression `let xxx = (mac! {}) else {}` expands to `let xxx = (expanded_ast) else {}`. - After: The same expression expands to `let xxx = expanded_ast else {}`. An alternative solution to this issue could involve handling the source code directly when encountering unused parentheses in `let-init-else` expressions. However, this approach might be more cumbersome due to the absence of the necessary data structure. r? `@petrochenkov`
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#134034 (handle paren in macro expand for let-init-else expr) - rust-lang#139186 (Refactor `diy_float`) - rust-lang#140062 (std: mention `remove_dir_all` can emit `DirectoryNotEmpty` when concurrently written into) - rust-lang#140430 (Improve test coverage of HIR pretty printing.) - rust-lang#140485 (Optimize the codegen for `Span::from_expansion`) - rust-lang#140505 (linker: Quote symbol names in .def files) - rust-lang#140521 (interpret: better error message for out-of-bounds pointer arithmetic and accesses) r? `@ghost` `@rustbot` modify labels: rollup
handle paren in macro expand for let-init-else expr Fixes rust-lang#131655 This PR modifies the codegen logic of the macro expansion within `let-init-else` expression: - Before: The expression `let xxx = (mac! {}) else {}` expands to `let xxx = (expanded_ast) else {}`. - After: The same expression expands to `let xxx = expanded_ast else {}`. An alternative solution to this issue could involve handling the source code directly when encountering unused parentheses in `let-init-else` expressions. However, this approach might be more cumbersome due to the absence of the necessary data structure. r? ``@petrochenkov``
Rollup of 11 pull requests Successful merges: - rust-lang#134034 (handle paren in macro expand for let-init-else expr) - rust-lang#138703 (chore: remove redundant words in comment) - rust-lang#139186 (Refactor `diy_float`) - rust-lang#139343 (Change signature of File::try_lock and File::try_lock_shared) - rust-lang#139780 (docs: Add example to `Iterator::take` with `by_ref`) - rust-lang#139802 (Fix some grammar errors and hyperlinks in doc for `trait Allocator`) - rust-lang#140034 (simd_select_bitmask: the 'padding' bits in the mask are just ignored) - rust-lang#140062 (std: mention `remove_dir_all` can emit `DirectoryNotEmpty` when concurrently written into) - rust-lang#140485 (Optimize the codegen for `Span::from_expansion`) - rust-lang#140505 (linker: Quote symbol names in .def files) - rust-lang#140521 (interpret: better error message for out-of-bounds pointer arithmetic and accesses) r? `@ghost` `@rustbot` modify labels: rollup
handle paren in macro expand for let-init-else expr Fixes rust-lang#131655 This PR modifies the codegen logic of the macro expansion within `let-init-else` expression: - Before: The expression `let xxx = (mac! {}) else {}` expands to `let xxx = (expanded_ast) else {}`. - After: The same expression expands to `let xxx = expanded_ast else {}`. An alternative solution to this issue could involve handling the source code directly when encountering unused parentheses in `let-init-else` expressions. However, this approach might be more cumbersome due to the absence of the necessary data structure. r? ```@petrochenkov```
Rollup of 10 pull requests Successful merges: - rust-lang#134034 (handle paren in macro expand for let-init-else expr) - rust-lang#138703 (chore: remove redundant words in comment) - rust-lang#139186 (Refactor `diy_float`) - rust-lang#139343 (Change signature of File::try_lock and File::try_lock_shared) - rust-lang#139780 (docs: Add example to `Iterator::take` with `by_ref`) - rust-lang#139802 (Fix some grammar errors and hyperlinks in doc for `trait Allocator`) - rust-lang#140034 (simd_select_bitmask: the 'padding' bits in the mask are just ignored) - rust-lang#140062 (std: mention `remove_dir_all` can emit `DirectoryNotEmpty` when concurrently written into) - rust-lang#140485 (Optimize the codegen for `Span::from_expansion`) - rust-lang#140521 (interpret: better error message for out-of-bounds pointer arithmetic and accesses) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 12 pull requests Successful merges: - rust-lang#134034 (handle paren in macro expand for let-init-else expr) - rust-lang#137474 (pretty-print: Print shebang at the top of the output) - rust-lang#138872 (rustc_target: RISC-V `Zfinx` is incompatible with `{ILP32,LP64}[FD]` ABIs) - rust-lang#139046 (Improve `Lifetime::suggestion`) - rust-lang#139206 (std: use the address of `errno` to identify threads in `unique_thread_exit`) - rust-lang#139608 (Clarify `async` block behaviour) - rust-lang#139847 (Delegate to inner `vec::IntoIter` from `env::ArgsOs`) - rust-lang#140159 (Avoid redundant WTF-8 checks in `PathBuf`) - rust-lang#140197 (Document breaking out of a named code block) - rust-lang#140389 (Remove `avx512dq` and `avx512vl` implication for `avx512fp16`) - rust-lang#140430 (Improve test coverage of HIR pretty printing.) - rust-lang#140507 (rustc_target: RISC-V: feature addition batch 3) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#134034 - bvanjoi:issue-131655, r=petrochenkov handle paren in macro expand for let-init-else expr Fixes rust-lang#131655 This PR modifies the codegen logic of the macro expansion within `let-init-else` expression: - Before: The expression `let xxx = (mac! {}) else {}` expands to `let xxx = (expanded_ast) else {}`. - After: The same expression expands to `let xxx = expanded_ast else {}`. An alternative solution to this issue could involve handling the source code directly when encountering unused parentheses in `let-init-else` expressions. However, this approach might be more cumbersome due to the absence of the necessary data structure. r? `@petrochenkov`
Fixes #131655
This PR modifies the codegen logic of the macro expansion within
let-init-else
expression:let xxx = (mac! {}) else {}
expands tolet xxx = (expanded_ast) else {}
.let xxx = expanded_ast else {}
.An alternative solution to this issue could involve handling the source code directly when encountering unused parentheses in
let-init-else
expressions. However, this approach might be more cumbersome due to the absence of the necessary data structure.r? @petrochenkov