]> git.lizzy.rs Git - rust.git/commitdiff
Fix regression in `unused_braces` with macros
authorclubby789 <jamie@hill-daniel.co.uk>
Sun, 15 Jan 2023 05:06:33 +0000 (05:06 +0000)
committerclubby789 <jamie@hill-daniel.co.uk>
Sun, 15 Jan 2023 05:08:30 +0000 (05:08 +0000)
compiler/rustc_lint/src/unused.rs
tests/ui/lint/unused_braces.fixed
tests/ui/lint/unused_braces.rs
tests/ui/lint/unused_braces.stderr

index f2ee9ab1a19873cfac16b3e6f2c29c253a5267a5..36791915964df75565cc6d3a1670fdc01b1e7f95 100644 (file)
@@ -1095,17 +1095,21 @@ fn check_unused_delims_expr(
                 //      ```
                 // - the block has no attribute and was not created inside a macro
                 // - if the block is an `anon_const`, the inner expr must be a literal
-                //      (do not lint `struct A<const N: usize>; let _: A<{ 2 + 3 }>;`)
-                //
+                //   not created by a macro, i.e. do not lint on:
+                //      ```
+                //      struct A<const N: usize>;
+                //      let _: A<{ 2 + 3 }>;
+                //      let _: A<{produces_literal!()}>;
+                //      ```
                 // FIXME(const_generics): handle paths when #67075 is fixed.
                 if let [stmt] = inner.stmts.as_slice() {
                     if let ast::StmtKind::Expr(ref expr) = stmt.kind {
                         if !Self::is_expr_delims_necessary(expr, followed_by_block, false)
                             && (ctx != UnusedDelimsCtx::AnonConst
-                                || matches!(expr.kind, ast::ExprKind::Lit(_)))
+                                || (matches!(expr.kind, ast::ExprKind::Lit(_))
+                                    && !expr.span.from_expansion()))
                             && !cx.sess().source_map().is_multiline(value.span)
                             && value.attrs.is_empty()
-                            && !expr.span.from_expansion()
                             && !value.span.from_expansion()
                             && !inner.span.from_expansion()
                         {
index 1a88d985dd86a6438bf0b6847895a8255c24d5fa..e691fb37e6c43e51341367474a4fe12ac274edd0 100644 (file)
@@ -50,4 +50,8 @@ fn main() {
     if { return } {
 
     }
+
+    // regression test for https://github.com/rust-lang/rust/issues/106899
+    return println!("!");
+    //~^ WARN unnecessary braces
 }
index 5ca4811fc32d8342f523d5af85ea902dcf852510..0d260d2cbc93f5d82dcc96f0c1fee67871bbf273 100644 (file)
@@ -50,4 +50,8 @@ fn main() {
     if { return } {
 
     }
+
+    // regression test for https://github.com/rust-lang/rust/issues/106899
+    return { println!("!") };
+    //~^ WARN unnecessary braces
 }
index 7773f44ea2d38c0860855afe26fd1cc50f0643a8..0b4a1c321805ddea51a8b4261d5794fd89b38efc 100644 (file)
@@ -68,5 +68,17 @@ LL -     consume({ 7 });
 LL +     consume(7);
    |
 
-warning: 5 warnings emitted
+warning: unnecessary braces around `return` value
+  --> $DIR/unused_braces.rs:55:12
+   |
+LL |     return { println!("!") };
+   |            ^^             ^^
+   |
+help: remove these braces
+   |
+LL -     return { println!("!") };
+LL +     return println!("!");
+   |
+
+warning: 6 warnings emitted