@@ -11,8 +11,8 @@ use std::collections::{HashMap, HashSet};
1111
1212use crate :: lexer:: Span ;
1313use crate :: parser:: {
14- Block , Expr , ExprKind , FnBody , Item , ItemKind , Pattern , PatternKind , SourceFile ,
15- Stmt , StmtKind , Type as AstType , TypeKind as AstTypeKind , UnaryOp ,
14+ Block , Expr , ExprKind , FnBody , Item , ItemKind , Pattern , PatternKind , SourceFile , Stmt ,
15+ StmtKind , Type as AstType , TypeKind as AstTypeKind , UnaryOp ,
1616} ;
1717
1818/// Borrow checking error.
@@ -68,18 +68,10 @@ impl std::fmt::Display for BorrowError {
6868 write ! ( f, "cannot store reference in collection" )
6969 }
7070 BorrowErrorKind :: ReturnLocalReference { name } => {
71- write ! (
72- f,
73- "cannot return reference to local variable `{}`" ,
74- name
75- )
71+ write ! ( f, "cannot return reference to local variable `{}`" , name)
7672 }
7773 BorrowErrorKind :: BorrowWhileMutBorrow { name } => {
78- write ! (
79- f,
80- "cannot borrow `{}` while mutable borrow is active" ,
81- name
82- )
74+ write ! ( f, "cannot borrow `{}` while mutable borrow is active" , name)
8375 }
8476 BorrowErrorKind :: MoveWhileBorrowed { name } => {
8577 write ! ( f, "cannot move `{}` while borrowed" , name)
@@ -316,7 +308,8 @@ impl BorrowChecker {
316308
317309 // For the last statement, if it's an expression and the function
318310 // returns a reference, check it as a return value
319- if is_last && self . returns_ref
311+ if is_last
312+ && self . returns_ref
320313 && let StmtKind :: Expr ( expr) = & stmt. kind
321314 {
322315 self . check_return_ref ( expr, fn_span) ;
@@ -497,8 +490,7 @@ impl BorrowChecker {
497490 crate :: parser:: ElseBranch :: Expr ( e) => self . check_expr ( e) ,
498491 crate :: parser:: ElseBranch :: Block ( b) => self . check_block ( b) ,
499492 crate :: parser:: ElseBranch :: ElseIf ( elif) => {
500- let elif_expr = Expr :: new ( ExprKind :: If ( elif. clone ( ) ) , elif. span ,
501- ) ;
493+ let elif_expr = Expr :: new ( ExprKind :: If ( elif. clone ( ) ) , elif. span ) ;
502494 self . check_expr ( & elif_expr) ;
503495 }
504496 }
@@ -550,7 +542,11 @@ impl BorrowChecker {
550542 ExprKind :: Closure ( closure) => {
551543 self . push_scope ( ) ;
552544 for param in & closure. params {
553- let is_ref = param. ty . as_ref ( ) . map ( |t| self . is_ref_type ( t) ) . unwrap_or ( false ) ;
545+ let is_ref = param
546+ . ty
547+ . as_ref ( )
548+ . map ( |t| self . is_ref_type ( t) )
549+ . unwrap_or ( false ) ;
554550 self . vars . insert (
555551 param. name . name . clone ( ) ,
556552 VarInfo {
@@ -667,30 +663,32 @@ impl BorrowChecker {
667663 // Must be a reference parameter
668664 if !self . ref_params . contains ( & ident. name )
669665 && let Some ( info) = self . vars . get ( & ident. name )
670- && !info. is_ref_param {
671- self . errors . push (
672- BorrowError :: new (
673- BorrowErrorKind :: ReturnLocalReference {
674- name : ident. name . clone ( ) ,
675- } ,
676- error_span,
677- )
678- . with_help ( "return value must be derived from a reference parameter" ) ,
679- ) ;
680- }
666+ && !info. is_ref_param
667+ {
668+ self . errors . push (
669+ BorrowError :: new (
670+ BorrowErrorKind :: ReturnLocalReference {
671+ name : ident. name . clone ( ) ,
672+ } ,
673+ error_span,
674+ )
675+ . with_help ( "return value must be derived from a reference parameter" ) ,
676+ ) ;
677+ }
681678 }
682679 ExprKind :: Unary ( UnaryOp :: Ref | UnaryOp :: RefMut , inner) => {
683680 // Check that inner is derived from ref param
684681 if let Some ( name) = self . get_root_name ( inner)
685- && !self . ref_params . contains ( & name) {
686- self . errors . push (
687- BorrowError :: new (
688- BorrowErrorKind :: ReturnLocalReference { name } ,
689- error_span,
690- )
691- . with_help ( "cannot return reference to local variable" ) ,
692- ) ;
693- }
682+ && !self . ref_params . contains ( & name)
683+ {
684+ self . errors . push (
685+ BorrowError :: new (
686+ BorrowErrorKind :: ReturnLocalReference { name } ,
687+ error_span,
688+ )
689+ . with_help ( "cannot return reference to local variable" ) ,
690+ ) ;
691+ }
694692 }
695693 ExprKind :: Field ( base, _) | ExprKind :: TupleField ( base, _) => {
696694 // Field of a borrowed value is OK if base is from ref param
@@ -705,33 +703,35 @@ impl BorrowChecker {
705703 ExprKind :: Block ( block) => {
706704 // Check last expression in block
707705 if let Some ( last) = block. stmts . last ( )
708- && let StmtKind :: Expr ( e) = & last. kind {
709- self . check_return_ref ( e, error_span) ;
710- }
706+ && let StmtKind :: Expr ( e) = & last. kind
707+ {
708+ self . check_return_ref ( e, error_span) ;
709+ }
711710 }
712711 ExprKind :: If ( if_expr) => {
713712 // Both branches must return valid refs
714713 match & if_expr. then_branch {
715714 crate :: parser:: IfBranch :: Expr ( e) => self . check_return_ref ( e, error_span) ,
716715 crate :: parser:: IfBranch :: Block ( b) => {
717716 if let Some ( last) = b. stmts . last ( )
718- && let StmtKind :: Expr ( e) = & last. kind {
719- self . check_return_ref ( e, error_span) ;
720- }
717+ && let StmtKind :: Expr ( e) = & last. kind
718+ {
719+ self . check_return_ref ( e, error_span) ;
720+ }
721721 }
722722 }
723723 if let Some ( else_branch) = & if_expr. else_branch {
724724 match else_branch {
725725 crate :: parser:: ElseBranch :: Expr ( e) => self . check_return_ref ( e, error_span) ,
726726 crate :: parser:: ElseBranch :: Block ( b) => {
727727 if let Some ( last) = b. stmts . last ( )
728- && let StmtKind :: Expr ( e) = & last. kind {
729- self . check_return_ref ( e, error_span) ;
730- }
728+ && let StmtKind :: Expr ( e) = & last. kind
729+ {
730+ self . check_return_ref ( e, error_span) ;
731+ }
731732 }
732733 crate :: parser:: ElseBranch :: ElseIf ( elif) => {
733- let elif_expr = Expr :: new ( ExprKind :: If ( elif. clone ( ) ) , elif. span ,
734- ) ;
734+ let elif_expr = Expr :: new ( ExprKind :: If ( elif. clone ( ) ) , elif. span ) ;
735735 self . check_return_ref ( & elif_expr, error_span) ;
736736 }
737737 }
0 commit comments