rust-lang / rust-lang/rust-clippy

FP skip_while_next: requires different mutabiltiy

Open
#5,252 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

        fn case_ignoreable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
            use core::unicode::derived_property::{Case_Ignorable, Cased};
            match iter.skip_while(|&c| Case_Ignorable(c)).next() {
                Some(c) => Cased(c),
                None => false,
            }
        }

This code causes clippy to warn:

warning: called `skip_while(p).next()` on an `Iterator`
   --> src/liballoc/str.rs:398:19
    |
398 |             match iter.skip_while(|&c| Case_Ignorable(c)).next() {
    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[warn(clippy::skip_while_next)]` on by default
    = help: this is more succinctly expressed by calling `.find(!p)` instead
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#skip_while_next

However, using iter.find(|&c| !Case_Ignorable(c)) does not work because it requires different mutability and a change in function signature:

        fn case_ignoreable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
            use core::unicode::derived_property::{Case_Ignorable, Cased};
            match iter.find(|&c| !Case_Ignorable(c)) {
                Some(c) => Cased(c),
                None => false,
            }
        }
    Checking alloc v0.0.0 (/home/matthias/vcs/github/rust/src/liballoc)
error[E0596]: cannot borrow `iter` as mutable, as it is not declared as mutable
   --> src/liballoc/str.rs:398:19
    |
396 |         fn case_ignoreable_then_cased<I: Iterator<Item = char>>(iter: I) -> bool {
    |                                                                 ---- help: consider changing this to be mutable: `mut iter`
397 |             use core::unicode::derived_property::{Case_Ignorable, Cased};
398 |             match iter.find(|&c| !Case_Ignorable(c)) {
    |                   ^^^^ cannot borrow as mutable

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example in src/liballoc/str.rs and reproduce the skip_while_next warning using the shown function signatures. Read the skip_while_next lint implementation and its tests, if present, to understand how the suggested find call handles iterator mutability. Done means the lint no longer gives an unusable suggestion for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.