Skip to content

significant-drop-tightening suggests syntax error stdin.take(40).; #15574

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::significant-drop-tightening

this code:

use std::{io::{stdin, BufRead, Read}, process};
fn main() {
    //Let's read from stdin
    println!("Hello, what's your name?");
    let stdin = stdin().lock();
    let mut buffer = String::with_capacity(10);
    //Here we lock stdin and block to 10 bytes
    // Our string is now then only 10 bytes.
    //Even if it overflows like expected, it will reallocate.
    let mut stdin = stdin.take(40);
    if stdin.read_line(&mut buffer).is_err() {
        eprintln!("An error has occured while reading.");
        return;
    } //Now we print the result, our data is safe
    println!("Our string has a capacity of {}",buffer.capacity());
    println!("Hello {}!",buffer);
    //The string is freed automatically.
}
//Now let's try to overflow!! The string was reallocated.

caused the following diagnostics:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.W9mV5T7suTWy/icemaker_clippyfix_tempdir.Z8u9dYrFB4Kw/_snippet_0)
warning: temporary with significant `Drop` can be early dropped
  --> src/main.rs:5:9
   |
 2 |   fn main() {
   |  ___________-
 3 | |     //Let's read from stdin
 4 | |     println!("Hello, what's your name?");
 5 | |     let stdin = stdin().lock();
   | |         ^^^^^
...  |
18 | | }
   | |_- temporary `stdin` is currently being dropped at the end of its contained scope
   |
   = note: this might lead to unnecessary resource contention
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening
   = note: requested on the command line with `--force-warn clippy::significant-drop-tightening`
help: merge the temporary construction with its single usage
   |
 5 ~     
 6 +     let stdin = stdin().lock().take(40);
 7 |     let mut buffer = String::with_capacity(10);
...
10 |     //Even if it overflows like expected, it will reallocate.
11 ~     
   |

warning: temporary with significant `Drop` can be early dropped
  --> src/main.rs:10:13
   |
 2 |   fn main() {
   |  ___________-
 3 | |     //Let's read from stdin
 4 | |     println!("Hello, what's your name?");
 5 | |     let stdin = stdin().lock();
...  |
10 | |     let mut stdin = stdin.take(40);
   | |             ^^^^^
...  |
18 | | }
   | |_- temporary `stdin` is currently being dropped at the end of its contained scope
   |
   = note: this might lead to unnecessary resource contention
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening
help: merge the temporary construction with its single usage
   |
10 ~     
11 +     stdin.take(40).;
12 ~      //Now we print the result, our data is safe
   |

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

However after applying these diagnostics, the resulting code:

use std::{io::{stdin, BufRead, Read}, process};
fn main() {
    //Let's read from stdin
    println!("Hello, what's your name?");
    let stdin = stdin().lock();
    let mut buffer = String::with_capacity(10);
    //Here we lock stdin and block to 10 bytes
    // Our string is now then only 10 bytes.
    //Even if it overflows like expected, it will reallocate.
    
    stdin.take(40).;
     //Now we print the result, our data is safe
    println!("Our string has a capacity of {}",buffer.capacity());
    println!("Hello {}!",buffer);
    //The string is freed automatically.
}
//Now let's try to overflow!! The string was reallocated.

no longer compiled:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.W9mV5T7suTWy/icemaker_clippyfix_tempdir.Z8u9dYrFB4Kw/_snippet_0)
error: unexpected token: `;`
  --> src/main.rs:11:20
   |
11 |     stdin.take(40).;
   |                    ^

error: could not compile `_snippet_0` (bin "_snippet_0" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_snippet_0` (bin "_snippet_0") due to 1 previous error

Version:

rustc 1.91.0-nightly (91ee6a405 2025-08-26)
binary: rustc
commit-hash: 91ee6a4057ce4bf1ab6d2f932cae497488d67c81
commit-date: 2025-08-26
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions