Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clippy nightly doesn't correctly fix CStr creation #13531

Closed
mutexlox-signal opened this issue Oct 10, 2024 · 2 comments · Fixed by #13532
Closed

clippy nightly doesn't correctly fix CStr creation #13531

mutexlox-signal opened this issue Oct 10, 2024 · 2 comments · Fixed by #13532
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@mutexlox-signal
Copy link

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

@mutexlox-signal mutexlox-signal added the C-bug Category: Clippy is not doing the correct thing label Oct 10, 2024
@mutexlox-signal
Copy link
Author

Note that non-nightly produces no warnings:

$ cargo rustc -- -Vv
   Compiling clippy-test v0.1.0 (/Users/miriam/clippy-test)
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
$ cargo clippy
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
$

mutexlox-signal added a commit to mutexlox-signal/cubeb-coreaudio-rs that referenced this issue Oct 10, 2024
Due to rust-lang/rust-clippy#13531, this
incorrectly applies here on nightly builds of clippy -- it should not,
since this project is on edition 2015 by default.
mutexlox-signal added a commit to mutexlox-signal/cubeb-pulse-rs that referenced this issue Oct 10, 2024
@y21
Copy link
Member

y21 commented Oct 10, 2024

Note that non-nightly produces no warnings:

If you run with -Wclippy::manual_c_str_literals then you should also see this on stable. The lint was only recently uplifted to a warn-by-default category whereas on stable it is in pedantic (allow-by-default).

mutexlox-signal added a commit to mutexlox-signal/cubeb-coreaudio-rs that referenced this issue Oct 10, 2024
Due to rust-lang/rust-clippy#13531, this
incorrectly applies here on nightly builds of clippy -- it should not,
since this project is on edition 2015 by default.
mutexlox-signal added a commit to mutexlox-signal/cubeb-pulse-rs that referenced this issue Oct 10, 2024
kinetiknz pushed a commit to mozilla/cubeb-pulse-rs that referenced this issue Oct 10, 2024
kinetiknz pushed a commit to mozilla/cubeb-coreaudio-rs that referenced this issue Oct 10, 2024
Due to rust-lang/rust-clippy#13531, this
incorrectly applies here on nightly builds of clippy -- it should not,
since this project is on edition 2015 by default.
bors added a commit that referenced this issue Oct 11, 2024
Only emit `manual_c_str_literals` in >= Edition 2021

Fixes #13531

changelog: none
@bors bors closed this as completed in 47903db Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants