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

(..=0 | 2) in macro does not compile without parentheses but generates warning for unnecessary parentheses #120737

Open
astra90x opened this issue Feb 7, 2024 · 2 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-patterns Relating to patterns and pattern matching A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-unused_parens Lint: unused_parens T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@astra90x
Copy link
Contributor

astra90x commented Feb 7, 2024

I tried this code:

matches!(2, ..=0 | 2)
matches!(2, (..=0 | 2))

I expected to see this happen: At least one of the expressions should compile without warning.

Instead, this happened: The first expression does not compile. The second expression generates a warning asking you to remove the parentheses.

This happens with any macro with a pattern matcher that ends up used. Skipping the parentheses does compile if the unstable exclusive range patterns are used, so I imagine this should work as well.

   Compiling playground v0.0.1 (/playground)
error: no rules expected the token `..=`
   --> src/lib.rs:2:17
    |
2   |     matches!(2, ..=0 | 2);
    |                 ^^^ no rules expected this token in macro call
    |
note: while trying to match meta-variable `$pattern:pat`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:430:24
    |
430 |     ($expression:expr, $pattern:pat $(if $guard:expr)? $(,)?) => {
    |                        ^^^^^^^^^^^^

warning: unnecessary parentheses around pattern
 --> src/lib.rs:3:17
  |
3 |     matches!(2, (..=0 | 2));
  |                 ^        ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
3 -     matches!(2, (..=0 | 2));
3 +     matches!(2, ..=0 | 2);
  |

warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to previous error; 1 warning emitted

Meta

rustc --version --verbose:

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6
@astra90x astra90x added the C-bug Category: This is a bug. label Feb 7, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 7, 2024
@astra90x astra90x changed the title (..=0 | 2) does not compile without parentheses but generates warning for unnecessary parentheses (..=0 | 2) in macro does not compile without parentheses but generates warning for unnecessary parentheses Feb 7, 2024
@astra90x
Copy link
Contributor Author

astra90x commented Feb 7, 2024

I found the relevant missing DotDotEq here, and permitting ..= there would be the easiest thing to do. That is technically a breaking change though, so I'm not sure if we'd like to limit it to edition 2024 instead. In that case, we may also want to remove the warning for the current edition, which is an issue or patterns also have.

@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented Feb 7, 2024

@rustbot label A-macros A-patterns A-lint A-suggestion-diagnostics D-invalid-suggestion T-compiler

@rustbot rustbot added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-patterns Relating to patterns and pattern matching labels Feb 7, 2024
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 7, 2024
@jieyouxu jieyouxu added the L-unused_parens Lint: unused_parens label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-patterns Relating to patterns and pattern matching A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-unused_parens Lint: unused_parens T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants