pub fn emit_warning_local_variable(variable: &symtable::Variable) -> Option<Diagnostic> {
    match &variable.usage_type {
        VariableUsage::Parameter => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "function parameter ‘{}‘ has never been read",
                        variable.id.name
                    ),
                ));
            }
            None
        }

        VariableUsage::ReturnVariable => {
            if !variable.assigned {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "return variable ‘{}‘ has never been assigned",
                        variable.id.name
                    ),
                ));
            }
            None
        }

        VariableUsage::LocalVariable => {
            let assigned = variable.initializer.has_initializer() || variable.assigned;
            if !assigned && !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "local variable ‘{}‘ has never been read nor assigned",
                        variable.id.name
                    ),
                ));
            } else if assigned && !variable.read && !variable.is_reference() {
                // Values assigned to variables that reference others change the value of its reference
                // No warning needed in this case
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "local variable ‘{}‘ has been assigned, but never read",
                        variable.id.name
                    ),
                ));
            }
            None
        }

        VariableUsage::DestructureVariable => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "destructure variable ‘{}‘ has never been used",
                        variable.id.name
                    ),
                ));
            }

            None
        }

        VariableUsage::TryCatchReturns => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "try-catch returns variable ‘{}‘ has never been read",
                        variable.id.name
                    ),
                ));
            }

            None
        }

        VariableUsage::TryCatchErrorBytes => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "try-catch error bytes ‘{}‘ has never been used",
                        variable.id.name
                    ),
                ));
            }

            None
        }

        VariableUsage::TryCatchErrorString => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "try-catch error string ‘{}‘ has never been used",
                        variable.id.name
                    ),
                ));
            }

            None
        }
        VariableUsage::YulLocalVariable => {
            let has_value = variable.assigned || variable.initializer.has_initializer();
            if !variable.read && !has_value {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "yul variable ‘{}‘ has never been read or assigned",
                        variable.id.name
                    ),
                ));
            } else if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!("yul variable ‘{}‘ has never been read", variable.id.name),
                ));
            }
            None
        }
        VariableUsage::AnonymousReturnVariable => None,
    }
}
/// Emit different warning types according to the function variable usage
Emit different warning types according to the function variable usage
Emit different warning types according to the function variable usage
pub fn emit_warning_local_variable(variable: &symtable::Variable) -> Option<Diagnostic> {
    match &variable.usage_type {
        VariableUsage::Parameter => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "function parameter ‘{}‘ has never been read",
                        variable.id.name
                    ),
                ));
            }
            None
        }
        VariableUsage::ReturnVariable => {
            if !variable.assigned {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "return variable ‘{}‘ has never been assigned",
                        variable.id.name
                    ),
                ));
            }
            None
        }
        VariableUsage::LocalVariable => {
            let assigned = variable.initializer.has_initializer() || variable.assigned;
            if !assigned && !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "local variable ‘{}‘ has never been read nor assigned",
                        variable.id.name
                    ),
                ));
            } else if assigned && !variable.read && !variable.is_reference() {
                // Values assigned to variables that reference others change the value of its reference
                // No warning needed in this case
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "local variable ‘{}‘ has been assigned, but never read",
                        variable.id.name
                    ),
                ));
            }
            None
        }
        VariableUsage::DestructureVariable => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "destructure variable ‘{}‘ has never been used",
                        variable.id.name
                    ),
                ));
            }
            None
        }
        VariableUsage::TryCatchReturns => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "try-catch returns variable ‘{}‘ has never been read",
                        variable.id.name
                    ),
                ));
            }
            None
        }
        VariableUsage::TryCatchErrorBytes => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "try-catch error bytes ‘{}‘ has never been used",
                        variable.id.name
                    ),
                ));
            }
            None
        }
        VariableUsage::TryCatchErrorString => {
            if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "try-catch error string ‘{}‘ has never been used",
                        variable.id.name
                    ),
                ));
            }
            None
        }
        VariableUsage::YulLocalVariable => {
            let has_value = variable.assigned || variable.initializer.has_initializer();
            if !variable.read && !has_value {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!(
                        "yul variable ‘{}‘ has never been read or assigned",
                        variable.id.name
                    ),
                ));
            } else if !variable.read {
                return Some(Diagnostic::warning(
                    variable.id.loc,
                    format!("yul variable ‘{}‘ has never been read", variable.id.name),
                ));
            }
            None
        }
        VariableUsage::AnonymousReturnVariable => None,
    }
}
pub fn emit_warning_local_variable ( variable : & symtable :: Variable ) -> Option < Diagnostic > { match & variable . usage_type { VariableUsage :: Parameter => { if ! variable . read { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } None } VariableUsage :: ReturnVariable => { if ! variable . assigned { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } None } VariableUsage :: LocalVariable => { let assigned = variable . initializer . has_initializer ( ) || variable . assigned ; if ! assigned && ! variable . read { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } else if assigned && ! variable . read && ! variable . is_reference ( ) { // Values assigned to variables that reference others change the value of its reference // No warning needed in this case return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } None } VariableUsage :: DestructureVariable => { if ! variable . read { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } None } VariableUsage :: TryCatchReturns => { if ! variable . read { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } None } VariableUsage :: TryCatchErrorBytes => { if ! variable . read { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } None } VariableUsage :: TryCatchErrorString => { if ! variable . read { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } None } VariableUsage :: YulLocalVariable => { let has_value = variable . assigned || variable . initializer . has_initializer ( ) ; if ! variable . read && ! has_value { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " variable id name ) , ) ) ; } else if ! variable . read { return Some ( Diagnostic :: warning ( variable . id . loc , format ! ( " a able.id. am ), ) ) ; } None } VariableUsage :: AnonymousReturnVariable => None , } }
Emit different warning types according to the function variable usage
Emit different warning types according to the function variable usage
variable
&symtable::Variable
variable
&symtable::Variable