Skip to content

Conversation

mlegner
Copy link
Contributor

@mlegner mlegner commented Mar 2, 2020

This PR resolves false positives of unnecessary_cast for hexadecimal integers to floats and adds a corresponding test case.

Fixes: #5220

changelog: none

@JohnTitor JohnTitor added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 3, 2020
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add test cases for 0b and 0o

return;
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits &&
num_lit.radix != Radix::Hexadecimal {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also want to not lint this for 0o and 0b, so you could add a is_decimal() method to the NumericLiteral struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For 0o and 0b, the suffixes _f32 or _f64 would not be interpreted as part of the literal value such that the cast is indeed unnecessary. Am I correct?
However, it would probably make sense to print the suggestion also in octal or binary format. I will work on that and modify the PR accordingly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same behavior for 0b and 0o Playground:

warning: casting integer literal to `f32` is unnecessary
 --> src/main.rs:2:13
  |
2 |     let _ = 0b10101 as f32;
  |             ^^^^^^^^^^^^^^ help: try: `21_f32`
  |
  = note: `#[warn(clippy::unnecessary_cast)]` on by default

If any other radix, than decimal is used, it is most definitely intended, so suggesting to change it to decimal+_f32 is not something we want to do. Since binary and octal float literals are not supported, we also want to bail out there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for the additional pointers. I will include the binary and octal case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@flip1995 flip1995 added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 3, 2020
@mlegner
Copy link
Contributor Author

mlegner commented Mar 3, 2020

@flip1995 I moved the NumericLiteral to its own module and now also handle the binary and octal cases. I needed to make some fields public such that they remain accessible.

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall. Only 1 NIT left.

@mlegner
Copy link
Contributor Author

mlegner commented Mar 4, 2020

Thanks a lot for the feedback, that was very helpful.

@bors
Copy link
Contributor

bors commented Mar 4, 2020

☔ The latest upstream changes (presumably #5246) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995
Copy link
Member

flip1995 commented Mar 4, 2020

Thanks, a rebase is needed, because of #5246. After that this is good to go.

@mlegner mlegner changed the title Resolve false positives of unnecessary_cast for hexadecimal integers Resolve false positives of unnecessary_cast for non-decimal integers Mar 4, 2020
@mlegner
Copy link
Contributor Author

mlegner commented Mar 4, 2020

@flip1995 Rebase done.

@flip1995
Copy link
Member

flip1995 commented Mar 4, 2020

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 4, 2020

📌 Commit 185fa0d has been approved by flip1995

@bors
Copy link
Contributor

bors commented Mar 4, 2020

⌛ Testing commit 185fa0d with merge 329923e...

@bors
Copy link
Contributor

bors commented Mar 4, 2020

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 329923e to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive in unnecessary_cast with hex literals
4 participants