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

chore(dev): more tricky clippy fixes #1237

Merged
merged 9 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions src/stdlib/encode_gzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ fn encode_gzip(value: Value, compression_level: Option<Value>) -> Resolved {
let compression_level = match compression_level {
None => flate2::Compression::default(),
Some(value) => {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
// TODO consider removal options
let level = value.try_integer()? as u32;
if level > MAX_COMPRESSION_LEVEL {
return Err(format!("compression level must be <= {MAX_COMPRESSION_LEVEL}").into());
Expand Down
2 changes: 2 additions & 0 deletions src/stdlib/encode_zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ fn encode_zlib(value: Value, compression_level: Option<Value>) -> Resolved {
let compression_level = match compression_level {
None => flate2::Compression::default(),
Some(value) => {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
// TODO consider removal options
let level = value.try_integer()? as u32;
if level > MAX_COMPRESSION_LEVEL {
return Err(format!("compression level must be <= {MAX_COMPRESSION_LEVEL}").into());
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/encode_zstd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use nom::AsBytes;
fn encode_zstd(value: Value, compression_level: Option<Value>) -> Resolved {
let compression_level = match compression_level {
None => 0,
#[allow(clippy::cast_possible_truncation)] //TODO evaluate removal options
Some(value) => value.try_integer()? as i32,
};

Expand Down
2 changes: 2 additions & 0 deletions src/stdlib/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::compiler::prelude::*;

#[allow(clippy::cast_possible_wrap)]
fn find(value: Value, pattern: Value, from: Option<Value>) -> Resolved {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
// TODO consider removal options
let from = match from {
Some(value) => value.try_integer()?,
None => 0,
Expand Down
2 changes: 2 additions & 0 deletions src/stdlib/format_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::VecDeque;

use crate::compiler::prelude::*;

#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
fn format_int(value: Value, base: Option<Value>) -> Resolved {
let value = value.try_integer()?;
let base = match base {
Expand Down Expand Up @@ -106,6 +107,7 @@ impl FunctionExpression for FormatIntFn {
// Formats x in the provided radix
//
// Panics if radix is < 2 or > 36
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
fn format_radix(x: i64, radix: u32) -> String {
let mut result: VecDeque<char> = VecDeque::new();

Expand Down
1 change: 1 addition & 0 deletions src/stdlib/format_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn format_number(
match scale {
Some(0) => parts.truncate(1),
Some(i) => {
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
let i = i as usize;

if parts.len() == 1 {
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/get.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::compiler::prelude::*;
use crate::path::{OwnedSegment, OwnedValuePath};

#[allow(clippy::cast_possible_truncation)] // TODO consider removal options
fn get(value: Value, value_path: Value) -> Resolved {
let path = match value_path {
Value::Array(array) => {
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/match_datadog_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ impl Filter<Value> for VrlFilter {
})
}

#[allow(clippy::cast_precision_loss)] //TODO evaluate removal options
fn compare(
&self,
field: Field,
Expand Down
3 changes: 0 additions & 3 deletions src/stdlib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![deny(warnings, clippy::pedantic)]
#![allow(
deprecated,
clippy::cast_possible_truncation, // allowed in initial deny commit
clippy::cast_precision_loss, // allowed in initial deny commit
clippy::cast_sign_loss, // allowed in initial deny commit
clippy::needless_pass_by_value, // allowed in initial deny commit
)]

Expand Down
1 change: 1 addition & 0 deletions src/stdlib/parse_etld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ impl FunctionExpression for ParseEtldFn {

let plus_parts = match self.plus_parts.resolve(ctx)?.try_integer()? {
x if x < 0 => 0,
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
x => x as usize,
};

Expand Down
1 change: 1 addition & 0 deletions src/stdlib/parse_influxdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use influxdb_line_protocol::{FieldValue, ParsedLine};
use crate::compiler::prelude::*;
use crate::{btreemap, value};

#[allow(clippy::cast_precision_loss)] //TODO evaluate removal options
fn influxdb_line_to_metrics(line: ParsedLine) -> Result<Vec<ObjectMap>, ExpressionError> {
let ParsedLine {
series,
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/parse_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn parse_int(value: Value, base: Option<Value>) -> Resolved {
)
.into());
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
(base as u32, 0)
}
None => match string.chars().next() {
Expand Down
3 changes: 2 additions & 1 deletion src/stdlib/parse_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ fn parse_layer(value: &RawValue, remaining_depth: u8) -> std::result::Result<Jso

fn validate_depth(value: Value) -> ExpressionResult<u8> {
let res = value.try_integer()?;
let res = u8::try_from(res).map_err(|e| e.to_string())?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already check for [1, 128] below so this is not a breaking change.


// The lower cap is 1 because it is pointless to use anything lower,
// because 'data = parse_json!(.message, max_depth: 0)' equals to 'data = .message'.
//
// The upper cap is 128 because serde_json has the same recursion limit by default.
// https://github.com/serde-rs/json/blob/4d57ebeea8d791b8a51c229552d2d480415d00e6/json/src/de.rs#L111
if (1..=128).contains(&res) {
Ok(res as u8)
Ok(res)
} else {
Err(ExpressionError::from(format!(
"max_depth value should be greater than 0 and less than 128, got {res}"
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/random_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn get_length(value: Value) -> std::result::Result<usize, &'static str> {
if length > MAX_LENGTH {
return Err(LENGTH_TOO_LARGE_ERR);
}
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
Ok(length as usize)
}

Expand Down
1 change: 1 addition & 0 deletions src/stdlib/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn remove(path: Value, compact: Value, mut value: Value) -> Resolved {
Value::Bytes(field) => {
OwnedSegment::Field(String::from_utf8_lossy(&field).into())
}
#[allow(clippy::cast_possible_truncation)] //TODO evaluate removal options
Value::Integer(index) => OwnedSegment::Index(index as isize),
value => {
return Err(format!(
Expand Down
3 changes: 2 additions & 1 deletion src/stdlib/replace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::compiler::prelude::*;

#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
fn replace(value: Value, with_value: Value, count: Value, pattern: Value) -> Resolved {
let value = value.try_bytes_utf8_lossy()?;
let with = with_value.try_bytes_utf8_lossy()?;
Expand All @@ -20,7 +21,7 @@ fn replace(value: Value, with_value: Value, count: Value, pattern: Value) -> Res
i if i > 0 => Bytes::copy_from_slice(
regex.replacen(&value, i as usize, with.as_ref()).as_bytes(),
)
.into(),
.into(),
i if i < 0 => {
Bytes::copy_from_slice(regex.replace_all(&value, with.as_ref()).as_bytes())
.into()
Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/replace_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where
{
let haystack = value.try_bytes_utf8_lossy()?;
let count = match count.try_integer()? {
i if i > 0 => i as usize,
i if i > 0 => usize::try_from(i).map_err(|e| e.to_string())?,
i if i < 0 => 0,
// this is when i == 0
_ => return Ok(value),
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn set(path: Value, mut value: Value, data: Value) -> Resolved {
Value::Bytes(path) => {
OwnedSegment::Field(String::from_utf8_lossy(&path).into())
}
#[allow(clippy::cast_possible_truncation)] //TODO evaluate removal options
Value::Integer(index) => OwnedSegment::Index(index as isize),
value => {
return Err(format!(
Expand Down
2 changes: 2 additions & 0 deletions src/stdlib/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::ops::Range;
use crate::compiler::prelude::*;

#[allow(clippy::cast_possible_wrap)]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)] //TODO evaluate removal options
fn slice(start: i64, end: Option<i64>, value: Value) -> Resolved {
let range = |len: i64| -> ExpressionResult<Range<usize>> {
let start = match start {
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fn split(value: Value, limit: Value, pattern: Value) -> Resolved {
let string = value.try_bytes_utf8_lossy()?;
let limit = match limit.try_integer()? {
x if x < 0 => 0,
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
x => x as usize,
};
match pattern {
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/to_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(crate) fn bytes_to_float(bytes: Bytes) -> Resolved {
.map_err(|e| e.to_string().into())
}

#[allow(clippy::cast_precision_loss)] //TODO evaluate removal options
fn to_float(value: Value) -> Resolved {
use Value::{Boolean, Bytes, Float, Integer, Null, Timestamp};
match value {
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/to_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn to_int(value: Value) -> Resolved {

match value {
Integer(_) => Ok(value),
#[allow(clippy::cast_possible_truncation)] //TODO evaluate removal options
Float(v) => Ok(Integer(v.into_inner() as i64)),
Boolean(v) => Ok(Integer(i64::from(v))),
Null => Ok(0.into()),
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/truncate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::compiler::prelude::*;
fn truncate(value: Value, limit: Value, suffix: Value) -> Resolved {
let mut value = value.try_bytes_utf8_lossy()?.into_owned();
let limit = limit.try_integer()?;
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] // TODO consider removal options
let limit = if limit < 0 { 0 } else { limit as usize };
let suffix = suffix.try_bytes_utf8_lossy()?.to_string();
let pos = if let Some((pos, chr)) = value.char_indices().take(limit).last() {
Expand Down
1 change: 1 addition & 0 deletions src/stdlib/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::value::{KeyString, ObjectMap, Value};
/// Takes a function parameter so the exact rounding function (ceil, floor or round)
/// can be specified.
#[inline]
#[allow(clippy::cast_precision_loss)] //TODO evaluate removal options
pub(crate) fn round_to_precision<F>(num: f64, precision: i64, fun: F) -> f64
where
F: Fn(f64) -> f64,
Expand Down
2 changes: 2 additions & 0 deletions src/stdlib/uuid_v7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bytes::Bytes;
use chrono::{DateTime, Utc};
use uuid::{timestamp::Timestamp, NoContext};

#[allow(clippy::cast_sign_loss)]
fn uuid_v7(timestamp: Option<Value>) -> Resolved {
let utc_timestamp: DateTime<Utc> = if let Some(timestamp) = timestamp {
timestamp.try_timestamp()?
Expand All @@ -12,6 +13,7 @@ fn uuid_v7(timestamp: Option<Value>) -> Resolved {

let seconds = utc_timestamp.timestamp() as u64;
let nanoseconds = match utc_timestamp.timestamp_nanos_opt() {
#[allow(clippy::cast_possible_truncation)] //TODO evaluate removal options
Some(nanos) => nanos as u32,
None => return Err(ValueError::OutOfRange(Kind::timestamp()).into()),
};
Expand Down
2 changes: 1 addition & 1 deletion src/value/value/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'de> Deserialize<'de> for Value {
Ok(value.into())
} else {
// TODO: Address this issue by providing a lossless conversion option.
#[allow(clippy::cast_precision_loss)]
#[allow(clippy::cast_precision_loss)] //TODO evaluate removal options
let converted_value = value as f64;
let wrapped_value = NotNan::new(converted_value).map_err(|_| {
SerdeError::invalid_value(
Expand Down
Loading