Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

false positive in simple example of io::stdin::lines() #9135

Closed
richlowe opened this issue Jul 8, 2022 · 1 comment · Fixed by #9140
Closed

false positive in simple example of io::stdin::lines() #9135

richlowe opened this issue Jul 8, 2022 · 1 comment · Fixed by #9140
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@richlowe
Copy link

richlowe commented Jul 8, 2022

Summary

Taking the basic example from the io::stdin::lines() documentation, I get clippy warnings about significant drops. I'm not sure if I understand certainly that it's a false positive, but it seems likely since I took the example from the rust documentation here: https://doc.rust-lang.org/std/io/struct.Stdin.html#method.lines

Sorry If I've mis-filed this, or just become overly confused.

Lint Name

clippy::significant_drop_in_scrutinee

Reproducer

I tried this code:

fn main() {
    let lines = std::io::stdin().lines();
    for line in lines {
        println!("foo: {}", line.unwrap());
    }
}

I saw this happen:

   Checking playground v0.0.1 (/playground)
warning: temporary with significant `Drop` in `for` loop condition will live until the end of the `for` expression
 --> src/main.rs:3:17
  |
3 |     for line in lines {
  |                 ^^^^^ another value with significant `Drop` created here
4 |         println!("foo: {}", line.unwrap());
5 |     }
  |      - temporary lives until here
  |
  = note: `#[warn(clippy::significant_drop_in_scrutinee)]` on by default
  = note: this might lead to deadlocks or other unexpected behavior
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_in_scrutinee

warning: temporary with significant `Drop` in `for` loop condition will live until the end of the `for` expression
 --> src/main.rs:3:17
  |
3 |     for line in lines {
  |                 ^^^^^- temporary lives until here
  |
  = note: this might lead to deadlocks or other unexpected behavior
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_in_scrutinee

warning: `playground` (bin "playground") generated 2 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.41s

I expected to see this happen:
No clippy warning, or a clippy warning that makes more sense in context.

This is also available on the playground, here: https://play.rust-lang.org/?version=beta&mode=debug&edition=2021&gist=52eb4bd0d9fc3e2cace0cd54cd18da5c

It requires the beta clippy.

Version

rustc 1.63.0-beta.4 (94811fdc2 2022-07-06)
binary: rustc
commit-hash: 94811fdc269d23dd9f1dff88f165bc2d6f5f85d4
commit-date: 2022-07-06
host: x86_64-unknown-illumos
release: 1.63.0-beta.4
LLVM version: 14.0.5

Additional Labels

No response

@richlowe richlowe added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 8, 2022
@Jarcho
Copy link
Contributor

Jarcho commented Jul 8, 2022

The for loop in that example is lowered to (roughly):

match lines {
    lines => loop {
        match lines.next() {
            Some(line) => { /* body */ }
            None => break,
        }
    }
};

It probably shouldn't warn as the drop order is exactly as one would expect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants