rust-lang / rust-lang/rust-clippy

`clippy::op_ref` on `CString` triggers even when MSRV does not support the required trait impls

Open
#15,868 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Clippy does not respect MSRV when suggesting to remove an & when comparing a CString and a &CStr on Rust 1.76.0.

Lint Name

clippy::op_ref

Reproducer

I tried this code:

fn intern_get_roundtrip(cstring: CString) -> bool {
    let mut table = SymbolTable::new();
    let sym = table.intern(cstring.clone()).unwrap();
    let retrieved_c_string = table.get(sym).unwrap();
    &*cstring == retrieved_c_string
}

I saw this happen:

warning: needlessly taken reference of left operand
    --> src/cstr.rs:1027:13
     |
1027 |             &*cstring == retrieved_c_string
     |             ---------^^^^^^^^^^^^^^^^^^^^^^
     |             |
     |             help: use the left value directly: `*cstring`
     |
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#op_ref
note: the lint level is defined here
    --> src/lib.rs:1:9
     |
   1 | #![warn(clippy::all)]
     |         ^^^^^^^^^^^
     = note: `#[warn(clippy::op_ref)]` implied by `#[warn(clippy::all)]`

I expected to see this happen: the lint should not fire because the crate has rust-version = "1.76.0" declared in Cargo.toml.

Applying this diff:

diff --git a/src/cstr.rs b/src/cstr.rs
index 75d702b..e2d3fa3 100644
--- a/src/cstr.rs
+++ b/src/cstr.rs
@@ -1024,7 +1024,7 @@ mod tests {
             let mut table = SymbolTable::new();
             let sym = table.intern(cstring.clone()).unwrap();
             let retrieved_c_string = table.get(sym).unwrap();
-            &*cstring == retrieved_c_string
+            *cstring == retrieved_c_string
         }

         fn table_contains_sym(cstring: CString) -> bool {

yields this error

   Compiling intaglio v1.10.0 (/home/runner/work/intaglio/intaglio)
error[E0308]: mismatched types
    --> src/cstr.rs:1027:25
     |
1027 |             *cstring == retrieved_c_string
     |             --------    ^^^^^^^^^^^^^^^^^^ expected `CStr`, found `&CStr`
     |             |
     |             expected because this is `CStr`
     |
help: consider dereferencing the borrow
     |
1027 |             *cstring == *retrieved_c_string
     |                         +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `intaglio` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...

when building with Rust 1.76.0, which does not have the required trait impls.

Failing GitHub Actions run is here: https://github.com/artichoke/intaglio/actions/runs/18436896685/job/52531604213?pr=324

Version
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-unknown-linux-gnu
release: 1.76.0
LLVM version: 17.0.6
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the clippy::op_ref lint and reproduce the report using the example in src/cstr.rs under Rust 1.76.0, checking the crate's rust-version declaration in Cargo.toml. Trace how the lint decides whether removing the reference is valid. Done means the lint does not suggest the incompatible change for the declared MSRV while retaining correct behavior on supported newer Rust versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.