Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Display warning if last expression of block is unused #2314

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions crates/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,11 @@ impl Statement {
Statement::Expression(expr)
}
}

// Don't wrap expressions that are not the last expression in
// a block in a Semi so that we can report errors in the type checker
// for unneeded expressions like { 1 + 2; 3 }
(_, Some(_), false) => Statement::Expression(expr),
(_, None, false) => {
emit_error(missing_semicolon);
Statement::Expression(expr)
}

(_, Some(_), true) => Statement::Semi(expr),
(_, Some(_), _) => Statement::Semi(expr),
(_, None, true) => Statement::Expression(expr),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl CrateId {
pub struct CrateName(SmolStr);

impl CrateName {
fn is_valid_name(name: &str) -> bool {
fn is_valid_name(name: &str) -> bool {
!name.is_empty() && name.chars().all(|n| !CHARACTER_BLACK_LIST.contains(&n))
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ pub enum TypeCheckError {
CallDeprecated { name: String, note: Option<String>, span: Span },
#[error("{0}")]
ResolverError(ResolverError),
#[error("Unused expression result of type {expr_type}")]
UnusedResultError { expr_type: Type, expr_span: Span },
}

impl TypeCheckError {
Expand Down Expand Up @@ -205,6 +207,13 @@ impl From<TypeCheckError> for Diagnostic {

Diagnostic::simple_warning(primary_message, secondary_message, span)
}
TypeCheckError::UnusedResultError { expr_type, expr_span } => {
Diagnostic::simple_warning(
format!("Unused expression result of type {expr_type}"),
String::new(),
expr_span,
)
}
}
}
}
24 changes: 13 additions & 11 deletions crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,21 @@ impl<'interner> TypeChecker<'interner> {
for (i, stmt) in statements.iter().enumerate() {
let expr_type = self.check_statement(stmt);

if i + 1 < statements.len() {
let id = match self.interner.statement(stmt) {
crate::hir_def::stmt::HirStatement::Expression(expr) => expr,
_ => *expr_id,
};
if let crate::hir_def::stmt::HirStatement::Semi(expr) =
self.interner.statement(stmt)
{
let inner_expr_type = self.interner.id_type(expr);
let span = self.interner.expr_span(&expr);

let span = self.interner.expr_span(&id);
self.unify(&expr_type, &Type::Unit, || TypeCheckError::TypeMismatch {
expected_typ: Type::Unit.to_string(),
expr_typ: expr_type.to_string(),
expr_span: span,
self.unify(&inner_expr_type, &Type::Unit, || {
TypeCheckError::UnusedResultError {
expr_type: inner_expr_type.clone(),
expr_span: span,
}
});
} else {
}

if i + 1 == statements.len() {
block_type = expr_type;
}
}
Expand Down