Skip to content

Commit

Permalink
fix(repl): do not check for unused expressions (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
pront authored Jul 15, 2024
1 parent 8126965 commit 95721a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion src/compiler/compile_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ use std::{

type AnyMap = HashMap<TypeId, Box<dyn Any>>;

#[derive(Default)]
pub struct CompileConfig {
/// Custom context injected by the external environment
custom: AnyMap,
read_only_paths: BTreeSet<ReadOnlyPath>,
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 {
Expand Down Expand Up @@ -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)]
Expand Down
20 changes: 12 additions & 8 deletions src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 95721a1

Please sign in to comment.