Skip to content

indexing_slicing should not warn with find/starts_with #12236

@SoniEx2

Description

@SoniEx2

Summary

When using find and starts_with the warning should obviously be suppressed.

Lint Name

indexing_slicing

Reproducer

I tried this code:

fn get_fallback(target: &str) -> Result<String, FallbackError> {
  use FallbackError::*;
  // find the scheme
  let scheme = {
    let colon = target.find(':').ok_or(NotAnUrl)?;
    let scheme = &target[..colon];
    if !scheme.starts_with("web+") {
      return Err(NotAnUrl);
    }
    let scheme = &scheme[4..];
    if is_scheme_invalid(scheme) {
      return Err(NotAnUrl);
    }
    scheme
  };
  // bunch of other stuff
}

I saw this happen:

warning: slicing may panic
   --> crates/api_common/src/request.rs:374:19
    |
374 |     let scheme = &target[..colon];
    |                   ^^^^^^^^^^^^^^^
    |
    = help: consider using `.get(..n)`or `.get_mut(..n)` instead
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
    = note: requested on the command line with `-D clippy::indexing-slicing`

warning: slicing may panic
   --> crates/api_common/src/request.rs:378:19
    |
378 |     let scheme = &scheme[4..];
    |                   ^^^^^^^^^^^
    |
    = help: consider using `.get(n..)` or .get_mut(n..)` instead
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing

I expected to see this happen:

It should simply not.

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions