Skip to content

clippy nightly doesn't correctly fix CStr creation #13531

Closed
@mutexlox-signal

Description

@mutexlox-signal

Summary

When running clippy's latest nightly, the automatically applied suggestion to use a CStr literal (https://rust-lang.github.io/rust-clippy/master/index.html#/manual_c_str_literals) incorrectly applies to projects on pre-2021 editions, causing clippy --fix to output non-compiling code.

Reproducer

I tried this code:

use std::ffi::CStr;
fn main() {
    let s = CStr::from_bytes_with_nul(b"test\0").unwrap();
    println!("Hello, world: {:?}", s);
}

with this Cargo.toml:

[package]
name = "clippy-test"
version = "0.1.0"
edition = "2015"

[dependencies]

I ran cargo +nightly clippy --fix --allow-dirty

I expected to see this happen:

No clippy warnings -- the edition is 2015, so c-str literals are not supported

Instead, this happened:

$ cargo +nightly clippy --fix --allow-dirty
    Checking clippy-test v0.1.0 (/Users/miriam/clippy-test)
warning: failed to automatically apply fixes suggested by rustc to crate `clippy_test`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `"test"`
 --> src/main.rs:3:14
  |
3 |     let s = c"test";
  |              ^^^^^^ expected one of 8 possible tokens
  |
  = note: you may be trying to write a c-string literal
  = note: c-string literals require Rust 2021 or later
  = help: set `edition = "2021"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

warning: unused import: `std::ffi::CStr`
 --> src/main.rs:1:5
  |
1 | use std::ffi::CStr;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: aborting due to 1 previous error; 1 warning emitted

Original diagnostics will follow.

warning: calling `CStr::new` with a byte string literal
 --> src/main.rs:3:13
  |
3 |     let s = CStr::from_bytes_with_nul(b"test\0").unwrap();
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a `c""` literal: `c"test"`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_c_str_literals
  = note: `#[warn(clippy::manual_c_str_literals)]` on by default

warning: `clippy-test` (bin "clippy-test" test) generated 1 warning (run `cargo clippy --fix --bin "clippy-test" --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `clippy_test`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `"test"`
 --> src/main.rs:3:14
  |
3 |     let s = c"test";
  |              ^^^^^^ expected one of 8 possible tokens
  |
  = note: you may be trying to write a c-string literal
  = note: c-string literals require Rust 2021 or later
  = help: set `edition = "2021"` in `Cargo.toml`
  = note: for more on editions, read https://doc.rust-lang.org/edition-guide

warning: unused import: `std::ffi::CStr`
 --> src/main.rs:1:5
  |
1 | use std::ffi::CStr;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error: aborting due to 1 previous error; 1 warning emitted

Original diagnostics will follow.

warning: `clippy-test` (bin "clippy-test") generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.08s

Version

rustc 1.83.0-nightly (eb4e23467 2024-10-09)
binary: rustc
commit-hash: eb4e2346748e1760f74fcaa27b42431e0b95f8f3
commit-date: 2024-10-09
host: aarch64-apple-darwin
release: 1.83.0-nightly
LLVM version: 19.1.1

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions