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

贡献者指南