rust-lang / rust-lang/rust-clippy
`significant_drop_tightening` on guard while indirectly borrowed
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
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
I-suggestion-causes-errorplease :3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Rust reproducer with the significant_drop_tightening lint enabled, then compare it with the linked src/macros.rs example. Inspect how indirectly borrowed values affect the lint diagnostic and suggestion; done means the lint no longer emits invalid fixes or incorrect warnings for these borrow relationships, while still reporting valid cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100