Skip to content

unused_parens false positive on addition of blocks? #71290

Closed
@vpzomtrrfrt

Description

@vpzomtrrfrt

In this example:

pub fn foo(a: bool, b: bool) -> u8 {
    (if a { 1 } else { 0 } + if b { 1 } else { 0 })
}

rustc throws a warning:

warning: unnecessary parentheses around block return value
 --> src/lib.rs:2:5
  |
2 |     (if a { 1 } else { 0 } + if b { 1 } else { 0 })
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
  |
  = note: `#[warn(unused_parens)]` on by default

Removing the parentheses as the message directs causes errors:

error: expected expression, found `+`
 --> src/lib.rs:2:27
  |
2 |     if a { 1 } else { 0 } + if b { 1 } else { 0 }
  |                           ^ expected expression

error[E0308]: mismatched types
 --> src/lib.rs:2:12
  |
2 |     if a { 1 } else { 0 } + if b { 1 } else { 0 }
  |     -------^-------------- help: consider using a semicolon here
  |     |      |
  |     |      expected `()`, found integer
  |     expected this to be `()`

error[E0308]: mismatched types
 --> src/lib.rs:2:23
  |
2 |     if a { 1 } else { 0 } + if b { 1 } else { 0 }
  |     ------------------^--- help: consider using a semicolon here
  |     |                 |
  |     |                 expected `()`, found integer
  |     expected this to be `()`

error: aborting due to 3 previous errors

It seems the warning is wrong, unless this is expected to compile

Meta

rustc --version --verbose:

rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-unknown-linux-gnu
release: 1.42.0
LLVM version: 9.0

This also happens on nightly.

rustc 1.44.0-nightly (ce93331e2 2020-04-17)
binary: rustc
commit-hash: ce93331e2cf21ac4b72a53854b105955919114e7
commit-date: 2020-04-17
host: x86_64-unknown-linux-gnu
release: 1.44.0-nightly
LLVM version: 9.0

This issue has been assigned to @mibac138 via this comment.

Metadata

Metadata

Assignees

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions