-
Notifications
You must be signed in to change notification settings - Fork 1.8k
cast_lossless: lint when converting usize, isize, char and float as well
#14470
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #![warn(clippy::cast_lossless)] | ||
| #![allow(clippy::char_lit_as_u8)] | ||
|
|
||
| type I32 = i32; | ||
| type U32 = u32; | ||
|
|
||
| fn main() { | ||
| let _ = u32::from('a'); | ||
| //~^ cast_lossless | ||
| let _ = 'a' as i32; | ||
| let _ = u64::from('a'); | ||
| //~^ cast_lossless | ||
| let _ = 'a' as i64; | ||
| let _ = U32::from('a'); | ||
| //~^ cast_lossless | ||
| let _ = 'a' as I32; | ||
|
|
||
| let _ = 'a' as u8; | ||
| let _ = 'a' as i8; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #![warn(clippy::cast_lossless)] | ||
| #![allow(clippy::char_lit_as_u8)] | ||
|
|
||
| type I32 = i32; | ||
| type U32 = u32; | ||
|
|
||
| fn main() { | ||
| let _ = 'a' as u32; | ||
| //~^ cast_lossless | ||
| let _ = 'a' as i32; | ||
| let _ = 'a' as u64; | ||
| //~^ cast_lossless | ||
| let _ = 'a' as i64; | ||
| let _ = 'a' as U32; | ||
| //~^ cast_lossless | ||
| let _ = 'a' as I32; | ||
|
|
||
| let _ = 'a' as u8; | ||
| let _ = 'a' as i8; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| error: casts from `char` to `u32` can be expressed infallibly using `From` | ||
| --> tests/ui/cast_lossless_char.rs:8:13 | ||
| | | ||
| LL | let _ = 'a' as u32; | ||
| | ^^^^^^^^^^ | ||
| | | ||
| = help: an `as` cast can become silently lossy if the types change in the future | ||
| = note: `-D clippy::cast-lossless` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::cast_lossless)]` | ||
| help: use `u32::from` instead | ||
| | | ||
| LL - let _ = 'a' as u32; | ||
| LL + let _ = u32::from('a'); | ||
| | | ||
|
|
||
| error: casts from `char` to `u64` can be expressed infallibly using `From` | ||
| --> tests/ui/cast_lossless_char.rs:11:13 | ||
| | | ||
| LL | let _ = 'a' as u64; | ||
| | ^^^^^^^^^^ | ||
| | | ||
| = help: an `as` cast can become silently lossy if the types change in the future | ||
| help: use `u64::from` instead | ||
| | | ||
| LL - let _ = 'a' as u64; | ||
| LL + let _ = u64::from('a'); | ||
| | | ||
|
|
||
| error: casts from `char` to `u32` can be expressed infallibly using `From` | ||
| --> tests/ui/cast_lossless_char.rs:14:13 | ||
| | | ||
| LL | let _ = 'a' as U32; | ||
| | ^^^^^^^^^^ | ||
| | | ||
| = help: an `as` cast can become silently lossy if the types change in the future | ||
| help: use `U32::from` instead | ||
| | | ||
| LL - let _ = 'a' as U32; | ||
| LL + let _ = U32::from('a'); | ||
| | | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can make an exception here to still lint if
!expr.span.from_expansion()? That is, still lint(So basically the new false negative in the tests).
I can see how linting expressions in macro definitions could be a hazard since the macro might be invoked multiple times with different types or expressions which are only sometimes compatible, but I don't think there is a harm if the primary cast expression is in the root context (regardless of what or where
cast_from/cast_tocomes from), is there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is possible for the most part. The
SyntaxContextfor the cast and the target type should be the same. The expression really just needs to be a child context which can be checked with something likewalk_span_to_context(expr.span, cast_ctxt).is_some().