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

Compiler warns to remove parentheses producing invalid code #103435

Closed
fables-tales opened this issue Oct 23, 2022 · 6 comments · Fixed by #103468
Closed

Compiler warns to remove parentheses producing invalid code #103435

fables-tales opened this issue Oct 23, 2022 · 6 comments · Fixed by #103468
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.

Comments

@fables-tales
Copy link
Contributor

fables-tales commented Oct 23, 2022

I tried this code:

fn main() {
     for (idx, rt_child) in root.iter_children().enumerate() {
          if let(Some(rt_child)) = rt_child {
            let reply_node = root_child_arena.get_reply_node(rt_child);
            info!("reply_node: {} {:?} {}", Move::from_index(idx), reply_node.get_visits(), reply_node.get_score());
          }
        }
}

I expected to see no compiler warning on the if let(Some(rt_child)) = rt_child line.

Instead, this happened:


warning: unnecessary parentheses around pattern
 --> src/main.rs:3:17
  |
3 |           if let(Some(rt_child)) = rt_child {
  |                 ^              ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
3 -           if let(Some(rt_child)) = rt_child {
3 +           if letSome(rt_child) = rt_child {
  |

For more information about this error, try `rustc --explain E0425`.
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` due to 3 previous errors; 1 warning emitted

Meta

here is a rust playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9d2bfd8156a76f0a565f0caf81b52d80
rustc --version --verbose:

1.64.0
@fables-tales fables-tales added the C-bug Category: This is a bug. label Oct 23, 2022
@jruderman
Copy link
Contributor

Simpler:

fn main() {
    if let(Some(_)) = Some(1) {
    }
}

which gives the error-introducing suggestion:

help: remove these parentheses
  |
2 -     if let(Some(_)) = Some(1) {
2 +     if letSome(_) = Some(1) {

@jruderman
Copy link
Contributor

The warning is correct: it is unnecessary to put parens around the entire if let pattern. The suggestion just needs to include a space in this case, so it doesn't run the let and Some tokens together.

The suggestion should be:

help: remove these parentheses
  |
2 -     if let(Some(_)) = Some(1) {
2 +     if let Some(_) = Some(1) {

@jruderman
Copy link
Contributor

@rustbot label +A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Oct 23, 2022
@Noratrieb
Copy link
Member

@rustbot label D-invalid-suggestion

@rustbot rustbot added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Oct 23, 2022
@chenyukang
Copy link
Member

chenyukang commented Oct 24, 2022

Another case:

fn main() {
    for(_x) in 1..10 { }
}
warning: unnecessary parentheses around pattern
 --> src/main.rs:2:8
  |
2 |     for(_x) in 1..10 { }
  |        ^  ^
  |
  = note: `#[warn(unused_parens)]` on by default
help: remove these parentheses
  |
2 -     for(_x) in 1..10 { }
2 +     for_x in 1..10 { }
  |

I guess we may have some other cases for remove these parentheses

@chenyukang
Copy link
Member

@rustbot claim

Manishearth added a commit to Manishearth/rust that referenced this issue Nov 11, 2022
…-parentheses, r=estebank

Fix unused lint and parser caring about spaces to won't produce invalid code

Fixes rust-lang#103435
Manishearth added a commit to Manishearth/rust that referenced this issue Nov 11, 2022
…-parentheses, r=estebank

Fix unused lint and parser caring about spaces to won't produce invalid code

Fixes rust-lang#103435
Manishearth added a commit to Manishearth/rust that referenced this issue Nov 11, 2022
…-parentheses, r=estebank

Fix unused lint and parser caring about spaces to won't produce invalid code

Fixes rust-lang#103435
@bors bors closed this as completed in c0447b4 Nov 11, 2022
Aaron1011 pushed a commit to Aaron1011/rust that referenced this issue Jan 6, 2023
…-parentheses, r=estebank

Fix unused lint and parser caring about spaces to won't produce invalid code

Fixes rust-lang#103435
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants