Skip to content

Lint on useless transmutes #441

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
Manishearth opened this issue Nov 7, 2015 · 9 comments
Closed

Lint on useless transmutes #441

Manishearth opened this issue Nov 7, 2015 · 9 comments
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types

Comments

@Manishearth
Copy link
Member

rust-lang/rust#11400

We should lint on transmutes between the same type, e.g. transmute::<u8,u8>(...)

The easiest thing to do here is to find ExprCalls which resolve to transmute, and checking if the expr_ty of the argument matches that of the whole call expression.

@Manishearth Manishearth added good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types A-lint Area: New lints labels Nov 7, 2015
@llogiq
Copy link
Contributor

llogiq commented Nov 7, 2015

This absolutely needs a macro check. And it should be mem::transmute, right? Just to make sure we don't catch anyone's own transmute(_) function, say, in a medieval fantasy game...

@Manishearth
Copy link
Member Author

Well, yeah, match_def_path takes care of that

@apasel422
Copy link
Contributor

I'm trying to implement this, but I'm wondering how to get a DefId from the Path in the ExprCall so that I can call match_def_path.

@Manishearth
Copy link
Member Author

use the def_map in tcx on the NodeId of the call?

@apasel422
Copy link
Contributor

Thanks! And to check if two types are equal?

@Manishearth
Copy link
Member Author

ty::Ty has an Eq impl.

@Manishearth
Copy link
Member Author

I can't recall if that actually works, or if it computes some arcane form of equality 😝. Worth a try though. If it works, see if it can also catch:

fn foo<T>(t: T) {
transmute::<T,T>(t);
}

or something.

@Manishearth
Copy link
Member Author

It looks like it does a pointer comparison, which sound scary but since it's in an explicit impl I'll believe it. Probably because all the TySs are borrowed from the same cache/arena.

@llogiq
Copy link
Contributor

llogiq commented Nov 9, 2015

Otherwise eq_op.rs should have a comparison function. It's not public yet, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

3 participants