diff options
| author | max <36980911+pr2502@users.noreply.github.com> | 2022-01-08 23:10:20 +0100 |
|---|---|---|
| committer | max <36980911+pr2502@users.noreply.github.com> | 2022-01-27 17:54:40 +0100 |
| commit | 23fd95a5e99acf35b471ca9b01230d7578bfac5f (patch) | |
| tree | 0f87767558dd911cf49f7fcc7147a552faff3bbc | |
| parent | 917890babb6b19447807b950b593312a675c2979 (diff) | |
| download | rust-23fd95a5e99acf35b471ca9b01230d7578bfac5f.tar.gz rust-23fd95a5e99acf35b471ca9b01230d7578bfac5f.zip | |
fix underflow in `check_manual_split_once` lint
| -rw-r--r-- | clippy_lints/src/methods/str_splitn.rs | 6 | ||||
| -rw-r--r-- | tests/ui/crashes/ice-8250.rs | 6 | ||||
| -rw-r--r-- | tests/ui/crashes/ice-8250.stderr | 18 |
3 files changed, 27 insertions, 3 deletions
diff --git a/clippy_lints/src/methods/str_splitn.rs b/clippy_lints/src/methods/str_splitn.rs index 70f20da1d6d..e8a3562c393 100644 --- a/clippy_lints/src/methods/str_splitn.rs +++ b/clippy_lints/src/methods/str_splitn.rs @@ -45,16 +45,16 @@ pub(super) fn check_manual_split_once( IterUsageKind::Next | IterUsageKind::Second => { let self_deref = { let adjust = cx.typeck_results().expr_adjustments(self_arg); - if adjust.is_empty() { + if adjust.len() < 2 { String::new() } else if cx.typeck_results().expr_ty(self_arg).is_box() || adjust .iter() .any(|a| matches!(a.kind, Adjust::Deref(Some(_))) || a.target.is_box()) { - format!("&{}", "*".repeat(adjust.len() - 1)) + format!("&{}", "*".repeat(adjust.len().saturating_sub(1))) } else { - "*".repeat(adjust.len() - 2) + "*".repeat(adjust.len().saturating_sub(2)) } }; if matches!(usage.kind, IterUsageKind::Next) { diff --git a/tests/ui/crashes/ice-8250.rs b/tests/ui/crashes/ice-8250.rs new file mode 100644 index 00000000000..d9a5ee1162a --- /dev/null +++ b/tests/ui/crashes/ice-8250.rs @@ -0,0 +1,6 @@ +fn _f(s: &str) -> Option<()> { + let _ = s[1..].splitn(2, '.').next()?; + Some(()) +} + +fn main() {} diff --git a/tests/ui/crashes/ice-8250.stderr b/tests/ui/crashes/ice-8250.stderr new file mode 100644 index 00000000000..04ea4456656 --- /dev/null +++ b/tests/ui/crashes/ice-8250.stderr @@ -0,0 +1,18 @@ +error: manual implementation of `split_once` + --> $DIR/ice-8250.rs:2:13 + | +LL | let _ = s[1..].splitn(2, '.').next()?; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s[1..].split_once('.').map_or(s[1..], |x| x.0)` + | + = note: `-D clippy::manual-split-once` implied by `-D warnings` + +error: unnecessary use of `splitn` + --> $DIR/ice-8250.rs:2:13 + | +LL | let _ = s[1..].splitn(2, '.').next()?; + | ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s[1..].split('.')` + | + = note: `-D clippy::needless-splitn` implied by `-D warnings` + +error: aborting due to 2 previous errors + |
