RS.CLIPPY.SUSPICIOUS_OPEN_OPTIONS

Suspicious combination of options for opening a file

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: suspicious_open_options. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for the suspicious use of OpenOptions::create() without an explicit OpenOptions::truncate().

Why is this bad?

create() alone will either create a new file or open an existing file. If the file already exists, it will be overwritten when written to, but the file will not be truncated by default. If less data is written to the file than it already contains, the remainder of the file will remain unchanged, and the end of the file will contain old data. In most cases, one should either use create_new to ensure the file is created from scratch, or ensure truncate is called so that the truncation behaviour is explicit. truncate(true) will ensure the file is entirely overwritten with new data, whereas truncate(false) will explicitly keep the default behavior.

Example

use std::fs::OpenOptions;

OpenOptions::new().create(true);

Use instead:

use std::fs::OpenOptions;

OpenOptions::new().create(true).truncate(true);