rust-lang / rust-lang/rust-clippy

For long logical lines, the suggested change is truncted before the actual changed part

Open
#8,294 3 comments 0 reactions 1 assignee View on GitHub

@xFrednet is already working on this.

Since Mar 14, 2022.

Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

When displaying a suggested change at the end of a long logical line of code which has been wrapped over several physical lines, the actual suggested change can get left off the output.

This is confusing because it looks like the part of the line which is good is the part which Clippy is suggesting should be changed, but the change is the exact same values as are in your code.

I wasn't sure if this is serious enough to count as a bug, so I've posted it as a blank issue.

I tried this code:

pub struct Stub {}

impl Stub {
    fn idx_change(&self, pos: usize) -> Result<usize, ()> {
        pos.checked_sub(2).ok_or(())
    }

    pub fn idx_map(&self, pos: usize) -> usize {
        let idx: usize = self
            .idx_change(pos)
            .ok()
            .and_then(|idx| {
                let offset = Some(1);
                offset.map(|off| idx.saturating_sub(off))
            })
            .unwrap_or_else(|| 0);

        self.idx_change(idx).ok().unwrap_or_default()
    }
}

I expected to see this happen:

help: use `unwrap_or` instead
   |
9  ~         let idx: usize = self
10 +             .idx_change(pos)
11 +             .ok()
12 +             .and_then(|idx| {
 ...
15 +             })
16 +             .unwrap_or(0);

warning: `clippy_bug_truncate` (lib) generated 1 warning

Instead, this happened:

help: use `unwrap_or` instead
   |
9  ~         let idx: usize = self
10 +             .idx_change(pos)
11 +             .ok()
12 +             .and_then(|idx| {
13 +                 let offset = Some(1);
14 +                 offset.map(|off| idx.saturating_sub(off))
 ...

warning: `clippy_bug_truncate` (lib) generated 1 warning

The actual warning part correctly removes irrelevant intermediate lines rather than the important changed lines.

warning: unnecessary closure used to substitute value for `Option::None`
  --> src/lib.rs:9:26
   |
9  |           let idx: usize = self
   |  __________________________^
10 | |             .idx_change(pos)
11 | |             .ok()
12 | |             .and_then(|idx| {
...  |
15 | |             })
16 | |             .unwrap_or_else(|| 0);
   | |_________________________________^
   |

Only the suggested change part has the problem.

Version
rustc 1.58.0 (02072b482 2022-01-11)
binary: rustc
commit-hash: 02072b482a8b5357f7fb5e5637444ae30e423c40
commit-date: 2022-01-11
host: x86_64-unknown-linux-gnu
release: 1.58.0
LLVM version: 13.0.0
Additional Labels

No response

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.