rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.01k stars 1.47k forks source link

`significant_drop_tightening` on guard while indirectly borrowed #11279

Open madonuko opened 11 months ago

madonuko commented 11 months ago

Summary

This issue seems to happen only on nightly. Not sure about beta.

Clippy will decide to drop() guards that are still being indirectly borrowed by another variable that will be later used.

Note that I used cargo +nightly to run clippy, so the output of cargo +nightly -vV is provided instead.

Also, the suggestions given by clippy in the reproducer is actually really really buggy for some reason.

Lint Name

significant_drop_tightening

Reproducer

#[warn(clippy::significant_drop_tightening)]

fn main() {
    let x = std::sync::Mutex::new((String::new(), String::new()));
    let mut guard = x.lock().unwrap();
    let (ss, _) = &mut *guard;
    ss.push_str("hai");
    let sth = &ss[..2];
    println!("{sth}");
    another();
}

fn another() {
    let y = &std::sync::Arc::new(std::sync::Mutex::new(String::new()));
    let mut guard = y.lock().unwrap();
    let ss = &mut *guard;
    ss.push_str("hai");
    let sss = &ss[..2];
    println!("{sss}");
    println!("hai?"); // maybe clippy should tell me to drop `guard` before this line? but somhow it doesn't? false negative?
}

Note: the message below is copy-pasted without changes whatsoever. Yes, it's broken as hell.

warning: temporary with significant `Drop` can be early dropped
  --> src/main.rs:5:13
   |
3  |   fn main() {
   |  ___________-
4  | |     let x = std::sync::Mutex::new((String::new(), String::new()));
5  | |     let mut guard = x.lock().unwrap();
   | |             ^^^^^
6  | |     let (ss, _) = &mut *guard;
...  |
10 | |     another();
11 | | }
   | |_- temporary `guard` 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: the lint level is defined here
  --> src/main.rs:1:8
   |
1  | #[warn(clippy::significant_drop_tightening)]
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: merge the temporary construction with its single usage
   |
5  ~     
6  +     x.lock().unwrap().;
   |
help: remove separated single usage
   |
6  -     let (ss, _) = &mut *guard;
6  +     
   |

I cannot fully reproduce this behaviour by Clippy: I initially found this by running Clippy on my project and it tells me this:

warning: temporary with significant `Drop` can be early dropped
   --> src/macros.rs:346:9
    |
340 |               if let Some(v) = v.last() {
    |  _______________________________________-
341 | |                 if let MacroType::Internal(_) = v {
342 | |                     stdout.write_fmt(format_args!("[<internal>]\t%{k}\t<builtin>\n"))?;
343 | |                     continue;
...   |
346 | |                 let ss = s.lock();
    | |                     ^^
...   |
353 | |                 stdout.write_fmt(format_args!("[{f}:{nline}:{col}]\t%{k}{p}\t{inner}\n"))?;
354 | |             }
    | |_____________- temporary `ss` 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: the lint level is defined here
   --> src/lib.rs:13:9
    |
13  | #![warn(clippy::nursery)]
    |         ^^^^^^^^^^^^^^^
    = note: `#[warn(clippy::significant_drop_tightening)]` implied by `#[warn(clippy::nursery)]`
help: drop the temporary after the end of its last usage
    |
352 ~                 let inner = &ss[*offset..*offset + *len];
353 +     drop(ss);
    |

but if you look at the actual code, you actually cannot drop it there because it's still being borrowed by inner and inner is used for stdout.write_fmt() on the next line. Note that the type of s is actually &Arc<Mutex<smartstring::alias::String>>. I tried to use this type in the reproducer instead but to no avail.

Version

cargo 1.73.0-nightly (7ac9416d8 2023-07-24)
release: 1.73.0-nightly
commit-hash: 7ac9416d82cd4fc5e707c9ec3574d22dff6466e5
commit-date: 2023-07-24
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.1.2-DEV (sys:0.4.63+curl-8.1.2 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: Red Hat Enterprise Linux 38.0.0 [64-bit]

Additional Labels

c410-f3r commented 11 months ago

Yes, it's broken as hell.

😁 😁 😁

Thank you for the feedback. The lint currently does not track lifetimes derived from the original guard but this situation will change once the code is ported to MIR in the next few months.