/// Validate `break` and `continue` is only used inside loops
Validate `break` and `continue` is only used inside loops
Validate ` break ` and ` continue ` is only used inside loops
pub(crate) fn validate_break_continue(stmt: &AstStatement) -> Result<(), Diagnostic> {
        match stmt.node {
            Statement::Break | Statement::Continue => {
                let kw = if let Statement::Break = stmt.node {
                    "break"
                } else {
                    "continue"
                };
                Err(Diagnostic {
                    level: Level::Error,
                    message: format!("{} cannot be used outside of loop", kw),
                    code: Some(BREAK_OR_CONTINUE_OUTSIDE_OF_LOOP_ERROR_CODE.to_owned()),
                    spans: vec![SpanLabel {
                        span: stmt.span,
                        label: None,
                        style: SpanStyle::Primary,
                    }],
                })
            }
            Statement::Def(.., ref stmt) => Statement::validate_break_continue(stmt),
            Statement::If(.., ref then_block) => Statement::validate_break_continue(then_block),
            Statement::IfElse(.., ref then_block, ref else_block) => {
                Statement::validate_break_continue(then_block)?;
                Statement::validate_break_continue(else_block)?;
                Ok(())
            }
            Statement::Statements(ref stmts) => {
                for stmt in stmts {
                    Statement::validate_break_continue(stmt)?;
                }
                Ok(())
            }
            Statement::For(..) => {
                // No need to check loop body, because `break` and `continue`
                // are valid anywhere in loop body.
                Ok(())
            }
            Statement::Return(..)
            | Statement::Expression(..)
            | Statement::Pass
            | Statement::Assign(..)
            | Statement::AugmentedAssign(..)
            | Statement::Load(..) => {
                // These statements do not contain nested statements
                Ok(())
            }
        }
    }
pub ( crate ) fn validate_break_continue ( stmt : & AstStatement ) -> Result < ( ) , Diagnostic > { match stmt . node { Statement :: Break | Statement :: Continue => { let kw = if let Statement :: Break = stmt . node { " " } else { " " } ; Err ( Diagnostic { level : Level :: Error , message : format ! ( " " kw ) , code : Some ( BREAK_OR_CONTINUE_OUTSIDE_OF_LOOP_ERROR_CODE . to_owned ( ) ) , spans : vec ! [ SpanLabel { span stmt span label None style SpanStyle Primary } ] , } ) } Statement :: Def ( .. , ref stmt ) => Statement :: validate_break_continue ( stmt ) , Statement :: If ( .. , ref then_block ) => Statement :: validate_break_continue ( then_block ) , Statement :: IfElse ( .. , ref then_block , ref else_block ) => { Statement :: validate_break_continue ( then_block ) ? ; Statement :: validate_break_continue ( else_block ) ? ; Ok ( ( ) ) } Statement :: Statements ( ref stmts ) => { for stmt in stmts { Statement :: validate_break_continue ( stmt ) ? ; } Ok ( ( ) ) } Statement :: For ( .. ) => { // No need to check loop body, because `break` and `continue` // are valid anywhere in loop body. Ok ( ( ) ) } Statement :: Return ( .. ) | Statement :: Expression ( .. ) | Statement :: Pass | Statement :: Assign ( .. ) | Statement :: AugmentedAssign ( .. ) | Statement :: Load ( .. ) => { // These statements do not contain nested statements Ok ( ( ) ) } } }
Validate `break` and `continue` is only used inside loops
Validate ` break ` and ` continue ` is only used inside loops
stmt
&AstStatement