Remove some indexing (#6728)

**Summary** A common pattern in the code used to be
```rust
if statements.len() != 1 {
    return;
}
use_single_entry(statements[0])?;
```
which can be better expressed as
```rust
let [statement] = statements else {
    return;
};
use_single_entry(statements)?;
```

Direct indexing can cause panics if you don't manually take care of
checking the length, while matching (such as if-let or let-else) can
never panic.

This isn't a complete refactor, i've just removed some of the obvious
cases. I've specifically looked for `.len() != 1` and fixed those.

**Test Plan** No functional changes
This commit is contained in:
konsti 2023-08-21 16:56:15 +02:00 committed by GitHub
parent 2405536d03
commit aafde6db28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 50 additions and 67 deletions

View file

@ -90,12 +90,11 @@ fn is_stdin(files: &[PathBuf], stdin_filename: Option<&Path>) -> bool {
return true;
}
let [file] = files else {
return false;
};
// If the user provided exactly `-`, read from standard input.
if files.len() == 1 && files[0] == Path::new("-") {
return true;
}
false
file == Path::new("-")
}
pub fn run(