Skip to content

Fix fail, named function cast to raw ptr, == it #14337

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

Closed
A4-Tacks opened this issue Mar 2, 2025 · 1 comment · Fixed by #14339
Closed

Fix fail, named function cast to raw ptr, == it #14337

A4-Tacks opened this issue Mar 2, 2025 · 1 comment · Fixed by #14339
Assignees

Comments

@A4-Tacks
Copy link

A4-Tacks commented Mar 2, 2025

Description

fn main() {
    let _x = main as *const () == main as *const ();
}

Run cargo clippy --fix outputs:

    Checking test_ v0.1.0 (/home/lrne/Project/Rust/test_)
warning: failed to automatically apply fixes suggested by rustc to crate `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[E0308]: arguments to this function are incorrect
    --> src/main.rs:2:14
     |
2    |     let _x = std::ptr::eq(main, main);
     |              ^^^^^^^^^^^^
     |
note: expected `*const _`, found fn item
    --> src/main.rs:2:27
     |
2    |     let _x = std::ptr::eq(main, main);
     |                           ^^^^
     = note: expected raw pointer `*const _`
                    found fn item `fn() {main}`
note: expected `*const _`, found fn item
    --> src/main.rs:2:33
     |
2    |     let _x = std::ptr::eq(main, main);
     |                                 ^^^^
     = note: expected raw pointer `*const _`
                    found fn item `fn() {main}`
note: function defined here
    --> /home/lrne/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:2143:8
     |
2143 | pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
     |        ^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: equal expressions as operands to `==`
 --> src/main.rs:2:14
  |
2 |     let _x = main as *const () == main as *const ();
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#eq_op
  = note: `#[warn(clippy::eq_op)]` on by default

warning: use `std::ptr::eq` when comparing raw pointers
 --> src/main.rs:2:14
  |
2 |     let _x = main as *const () == main as *const ();
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::ptr::eq(main, main)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq
  = note: `#[warn(clippy::ptr_eq)]` on by default

warning: `test_` (bin "test_" test) generated 2 warnings (run `cargo clippy --fix --bin "test_" --tests` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `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[E0308]: arguments to this function are incorrect
    --> src/main.rs:2:14
     |
2    |     let _x = std::ptr::eq(main, main);
     |              ^^^^^^^^^^^^
     |
note: expected `*const _`, found fn item
    --> src/main.rs:2:27
     |
2    |     let _x = std::ptr::eq(main, main);
     |                           ^^^^
     = note: expected raw pointer `*const _`
                    found fn item `fn() {main}`
note: expected `*const _`, found fn item
    --> src/main.rs:2:33
     |
2    |     let _x = std::ptr::eq(main, main);
     |                                 ^^^^
     = note: expected raw pointer `*const _`
                    found fn item `fn() {main}`
note: function defined here
    --> /home/lrne/.rustup/toolchains/stable-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:2143:8
     |
2143 | pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
     |        ^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: `test_` (bin "test_") generated 2 warnings (2 duplicates)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.81s

Version

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: aarch64-unknown-linux-gnu
release: 1.82.0
LLVM version: 19.1.1

Additional Labels

No response

@samueltardieu
Copy link
Contributor

@rustbot claim

github-merge-queue bot pushed a commit that referenced this issue Mar 4, 2025
This PR:

- lints more case of raw pointer comparisons
- do not omit the named function to raw pointer conversion before
suggesting
- trigger the `ptr_eq` lint only if `cmp_null` doesn't trigger first, as
this is a more specialized version
- lints against `!=` in addition to `==`

The `ptr_eq` code has been moved from under `operators` to `ptr.rs`, in
order to benefit from factorization.

Fix #14337

changelog: [`ptr_eq`]: handle more cases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants