@@ -1340,7 +1340,12 @@ declare_lint! {
1340
1340
"unnecessary braces around an expression"
1341
1341
}
1342
1342
1343
- declare_lint_pass ! ( UnusedBraces => [ UNUSED_BRACES ] ) ;
1343
+ #[ derive( Default ) ]
1344
+ pub ( crate ) struct UnusedBraces {
1345
+ block_as_value : bool ,
1346
+ }
1347
+
1348
+ impl_lint_pass ! ( UnusedBraces => [ UNUSED_BRACES ] ) ;
1344
1349
1345
1350
impl UnusedDelimLint for UnusedBraces {
1346
1351
const DELIM_STR : & ' static str = "braces" ;
@@ -1369,7 +1374,10 @@ impl UnusedDelimLint for UnusedBraces {
1369
1374
//
1370
1375
// - the block does not have a label
1371
1376
// - the block is not `unsafe`
1372
- // - the block contains exactly one expression (do not lint `{ expr; }`)
1377
+ // - the block contains exactly one expression
1378
+ // - do not lint `{ expr; }`
1379
+ // - do not lint `{ return }` if block is used as value by other
1380
+ // expressions, e.g. `return` and `match`, which may cause false positive.
1373
1381
// - `followed_by_block` is true and the internal expr may contain a `{`
1374
1382
// - the block is not multiline (do not lint multiline match arms)
1375
1383
// ```
@@ -1399,6 +1407,7 @@ impl UnusedDelimLint for UnusedBraces {
1399
1407
&& value. attrs . is_empty ( )
1400
1408
&& !value. span . from_expansion ( )
1401
1409
&& !inner. span . from_expansion ( )
1410
+ && !( self . block_as_value && matches ! ( expr. kind, ast:: ExprKind :: Ret ( _) ) )
1402
1411
{
1403
1412
self . emit_unused_delims_expr ( cx, value, ctx, left_pos, right_pos, is_kw)
1404
1413
}
@@ -1428,6 +1437,12 @@ impl EarlyLintPass for UnusedBraces {
1428
1437
1429
1438
#[ inline]
1430
1439
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
1440
+ use rustc_ast:: ast:: ExprKind :: * ;
1441
+ self . block_as_value = match e. kind {
1442
+ Ret ( Some ( ref expr) ) | Match ( ref expr, ..) if matches ! ( expr. kind, Block ( ..) ) => true ,
1443
+ _ => false ,
1444
+ } ;
1445
+
1431
1446
<Self as UnusedDelimLint >:: check_expr ( self , cx, e) ;
1432
1447
1433
1448
if let ExprKind :: Repeat ( _, ref anon_const) = e. kind {
0 commit comments