From d14bcb897f89effe10f6c5f7b51cef1025ccaf48 Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Wed, 27 Nov 2024 14:33:40 -0500 Subject: [PATCH] fix(diagnostics): add 'set_semantic_meaning' to the side effects list (#1148) * fix(diagnostics): add 'set_semantic_meaning' to the side effects list * changelog --- changelog.d/1148.fix.md | 1 + src/compiler/unused_expression_checker.rs | 31 +++++++++++------------ 2 files changed, 16 insertions(+), 16 deletions(-) create mode 100644 changelog.d/1148.fix.md diff --git a/changelog.d/1148.fix.md b/changelog.d/1148.fix.md new file mode 100644 index 0000000000..000ec08247 --- /dev/null +++ b/changelog.d/1148.fix.md @@ -0,0 +1 @@ +Removed false warning when using `set_semantic_meaning`. diff --git a/src/compiler/unused_expression_checker.rs b/src/compiler/unused_expression_checker.rs index e0c070f4d3..01157b2052 100644 --- a/src/compiler/unused_expression_checker.rs +++ b/src/compiler/unused_expression_checker.rs @@ -30,6 +30,9 @@ use crate::parser::{Literal, Program, Span}; use std::collections::{BTreeMap, HashMap}; use tracing::warn; +const SIDE_EFFECT_FUNCTIONS: [&str; 5] = + ["del", "log", "assert", "assert_eq", "set_semantic_meaning"]; + #[must_use] pub fn check_for_unused_results(ast: &Program) -> DiagnosticList { let expression_visitor = AstVisitor { ast }; @@ -351,23 +354,19 @@ impl AstVisitor<'_> { state.mark_level_as_expecting_result(); } - match function_call.ident.0.as_str() { - // All bets are off for functions with side-effects. - "del" | "log" | "assert" | "assert_eq" => (), - _ => { - if let Some(closure) = &function_call.closure { - for variable in &closure.variables { - state.mark_identifier_pending_usage(&variable.node, &variable.span); - } - state.mark_level_as_expecting_result(); - self.visit_block(&closure.block, state); - state.mark_level_as_not_expecting_result(); - } else if state.is_unused() { - state.append_diagnostic( - format!("unused result for function call `{function_call}`"), - span, - ); + if !SIDE_EFFECT_FUNCTIONS.contains(&function_call.ident.0.as_str()) { + if let Some(closure) = &function_call.closure { + for variable in &closure.variables { + state.mark_identifier_pending_usage(&variable.node, &variable.span); } + state.mark_level_as_expecting_result(); + self.visit_block(&closure.block, state); + state.mark_level_as_not_expecting_result(); + } else if state.is_unused() { + state.append_diagnostic( + format!("unused result for function call `{function_call}`"), + span, + ); } }