Skip to content

Add warning for hexdec literals with mixed case-sensivity #703

@Robbepop

Description

@Robbepop

In Rust it is possible to define hexadecimal number literals with

let v1 = 0xff;
let v2 = 0xAC;

After the 0x prefix the programmer may use numerals from 0..9, a..f and A..F.
So this makes it also possible to mix the case-sensitivity even within the same numeric literal:

let v3 = 0xFfEeDdCc;

It is also possible to suffix the literal for type explicity:

let v1 = 0xaai32; // may be confusing
let v2 = 0xAA_i32; // clear intent!
assert_eq!(v1,v2);

This is not only likely unreadable but also may lead to confusion:

let v4 = 0xFFAA_f32;

In this case the programmer wanted to instantiate a float (note the f32 suffix) and it compiled fine.
However, it is not a float literal but still a valid hexdec literal which makes absolutely sense following the current rules.

let v5: f32 = 0xFFAA_f32; // error: mismatched types!

Improvement:
A possible improvement (not only to readability) is to add a warning when the user mixes case-sensitivity within the same hexa decimal literal as this would at least prevent the above case.
Using lower-case literals would still allow this possible trap, however it looks less tricky:

let v6 = 0xffaa_f32;
let v7 = 0xffaaf32;
assert_eq!(v6,v7);

Better Solution:
In my opinion an even better improvement would be to disallow lower-case characters in hexdec literals to make numeric suffixes more significant within the literal but I guess many people would disagree with this suggestion.

let e1 = 0xFF_AA00_11; // ok
let e2 = 0xFf_aAbB; // warning (or error!)
let e3 = 0xFA_CC_f32; // warning (or error!)
let e4 = 0xFACC_i64; // intent is clear!
let e5 = 0xFACCi32; // intent is still clearer than the following:
let e6 = 0xfacci32; // looks confusing

Another minor improvement for readability would be to allow suffixes (for hexdec literals) only when the last character of the literal is an _ (underscore).

Feasibility
This may break code if errors are used instead of warnings, however it should be possible to create tools that fix such code automatically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions