Skip to content

Commit

Permalink
chore(dev): more clippy fixes (#1236)
Browse files Browse the repository at this point in the history
* deny clippy::match_same_arms,

* clippy::inefficient_to_string,

* deny clippy::match_bool

* smplify deny block

* deny clippy::default_trait_access

* deny clippy::doc_markdown,

* cargo fmt
  • Loading branch information
pront authored Jan 28, 2025
1 parent 1d7e651 commit 942b8ce
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 70 deletions.
33 changes: 15 additions & 18 deletions src/stdlib/assert.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
use crate::compiler::prelude::*;

fn assert(condition: Value, message: Option<Value>, format: Option<String>) -> Resolved {
match condition.try_boolean()? {
true => Ok(true.into()),
false => {
if let Some(message) = message {
let message = message.try_bytes_utf8_lossy()?.into_owned();
Err(ExpressionError::Error {
message: message.clone(),
labels: vec![],
notes: vec![Note::UserErrorMessage(message)],
})
} else {
let message = match format {
Some(string) => format!("assertion failed: {string}"),
None => "assertion failed".to_owned(),
};
Err(ExpressionError::from(message))
}
}
if condition.try_boolean()? {
Ok(true.into())
} else if let Some(message) = message {
let message = message.try_bytes_utf8_lossy()?.into_owned();
Err(ExpressionError::Error {
message: message.clone(),
labels: vec![],
notes: vec![Note::UserErrorMessage(message)],
})
} else {
let message = match format {
Some(string) => format!("assertion failed: {string}"),
None => "assertion failed".to_owned(),
};
Err(ExpressionError::from(message))
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/stdlib/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,13 @@ mod test {
vec!["".into(), Value::Null, "".into()], // original
CompactOptions {
string: false,
..Default::default()
..CompactOptions::default()
},
),
(
vec![1.into(), 2.into()],
vec![1.into(), Value::Array(vec![]), 2.into()],
Default::default(),
CompactOptions::default(),
),
(
vec![1.into(), Value::Array(vec![3.into()]), 2.into()],
Expand All @@ -306,7 +306,7 @@ mod test {
Value::Array(vec![Value::Null, 3.into(), Value::Null]),
2.into(),
],
Default::default(),
CompactOptions::default(),
),
(
vec![1.into(), 2.into()],
Expand All @@ -315,7 +315,7 @@ mod test {
Value::Array(vec![Value::Null, Value::Null]),
2.into(),
],
Default::default(),
CompactOptions::default(),
),
(
vec![
Expand All @@ -334,7 +334,7 @@ mod test {
])),
2.into(),
],
Default::default(),
CompactOptions::default(),
),
];

Expand All @@ -359,7 +359,7 @@ mod test {
}, // original
CompactOptions {
string: false,
..Default::default()
..CompactOptions::default()
},
),
(
Expand All @@ -372,7 +372,7 @@ mod test {
"key2" => Value::Array(vec![]),
"key3" => Value::from(2),
},
Default::default(),
CompactOptions::default(),
),
(
ObjectMap::from([
Expand All @@ -395,7 +395,7 @@ mod test {
),
(KeyString::from("key3"), Value::from(2)),
]),
Default::default(),
CompactOptions::default(),
),
(
ObjectMap::from([
Expand All @@ -416,7 +416,7 @@ mod test {
]),
CompactOptions {
recursive: false,
..Default::default()
..CompactOptions::default()
},
),
(
Expand All @@ -432,7 +432,7 @@ mod test {
),
(KeyString::from("key3"), Value::from(2)),
]),
Default::default(),
CompactOptions::default(),
),
(
btreemap! {
Expand All @@ -445,7 +445,7 @@ mod test {
"key2" => Value::Array(vec![Value::Null, 2.into(), Value::Null]),
"key3" => Value::from(2),
},
Default::default(),
CompactOptions::default(),
),
];

Expand Down
14 changes: 7 additions & 7 deletions src/stdlib/encode_percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@ fn encode_percent(value: Value, ascii_set: &Bytes) -> Resolved {
Ok(utf8_percent_encode(&string, ascii_set).to_string().into())
}

/// https://url.spec.whatwg.org/#fragment-percent-encode-set
/// <https://url.spec.whatwg.org/#fragment-percent-encode-set>
const FRAGMENT: &AsciiSet = &percent_encoding::CONTROLS
.add(b' ')
.add(b'"')
.add(b'<')
.add(b'>')
.add(b'`');

/// https://url.spec.whatwg.org/#query-percent-encode-set
/// <https://url.spec.whatwg.org/#query-percent-encode-set>
const QUERY: &AsciiSet = &percent_encoding::CONTROLS
.add(b' ')
.add(b'"')
.add(b'#')
.add(b'<')
.add(b'>');

/// https://url.spec.whatwg.org/#special-percent-encode-set
/// <https://url.spec.whatwg.org/#special-percent-encode-set>
const SPECIAL: &AsciiSet = &QUERY.add(b'\'');

/// https://url.spec.whatwg.org/#path-percent-encode-set
/// <https://url.spec.whatwg.org/#path-percent-encode-set>
const PATH: &AsciiSet = &QUERY.add(b'?').add(b'`').add(b'{').add(b'}');

/// https://url.spec.whatwg.org/#userinfo-percent-encode-set
/// <https://url.spec.whatwg.org/#userinfo-percent-encode-set>
const USERINFO: &AsciiSet = &PATH
.add(b'/')
.add(b':')
Expand All @@ -55,10 +55,10 @@ const USERINFO: &AsciiSet = &PATH
.add(b'^')
.add(b'|');

/// https://url.spec.whatwg.org/#component-percent-encode-set
/// <https://url.spec.whatwg.org/#component-percent-encode-set>
const COMPONENT: &AsciiSet = &USERINFO.add(b'$').add(b'%').add(b'&').add(b'+').add(b',');

/// https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set
/// <https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set>
const WWW_FORM_URLENCODED: &AsciiSet =
&COMPONENT.add(b'!').add(b'\'').add(b'(').add(b')').add(b'~');

Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod tests {
value!(1),
&Bytes::from("warn"),
value!("simple test message"),
Default::default(),
Span::default(),
)
.unwrap();

Expand Down
6 changes: 3 additions & 3 deletions src/stdlib/map_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ where

for item in iter.by_ref() {
let value = match item {
IterItem::KeyValue(_, value) => value,
IterItem::IndexValue(_, value) => value,
IterItem::Value(value) => value,
IterItem::KeyValue(_, value)
| IterItem::IndexValue(_, value)
| IterItem::Value(value) => value,
};

runner.map_value(ctx, value)?;
Expand Down
16 changes: 1 addition & 15 deletions src/stdlib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
#![deny(
warnings,
clippy::all,
clippy::pedantic,
unreachable_pub,
unused_allocation,
unused_extern_crates,
unused_assignments,
unused_comparisons
)]
#![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::default_trait_access, // allowed in initial deny commit
clippy::doc_markdown, // allowed in initial deny commit
clippy::inefficient_to_string, // allowed in initial deny commit
clippy::match_bool, // allowed in initial deny commit
clippy::match_same_arms, // allowed in initial deny commit
clippy::needless_pass_by_value, // allowed in initial deny commit
)]

Expand Down
7 changes: 4 additions & 3 deletions src/stdlib/parse_aws_alb_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,10 @@ fn parse_log(mut input: &str) -> ExpressionResult<Value> {
field_raw!("classification_reason", take_quoted1);
field_tid!("traceability_id");

match input.is_empty() {
true => Ok(log.into()),
false => Err(format!(r#"Log should be fully consumed: "{input}""#).into()),
if input.is_empty() {
Ok(log.into())
} else {
Err(format!(r#"Log should be fully consumed: "{input}""#).into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/parse_cef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fn parse_value(input: &str) -> IResult<&str, String, VerboseError<&str>> {
))(input)
}

/// As take take_till1 but can have condition on input instead of Input::Item.
/// As take `take_till1` but can have condition on input instead of `Input::Item`.
fn take_till1_input<'a, F: Fn(&'a str) -> bool, Error: ParseError<&'a str>>(
cond: F,
) -> impl Fn(&'a str) -> IResult<&'a str, &'a str, Error> {
Expand Down
7 changes: 3 additions & 4 deletions src/stdlib/parse_key_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,9 @@ fn escape_char(c: char, rest: &mut Peekable<Chars>) -> char {
let _ = rest.next();
'\"'
}
// ignore escape sequences not added by encode_key_value and return the backslash untouched
Some(_) => c,
// trailing escape char is a little odd... Might need to error here!
None => c,
// Some(_): ignore escape sequences not added by encode_key_value and return the backslash untouched
// None: // trailing escape char is a little odd... Might need to error here!
Some(_) | None => c,
}
} else {
c
Expand Down
4 changes: 1 addition & 3 deletions src/stdlib/parse_nginx_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ fn regex_for_format(format: &[u8]) -> &Regex {

fn time_format_for_format(format: &[u8]) -> String {
match format {
b"combined" => "%d/%b/%Y:%T %z".to_owned(),
b"ingress_upstreaminfo" => "%d/%b/%Y:%T %z".to_owned(),
b"main" => "%d/%b/%Y:%T %z".to_owned(),
b"combined" | b"ingress_upstreaminfo" | b"main" => "%d/%b/%Y:%T %z".to_owned(),
b"error" => "%Y/%m/%d %H:%M:%S".to_owned(),
_ => unreachable!(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/parse_syslog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn message_to_value(message: Message<&str>) -> Value {
for element in message.structured_data {
let mut sdata = BTreeMap::new();
for (name, value) in element.params() {
sdata.insert(name.to_string().into(), value.into());
sdata.insert((*name).into(), value.into());
}
result.insert(element.id.to_string().into(), sdata.into());
}
Expand Down
7 changes: 4 additions & 3 deletions src/stdlib/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::prelude::{Value, ValueError, VrlValueConvert};

pub(crate) fn convert_to_string(value: Value, to_lowercase: bool) -> Result<String, ValueError> {
let string = value.try_bytes_utf8_lossy()?;
Ok(match to_lowercase {
true => string.to_lowercase(),
false => string.to_string(),
Ok(if to_lowercase {
string.to_lowercase()
} else {
string.to_string()
})
}

0 comments on commit 942b8ce

Please sign in to comment.