rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.98k stars 12.54k forks source link

comparison is useless due to type limits #119809

Closed SvenKeimpema closed 8 months ago

SvenKeimpema commented 8 months ago

I tried this code:

#[macro_export]
    macro_rules! get_bit {
        ($bb:expr, $sq:expr) => {
            {
                let mut bb: u64 = $bb;
                let sq: u64 = $sq as u64;

                if(0u64 <= sq && sq < 64u64) {
                    bb &= (1u64 << sq);
                }else {
                    bb = 0;
                }

                bb != 0
            }
        };
    }

I expected to see this happen: explanation Expected the code to run without any type of warnings/errors.

Instead, this happened: explanation

warning: comparison is useless due to type limits
  --> src/board/bitboard.rs:25:20
   |
25 |                 if(0u64 <= sq && sq < 64u64) {
   |                    ^^^^^^^^^^
...
71 |                 if get_bit!(bb, sq) {
   |                    ---------------- in this macro invocation
   |
   = note: this warning originates in the macro `get_bit` (in Nightly builds, run with -Z macro-backtrace for more info)

warning here seems kind of strange bc i am comparing u64 with u64

Meta

rustc --version --verbose:

rustc 1.74.1 (a28077b28 2023-12-04)
binary: rustc
commit-hash: a28077b28a02b92985b3a3faecf92813155f1ea1
commit-date: 2023-12-04
host: x86_64-unknown-linux-gnu
release: 1.74.1
LLVM version: 17.0.4
krtab commented 8 months ago

Hi! You get the warning because checking that an u64 is greater than or equal to zero cannot fail. You can remove this comparison or mark it #[allow(unused_comparisons)].

For further questions regarding this (non-bug) topic, please rather use https://users.rust-lang.org/ or other community resources.

SvenKeimpema commented 8 months ago

ok, that seemed to be it. of course kind of dumb mistake by me checking if u64 < 0 bc it can't although still seems like kind of a bug tho? or is it raising the warning because it can't ever be less then 0?

clubby789 commented 8 months ago

Yes, the warning is raised because u64 values can never be less than zero (unlike, for example, an i64 which may be negative).