@@ -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,11 @@ 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 `{ epxr }` if block is used as value by other
1380
+ // expressions, e.g. `return`, `match`, `break`, which may cause
1381
+ // false positive.
1373
1382
// - `followed_by_block` is true and the internal expr may contain a `{`
1374
1383
// - the block is not multiline (do not lint multiline match arms)
1375
1384
// ```
@@ -1399,6 +1408,7 @@ impl UnusedDelimLint for UnusedBraces {
1399
1408
&& value. attrs . is_empty ( )
1400
1409
&& !value. span . from_expansion ( )
1401
1410
&& !inner. span . from_expansion ( )
1411
+ && !self . block_as_value
1402
1412
{
1403
1413
self . emit_unused_delims_expr ( cx, value, ctx, left_pos, right_pos, is_kw)
1404
1414
}
@@ -1428,6 +1438,16 @@ impl EarlyLintPass for UnusedBraces {
1428
1438
1429
1439
#[ inline]
1430
1440
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
1441
+ use rustc_ast:: ast:: ExprKind :: * ;
1442
+ self . block_as_value = match e. kind {
1443
+ Ret ( Some ( ref expr) ) | Match ( ref expr, ..) | Break ( _, Some ( ref expr) )
1444
+ if matches ! ( expr. kind, Block ( ..) ) =>
1445
+ {
1446
+ true
1447
+ }
1448
+ _ => false ,
1449
+ } ;
1450
+
1431
1451
<Self as UnusedDelimLint >:: check_expr ( self , cx, e) ;
1432
1452
1433
1453
if let ExprKind :: Repeat ( _, ref anon_const) = e. kind {
0 commit comments