diff --git a/src/ast/array_accessor_expression.cc b/src/ast/array_accessor_expression.cc index 239748462e..87e0ea2ec7 100644 --- a/src/ast/array_accessor_expression.cc +++ b/src/ast/array_accessor_expression.cc @@ -35,7 +35,12 @@ ArrayAccessorExpression::ArrayAccessorExpression( ArrayAccessorExpression::~ArrayAccessorExpression() = default; bool ArrayAccessorExpression::IsValid() const { - return array_ != nullptr && idx_expr_ != nullptr; + if (array_ == nullptr || !array_->IsValid()) + return false; + if (idx_expr_ == nullptr || !idx_expr_->IsValid()) + return false; + + return true; } void ArrayAccessorExpression::to_str(std::ostream& out, size_t indent) const { diff --git a/src/ast/array_accessor_expression_test.cc b/src/ast/array_accessor_expression_test.cc index c3c643bd51..51b41e36cb 100644 --- a/src/ast/array_accessor_expression_test.cc +++ b/src/ast/array_accessor_expression_test.cc @@ -72,6 +72,20 @@ TEST_F(ArrayAccessorExpressionTest, IsValid_MissingIndex) { EXPECT_FALSE(exp.IsValid()); } +TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidArray) { + auto ary = std::make_unique(""); + auto idx = std::make_unique("idx"); + ArrayAccessorExpression exp(std::move(ary), std::move(idx)); + EXPECT_FALSE(exp.IsValid()); +} + +TEST_F(ArrayAccessorExpressionTest, IsValid_InvalidIndex) { + auto ary = std::make_unique("ary"); + auto idx = std::make_unique(""); + ArrayAccessorExpression exp(std::move(ary), std::move(idx)); + EXPECT_FALSE(exp.IsValid()); +} + TEST_F(ArrayAccessorExpressionTest, ToStr) { auto ary = std::make_unique("ary"); auto idx = std::make_unique("idx"); diff --git a/src/ast/else_statement.cc b/src/ast/else_statement.cc index 09f39e2448..1ad7a029eb 100644 --- a/src/ast/else_statement.cc +++ b/src/ast/else_statement.cc @@ -41,9 +41,7 @@ ElseStatement::~ElseStatement() = default; bool ElseStatement::IsValid() const { for (const auto& stmt : body_) { - if (stmt == nullptr) - return false; - if (!stmt->IsValid()) + if (stmt == nullptr || !stmt->IsValid()) return false; } if (condition_)