chore(clippy): add workspace lints string_slice, await_holding_lock, let_underscore_must_use#25669
Open
pront wants to merge 2 commits into
Open
chore(clippy): add workspace lints string_slice, await_holding_lock, let_underscore_must_use#25669pront wants to merge 2 commits into
pront wants to merge 2 commits into
Conversation
…let_underscore_must_use Adds three opt-in clippy lints to [workspace.lints.clippy] so they apply across all workspace crates: - string_slice: catches &str[n..] byte-index slicing that can panic on multi-byte UTF-8 boundaries (the direct cause of the bug fixed in #25582) - await_holding_lock: catches .await while holding a MutexGuard (potential deadlock) - let_underscore_must_use: catches let _ = expr where expr has a #[must_use] type or return value All existing violations are resolved: real fixes preferred (strip_prefix, split_once, split_at, .ok()), with targeted #[expect] or #![allow] reserved for provably-safe patterns (regex/find() indices, Derivative macro false positives) and one genuine unavoidable case (kinesis partition_key truncation via floor_char_boundary). Also fixes a latent panic in aws_kinesis sink: partition_key[..256] could panic on multi-byte keys; replaced with floor_char_boundary(256).
Comment on lines
142
to
+148
| if !input.starts_with('@') || input.len() < 2 { | ||
| return Err(ParseError::Malformed( | ||
| "expected non empty '@'-prefixed sampling component", | ||
| )); | ||
| } | ||
|
|
||
| let num: f64 = input[1..].parse()?; | ||
| let num: f64 = input.strip_prefix('@').unwrap().parse()?; |
Member
There was a problem hiding this comment.
I think we can reassign input here by calling input.strip_prefix('@') instead of input.starts_with('@'). This way we avoid the unwrap call. Same applies for other usages of strip_prefix().unwrap()
Member
There was a problem hiding this comment.
For libs that are using custom lints like this one I think it's best that we use
[lints]
workspace = true
and add #![deny(clippy::unwrap_used)] in lib.rs so that workspace lints never fall out of date
Affected tomls:
- lib/codecs/Cargo.toml
- lib/dnsmsg-parser/Cargo.toml
- lib/tracing-limit/Cargo.toml
- lib/vector-core/Cargo.toml
Comment on lines
+439
to
+444
| write!(acc, "[{sd_id}").ok(); | ||
| for (key, value) in sd_params { | ||
| let esc_val = escape_sd_value(value); | ||
| let _ = write!(acc, " {key}=\"{esc_val}\""); | ||
| write!(acc, " {key}=\"{esc_val}\"").ok(); | ||
| } | ||
| let _ = write!(acc, "]"); | ||
| write!(acc, "]").ok(); |
Member
There was a problem hiding this comment.
We should probably use push_str + format here to avoid .ok()s
Suggested change
| write!(acc, "[{sd_id}").ok(); | |
| for (key, value) in sd_params { | |
| let esc_val = escape_sd_value(value); | |
| let _ = write!(acc, " {key}=\"{esc_val}\""); | |
| write!(acc, " {key}=\"{esc_val}\"").ok(); | |
| } | |
| let _ = write!(acc, "]"); | |
| write!(acc, "]").ok(); | |
| acc.push_str("[{sd_id}"); | |
| for (key, value) in sd_params { | |
| let esc_val = escape_sd_value(value); | |
| acc.push_str(&format!(" {key}=\"{esc_val}\"")); | |
| } | |
| acc.push_str("]"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
A recent bug (#25582) was caused by byte-indexing into a
&strthat could contain multi-byte UTF-8 characters. Clippy'sstring_slicelint would have caught it. This PR enables that lint — plus two other high-value lints — across the entire workspace.Changes
Adds to
[workspace.lints.clippy]:string_slice— flags&str[n..]byte-index slicing that can panic on multi-byte boundariesawait_holding_lock— flags.awaitwhile holding aMutexGuard(potential deadlock)let_underscore_must_use— flagslet _ = exprwhere the return value is#[must_use]All existing violations are resolved. Where a clean API exists (
strip_prefix,split_once,.ok()), that's used.#[expect]is reserved for cases where the index is provably on a char boundary (e.g., indices fromfind()on ASCII chars, orchar_indices()).Also incidentally fixes a latent panic in the
aws_kinesissink wherepartition_key[..256]could panic on multi-byte keys; replaced withfloor_char_boundary(256).