Skip to content
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

Catch xor vs power confusion #4205

Closed
kornelski opened this issue Jun 13, 2019 · 8 comments · Fixed by #9506
Closed

Catch xor vs power confusion #4205

kornelski opened this issue Jun 13, 2019 · 8 comments · Fixed by #9506
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@kornelski
Copy link
Contributor

kornelski commented Jun 13, 2019

2x is often written in ASCII as 2^x, but in Rust this happens to xor the value instead of raising it to the power.

let bad  = 2^31;
let good = 1<<31;

Apparently, people make that mistake, and so far it goes undetected:

https://twitter.com/johnregehr/status/1139302389612077056

@flip1995
Copy link
Member

flip1995 commented Jun 14, 2019

I'm curious: Is there a programming language where x^y means "raise x to the power of y"?

Since writing 2^31 is totally valid rust code and whether it is correct or not strongly depends on the context, I would classify this as a restriction lint.

@flip1995 flip1995 added L-suggestion Lint: Improving, adding or fixing lint suggestions good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints labels Jun 14, 2019
@Manishearth
Copy link
Member

I think writing this with constants in base ten is highly suspicious and could be a regular or pedantic lint. Especially for 2^x

@kornelski
Copy link
Contributor Author

^ is exponentiation in AWK, BASIC, J, MATLAB, Mathematica, R, Excel, TeX, Haskell, and some others, but I don't think that matters. It's more commonly an ASCII-compatible/keyboard-accessible way to write down exponentiation in docs, readmes, and comments. I presume copying of formulas from other documents is most likely to be the source of this error.

The fact that x^y is technically perfectly valid in Rust is part of the problem, because it'll compile, and just give a result different from what the programmer meant, so this lint is all about second-guessing the programmer.

I think raising 2 and 10 to a power is a much more common operation than xoring constant 2 or 10 with something.

x^y may be anything, but 2^x and 10^x are very suspicious (x^2 maybe, x^10 not).

xoring is all about bits, so the lint shouldn't apply to non-decimal notations (e.g. 0xA^x or x^0b10)

@Walther
Copy link

Walther commented Jun 17, 2019

Found this issue after being linked to this GCC thread on the same topic https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90885 - possibly useful for additional context / comments 🙂

My initial reaction was "ha, that definitely sounds like an easy human error to make, and sounds exactly like one of those errors where rustc and/or clippy would go the extra mile in being magically helpful as they always are".

Alas,
Screen Shot 2019-06-17 at 00 48 07

I agree that this is probably a high-value lint to add, considering how easy it probably is to make this mistake (ref. ascii documentation etc), and the likelihood of intended behavior of xor vs exponentiation when dealing with decimal values, especially constants.

EDIT: I'll give a try at implementing this 🙂

@jolson88
Copy link
Contributor

Commented in closed PR (as it was inactive). Would anybody mind me picking this up and pushing it across the finish line since it's been a while?

@HKalbasi
Copy link
Member

Three inactive PR, cool!

@blyxyas
Copy link
Member

blyxyas commented Sep 18, 2022

Really impressed this isn't solved yet, I got a lucky one!
@rustbot claim

Edit: 3 pull requests before mine. Relevant XKCD

@leonardo-m
Copy link

leonardo-m commented Sep 24, 2022

I'm curious: Is there a programming language where x^y means "raise x to the power of y"?

In Haskell:

Prelude> 2 ^ 5
32
Prelude> 2 ^^ 5
32.0    

In Python ** is used. D language uses ^^.

@bors bors closed this as completed in 37d338c Oct 31, 2022
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 L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
9 participants