diff options
| author | Yuki Okushi <huyuumi.dev@gmail.com> | 2020-02-03 10:42:21 +0900 |
|---|---|---|
| committer | Yuki Okushi <huyuumi.dev@gmail.com> | 2020-02-03 15:19:54 +0900 |
| commit | bae129ac692f33998094eaa73edb977df66b854d (patch) | |
| tree | d44905bd112b29b04a80e5a32726b5baf633f406 | |
| parent | 6184710d085e23c222c3c60b6bab2635032d834d (diff) | |
| download | rust-bae129ac692f33998094eaa73edb977df66b854d.tar.gz rust-bae129ac692f33998094eaa73edb977df66b854d.zip | |
Use `checked_sub` to avoid index out of bounds
| -rw-r--r-- | clippy_lints/src/misc_early.rs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index 4629a53025c..6f54750cb9b 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -488,7 +488,11 @@ impl MiscEarlyLints { LitIntType::Unsuffixed => "", }; - let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1; + let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) { + val + } else { + return; // It's useless so shouldn't lint. + }; // Do not lint when literal is unsuffixed. if !suffix.is_empty() && lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' { span_lint_and_sugg( @@ -502,7 +506,7 @@ impl MiscEarlyLints { ); } - if lit_snip.starts_with("0x") { + if lit_snip.starts_with("0x") && maybe_last_sep_idx >= 3 { let mut seen = (false, false); for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() { match ch { @@ -546,7 +550,11 @@ impl MiscEarlyLints { } } else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind { let suffix = float_ty.name_str(); - let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1; + let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) { + val + } else { + return; // It's useless so shouldn't lint. + }; if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' { span_lint_and_sugg( cx, |
