rust-lang / rust-lang/rust-clippy

Typealias shouldn't be expanded when suggesting casts/transmutes/type annotations

Open
#12,988 1 comment 1 reaction 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 tends to expand types when suggesting to e.g. annotate std::mem::transmute, however the original type could be different based on architecture, like c_char, thus such suggestions are not accurate and will likely result in compilation failures.

Reproducer

I tried this code:

// Suggestion
            mem::transmute::<usize, unsafe extern "C" fn(*mut ffi::_XIM, *mut i8, *mut i8)>(
                preedit_start_callback as usize,
            )

The said type annotation was suggested by clippy, which is correct, on X86, but the original function uses the ffi::XPointer and not the *mut i8, thus it failed to build on arm. Instead it should suggest

mod ffi {
pub type XPointer = *mut c_char;
pub type XIM = *mut _XIM;

pub enum _XIM {}
}

// Suggestion
            mem::transmute::<usize, unsafe extern "C" fn(ffi::XIM, ffi::XPointer, ffi::XPointer)>(
                preedit_start_callback as usize,
            )
Version
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
Additional Labels

@rustbot label +'I-suggestion-causes-error'

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 by reproducing the reported Clippy suggestion with the Rust code in the issue on architectures where the aliases differ from their expanded types. Trace the lint logic that generates suggestions for casts, transmutes, and type annotations, then add regression coverage for preserving aliases such as ffi::XPointer and ffi::XIM. Done means the suggestion remains architecture-independent and compiles across the affected targets.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.