rust-lang/rust-clippy

TOCTOU: call `Path::metadata` after `Path::exists`

Open

#17,158 建立於 2026年6月4日

在 GitHub 查看
 (2 留言) (1 反應) (1 負責人)Rust (1,391 fork)batch import
A-lintgood first issue

倉庫指標

Star
 (10,406 star)
PR 合併指標
 (平均合併 16天 6小時) (30 天內合併 79 個 PR)

描述

What it does

Detect when calling Path::exists followed by another operation that would cause a new syscall to be used on the path, like Path::metadata.

Advantage

Remove a source of TOCTOU bugs.

Drawbacks

No response

Example

if new_path.exists() {
    let md = new_path.metadata().unwrap();

Could be written as:

if let Ok(md) = new_path.metadata() {

Comparison with existing lints

No response

Additional Context

https://github.com/uutils/coreutils/pull/11711/changes

貢獻者指南