Skip to content

Commit

Permalink
Add check for unused return value of function
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Mar 11, 2024
1 parent a1bf269 commit 658223c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/analyzer/src/analyzer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,21 @@ pub enum AnalyzerError {
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Warning),
code(unused_return),
help("add variable assignment for function return"),
url("https://doc.veryl-lang.org/book/06_appendix/02_semantic_error.html#unused_return")
)]
#[error("return value of {identifier} is unused")]
UnusedReturn {
identifier: String,
#[source_code]
input: NamedSource,
#[label("Error location")]
error_location: SourceSpan,
},
}

impl AnalyzerError {
Expand Down Expand Up @@ -744,4 +759,12 @@ impl AnalyzerError {
error_location: token.token.into(),
}
}

pub fn unused_return(identifier: &str, source: &str, token: &VerylToken) -> Self {
AnalyzerError::UnusedReturn {
identifier: identifier.to_string(),
input: AnalyzerError::named_source(source, token),
error_location: token.token.into(),
}
}
}
38 changes: 38 additions & 0 deletions crates/analyzer/src/handlers/check_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,44 @@ impl<'a> Handler for CheckFunction<'a> {
}

impl<'a> VerylGrammarTrait for CheckFunction<'a> {
fn identifier_statement(&mut self, arg: &IdentifierStatement) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
if let IdentifierStatementGroup::FunctionCall(_) = &*arg.identifier_statement_group {
// skip system function
if arg
.expression_identifier
.expression_identifier_opt
.is_some()
{
return Ok(());
}

if let Ok(symbol) = symbol_table::resolve(arg.expression_identifier.as_ref()) {
if let ResolveSymbol::Symbol(symbol) = symbol.found {
if let SymbolKind::Function(x) = symbol.kind {
if x.ret.is_some() {
let name = format!(
"{}",
SymbolPath::from(arg.expression_identifier.as_ref())
.as_slice()
.last()
.unwrap()
);
self.errors.push(AnalyzerError::unused_return(
&name,
self.text,
&arg.expression_identifier.identifier.identifier_token,
));
}
}
}
}
}
}

Ok(())
}

fn factor(&mut self, arg: &Factor) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
if let Factor::ExpressionIdentifierFactorOpt(x) = arg {
Expand Down
5 changes: 5 additions & 0 deletions crates/analyzer/src/handlers/create_symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,17 @@ impl<'a> VerylGrammarTrait for CreateSymbolTable<'a> {
}
}
}
let ret = arg
.function_declaration_opt1
.as_ref()
.map(|x| (&*x.scalar_type).into());
let range =
TokenRange::new(&arg.function.function_token, &arg.r_brace.r_brace_token);
let property = FunctionProperty {
range,
parameters,
ports,
ret,
};
self.insert_symbol(
&arg.identifier.identifier_token,
Expand Down
1 change: 1 addition & 0 deletions crates/analyzer/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ pub struct FunctionProperty {
pub range: TokenRange,
pub parameters: Vec<Parameter>,
pub ports: Vec<Port>,
pub ret: Option<Type>,
}

#[derive(Debug, Clone)]
Expand Down

0 comments on commit 658223c

Please sign in to comment.