Skip to content

Commit

Permalink
Merge #88
Browse files Browse the repository at this point in the history
88: Fix unused braces warnings r=taiki-e a=taiki-e

Basically due to this function:

https://github.com/taiki-e/auto_enums/blob/f77fd83a2b2c1b694cdc23cad0d301a7402c12b7/core/src/utils.rs#L55-L57

`brace_token` of the block that passed to `f` should have `call_site` span.
If `f` generates unused braces containing the span of `this.brace_token`, 
this will cause confusing warnings: rust-lang/rust#71080

Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
bors[bot] and taiki-e authored Apr 13, 2020
2 parents f77fd83 + eb5d40e commit a9f367e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ jobs:
- name: cargo doc
if: matrix.component == 'rustdoc'
env:
RUSTDOCFLAGS: -Dwarnings
# TODO: once https://github.com/rust-lang/rust/issues/70814 fixed, remove '-Aunused_braces'
RUSTDOCFLAGS: -Dwarnings -Aunused_braces
run: |
cargo doc --no-deps --all --all-features
6 changes: 5 additions & 1 deletion core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ pub(crate) fn replace_expr(this: &mut Expr, f: impl FnOnce(Expr) -> Expr) {
}

pub(crate) fn replace_block(this: &mut Block, f: impl FnOnce(Block) -> Expr) {
*this = block(vec![Stmt::Expr(f(mem::replace(this, block(Vec::new()))))]);
// `brace_token` of the block that passed to `f` should have `call_site` span.
// If `f` generates unused braces containing the span of `this.brace_token`,
// this will cause confusing warnings: https://github.com/rust-lang/rust/issues/71080
let stmts = mem::replace(&mut this.stmts, Vec::new());
this.stmts = vec![Stmt::Expr(f(block(stmts)))];
}

// =================================================================================================
Expand Down
2 changes: 2 additions & 0 deletions tests/auto_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ mod stable {

// block + unsafe block + parentheses
#[rustfmt::skip]
#[allow(unknown_lints)]
#[allow(unsafe_code)]
#[allow(unused_parens)]
#[allow(unused_braces)]
#[allow(unused_unsafe)]
#[auto_enum(Iterator)]
fn block(x: usize) -> impl Iterator<Item = i32> {
Expand Down
2 changes: 2 additions & 0 deletions tests/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ fn nested() {
}

#[rustfmt::skip]
#[allow(unknown_lints)]
#[allow(unsafe_code)]
#[allow(unused_unsafe)]
#[allow(unused_braces)]
#[auto_enum(Iterator)]
fn in_block(x: usize) -> impl Iterator<Item = i32> {
{{{ unsafe {{{ unsafe { unsafe {{
Expand Down
23 changes: 15 additions & 8 deletions tests/ui/auto_enum/nested.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ error[E0308]: `match` arms have incompatible types
error[E0308]: `if` and `else` have incompatible types
--> $DIR/nested.rs:16:1
|
16 | / #[auto_enum(Iterator)]
17 | | fn if_nop(x: usize) -> impl Iterator<Item = i32> {
18 | | if x == 0 {
19 | | 1..8
... |
23 | | 2..=10
| | ------ expected because of this
... |
16 | / #[auto_enum(Iterator)]
17 | | fn if_nop(x: usize) -> impl Iterator<Item = i32> {
18 | | if x == 0 {
19 | | 1..8
20 | | } else if x > 3 {
| |____________-
21 | || // This strange formatting is a rustfmt bug.
22 | || #[nested] //~ ERROR E0308
23 | || 2..=10
| || ------ expected because of this
24 | || } else {
25 | || (0..2).map(|x| x + 1)
26 | || }
| ||_____- `if` and `else` have incompatible types
... |
|
= note: expected type `std::ops::RangeInclusive<{integer}>`
found enum `if_nop::__Enumif_nop<_, std::iter::Map<std::ops::Range<{integer}>, [closure@$DIR/tests/ui/auto_enum/nested.rs:25:20: 25:29]>>`
Expand Down
6 changes: 0 additions & 6 deletions tests/ui/auto_enum/rejected-by-rustc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,4 @@ help: try placing this code inside a block
10| } }
|

error: attributes are not yet allowed on `if` expressions
--> $DIR/rejected-by-rustc.rs:18:9
|
18 | #[nested]
| ^^^^^^^^^

error: `#[auto_enum]` is required two or more branches or marker macros in total, there is no branch or marker macro in this statement

0 comments on commit a9f367e

Please sign in to comment.