Skip to content

Comments

Assume that any external function might return a type alias#16415

Merged
dswij merged 1 commit intorust-lang:masterfrom
samueltardieu:issues/14625
Feb 14, 2026
Merged

Assume that any external function might return a type alias#16415
dswij merged 1 commit intorust-lang:masterfrom
samueltardieu:issues/14625

Conversation

@samueltardieu
Copy link
Member

@samueltardieu samueltardieu commented Jan 16, 2026

We might not have the declaration around if extern crate is used, or if rust-src is not installed for the standard library. Better err on the safe side rather than having a behavior depending on what happens to be available at compile time.

Fixes #14625

changelog: [unnecessary_cast]: do not warn on casts of external function return type

@samueltardieu
Copy link
Member Author

Draft for the time being, in order to get a lintcheck run.

@github-actions
Copy link

github-actions bot commented Jan 16, 2026

Lintcheck changes for 759c2bf

Lint Added Removed Changed
clippy::unnecessary_cast 4 0 1

This comment will be updated if you push new changes

@samueltardieu
Copy link
Member Author

Well, lintcheck shows that we detect more cases by doing it more cleanly. We could also exclude code behind rust-src if we have a way to detect this easily, but this might be overkill.

Undrafting.

@samueltardieu samueltardieu marked this pull request as ready for review January 16, 2026 19:26
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 16, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 16, 2026

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

We might not have the declaration around if `extern crate` is used, or if
rust-src is not installed for the standard library. Better err on the safe
side rather than having a behavior depending on what happens to be available
at compile time.
@Jarcho
Copy link
Contributor

Jarcho commented Jan 16, 2026

The lint emission in libc is wrong. cmsg_len is defined as size_t.

@samueltardieu
Copy link
Member Author

samueltardieu commented Jan 17, 2026

The lint emission in libc is wrong. cmsg_len is defined as size_t.

It is just a manifestation of an existing shortcoming which already exists and was masked in libc at just this line for whatever reason. Other casts in this file were already detected the same way. There are no attempts to detect type aliases in struct fields or tuple definitions.

The following program exhibits this shortcoming both before and after this PR:

#![allow(unused)]
#![deny(clippy::unnecessary_cast)]

type I = i32;
struct S {
    i: I,
}

fn main() {
    let s = S { i: 42 };
    let i = s.i as i32;
}
$ clippy-driver t.rs
error: casting to the same type is unnecessary (`i32` -> `i32`)
  --> t.rs:11:13
   |
11 |     let i = s.i as i32;
   |             ^^^^^^^^^^ help: try: `s.i`

@dswij dswij added this pull request to the merge queue Feb 14, 2026
Merged via the queue into rust-lang:master with commit ad7979b Feb 14, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 14, 2026
@samueltardieu samueltardieu deleted the issues/14625 branch February 14, 2026 17:23
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 this pull request may close these issues.

unnecessary_cast false negative with rust-src installed

4 participants