From 95721a1689df75270a584e8425481ef7046b9624 Mon Sep 17 00:00:00 2001 From: Pavlos Rontidis Date: Mon, 15 Jul 2024 12:49:57 -0400 Subject: [PATCH] fix(repl): do not check for unused expressions (#940) --- src/cli/repl.rs | 1 + src/compiler/compile_config.rs | 21 ++++++++++++++++++++- src/compiler/mod.rs | 20 ++++++++++++-------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/cli/repl.rs b/src/cli/repl.rs index ed38790f44..f953c14d60 100644 --- a/src/cli/repl.rs +++ b/src/cli/repl.rs @@ -168,6 +168,7 @@ fn resolve( let mut config = CompileConfig::default(); // The CLI should be moved out of the "vrl" module, and then it can use the `vector-core::compile_vrl` function which includes this automatically config.set_read_only_path(owned_metadata_path!("vector"), true); + config.disable_unused_expression_check(); let program = match compile_with_state(program, stdlib_functions, state, config) { Ok(result) => result.program, diff --git a/src/compiler/compile_config.rs b/src/compiler/compile_config.rs index a41ee124e6..c81407ad06 100644 --- a/src/compiler/compile_config.rs +++ b/src/compiler/compile_config.rs @@ -6,11 +6,21 @@ use std::{ type AnyMap = HashMap>; -#[derive(Default)] pub struct CompileConfig { /// Custom context injected by the external environment custom: AnyMap, read_only_paths: BTreeSet, + check_unused_expressions: bool, +} + +impl Default for CompileConfig { + fn default() -> Self { + CompileConfig { + custom: AnyMap::default(), + read_only_paths: BTreeSet::default(), + check_unused_expressions: true, + } + } } impl CompileConfig { @@ -70,6 +80,15 @@ impl CompileConfig { self.read_only_paths .insert(ReadOnlyPath { path, recursive }); } + + #[must_use] + pub fn unused_expression_check_enabled(&self) -> bool { + self.check_unused_expressions + } + + pub fn disable_unused_expression_check(&mut self) { + self.check_unused_expressions = false; + } } #[derive(Debug, Clone, Ord, Eq, PartialEq, PartialOrd)] diff --git a/src/compiler/mod.rs b/src/compiler/mod.rs index 93528afc15..c39146cd5e 100644 --- a/src/compiler/mod.rs +++ b/src/compiler/mod.rs @@ -109,16 +109,20 @@ pub fn compile_with_state( let ast = parse(source) .map_err(|err| crate::diagnostic::DiagnosticList::from(vec![Box::new(err) as Box<_>]))?; + let unused_expression_check_enabled = config.unused_expression_check_enabled(); let result = Compiler::compile(fns, ast.clone(), state, config); - let unused_warnings = check_for_unused_results(&ast); - if unused_warnings.is_empty() { - result - } else { - result.map(|mut compilation_result| { - compilation_result.warnings.extend(unused_warnings); - compilation_result - }) + + if unused_expression_check_enabled { + let unused_warnings = check_for_unused_results(&ast); + if !unused_warnings.is_empty() { + return result.map(|mut compilation_result| { + compilation_result.warnings.extend(unused_warnings); + compilation_result + }); + } } + + result } /// Available VRL runtimes.