@@ -17,7 +17,6 @@ use ruff_text_size::{Ranged, TextRange, TextSize};
17
17
use crate :: checkers:: ast:: Checker ;
18
18
use crate :: fix:: edits;
19
19
use crate :: fix:: edits:: adjust_indentation;
20
- use crate :: preview:: is_only_add_return_none_at_end_enabled;
21
20
use crate :: registry:: Rule ;
22
21
use crate :: rules:: flake8_return:: helpers:: end_of_last_statement;
23
22
use crate :: { AlwaysFixableViolation , FixAvailability , Violation } ;
@@ -463,96 +462,70 @@ fn add_return_none(checker: &Checker, stmt: &Stmt, range: TextRange) {
463
462
}
464
463
}
465
464
466
- /// Returns a list of all implicit returns in the given statement.
467
- ///
468
- /// Note: The function should be refactored to `has_implicit_return` with an early return (when seeing the first implicit return)
469
- /// when removing the preview gating.
470
- fn implicit_returns < ' a > ( checker : & Checker , stmt : & ' a Stmt ) -> Vec < & ' a Stmt > {
465
+ fn has_implicit_return ( checker : & Checker , stmt : & Stmt ) -> bool {
471
466
match stmt {
472
467
Stmt :: If ( ast:: StmtIf {
473
468
body,
474
469
elif_else_clauses,
475
470
..
476
471
} ) => {
477
- let mut implicit_stmts = body
472
+ if body
478
473
. last ( )
479
- . map ( |last| implicit_returns ( checker, last) )
480
- . unwrap_or_default ( ) ;
481
-
482
- for clause in elif_else_clauses {
483
- implicit_stmts. extend (
484
- clause
485
- . body
486
- . last ( )
487
- . iter ( )
488
- . flat_map ( |last| implicit_returns ( checker, last) ) ,
489
- ) ;
474
+ . is_some_and ( |last| has_implicit_return ( checker, last) )
475
+ {
476
+ return true ;
477
+ }
478
+
479
+ if elif_else_clauses. iter ( ) . any ( |clause| {
480
+ clause
481
+ . body
482
+ . last ( )
483
+ . is_some_and ( |last| has_implicit_return ( checker, last) )
484
+ } ) {
485
+ return true ;
490
486
}
491
487
492
488
// Check if we don't have an else clause
493
- if matches ! (
489
+ matches ! (
494
490
elif_else_clauses. last( ) ,
495
491
None | Some ( ast:: ElifElseClause { test: Some ( _) , .. } )
496
- ) {
497
- implicit_stmts. push ( stmt) ;
498
- }
499
- implicit_stmts
492
+ )
500
493
}
501
- Stmt :: Assert ( ast:: StmtAssert { test, .. } ) if is_const_false ( test) => vec ! [ ] ,
502
- Stmt :: While ( ast:: StmtWhile { test, .. } ) if is_const_true ( test) => vec ! [ ] ,
494
+ Stmt :: Assert ( ast:: StmtAssert { test, .. } ) if is_const_false ( test) => false ,
495
+ Stmt :: While ( ast:: StmtWhile { test, .. } ) if is_const_true ( test) => false ,
503
496
Stmt :: For ( ast:: StmtFor { orelse, .. } ) | Stmt :: While ( ast:: StmtWhile { orelse, .. } ) => {
504
497
if let Some ( last_stmt) = orelse. last ( ) {
505
- implicit_returns ( checker, last_stmt)
498
+ has_implicit_return ( checker, last_stmt)
506
499
} else {
507
- vec ! [ stmt]
508
- }
509
- }
510
- Stmt :: Match ( ast:: StmtMatch { cases, .. } ) => {
511
- let mut implicit_stmts = vec ! [ ] ;
512
- for case in cases {
513
- implicit_stmts. extend (
514
- case. body
515
- . last ( )
516
- . into_iter ( )
517
- . flat_map ( |last_stmt| implicit_returns ( checker, last_stmt) ) ,
518
- ) ;
500
+ true
519
501
}
520
- implicit_stmts
521
502
}
503
+ Stmt :: Match ( ast:: StmtMatch { cases, .. } ) => cases. iter ( ) . any ( |case| {
504
+ case. body
505
+ . last ( )
506
+ . is_some_and ( |last| has_implicit_return ( checker, last) )
507
+ } ) ,
522
508
Stmt :: With ( ast:: StmtWith { body, .. } ) => body
523
509
. last ( )
524
- . map ( |last_stmt| implicit_returns ( checker, last_stmt) )
525
- . unwrap_or_default ( ) ,
526
- Stmt :: Return ( _) | Stmt :: Raise ( _) | Stmt :: Try ( _) => vec ! [ ] ,
510
+ . is_some_and ( |last_stmt| has_implicit_return ( checker, last_stmt) ) ,
511
+ Stmt :: Return ( _) | Stmt :: Raise ( _) | Stmt :: Try ( _) => false ,
527
512
Stmt :: Expr ( ast:: StmtExpr { value, .. } )
528
513
if matches ! (
529
514
value. as_ref( ) ,
530
515
Expr :: Call ( ast:: ExprCall { func, .. } )
531
516
if is_noreturn_func( func, checker. semantic( ) )
532
517
) =>
533
518
{
534
- vec ! [ ]
535
- }
536
- _ => {
537
- vec ! [ stmt]
519
+ false
538
520
}
521
+ _ => true ,
539
522
}
540
523
}
541
524
542
525
/// RET503
543
526
fn implicit_return ( checker : & Checker , function_def : & ast:: StmtFunctionDef , stmt : & Stmt ) {
544
- let implicit_stmts = implicit_returns ( checker, stmt) ;
545
-
546
- if implicit_stmts. is_empty ( ) {
547
- return ;
548
- }
549
-
550
- if is_only_add_return_none_at_end_enabled ( checker. settings ) {
527
+ if has_implicit_return ( checker, stmt) {
551
528
add_return_none ( checker, stmt, function_def. range ( ) ) ;
552
- } else {
553
- for implicit_stmt in implicit_stmts {
554
- add_return_none ( checker, implicit_stmt, implicit_stmt. range ( ) ) ;
555
- }
556
529
}
557
530
}
558
531
0 commit comments