-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Show better warning for trying to cast non-u8 scalar to char #48033
Show better warning for trying to cast non-u8 scalar to char #48033
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
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.
r=me after addressing comments.
src/librustc_lint/types.rs
Outdated
OVERFLOWING_LITERALS, | ||
e.span, | ||
"only u8 can be casted into char"); | ||
err.help(&format!("try with `'\\u{:X}' as char`", lit_val)); |
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.
Could this be a span_suggestion
instead? Specially so that the help doesn't appear after the "lint defined here" span. Also, that way it can be easily applied by RLS enabled tools.
You don't need to cast (as char
) when you have a literal char
(\uEF8888
).
I would probably word this as: "instead of casting this numeric literal, use a char
literal".
src/test/ui/cast_char.rs
Outdated
#![deny(overflowing_literals)] | ||
|
||
fn main() { | ||
const XYZ: char = 0xEF8888 as char; |
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.
Add test line using a non-hex, non-u8 numeric literal.
09f038d
to
d665ea8
Compare
Updated. |
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.
Love the PR!
I left a few more changes that I'd like to see before merging. By doing the suggested changes, the output would be:
14 | const XYZ: char = 0x1F888 as char;
| ^^^^^^^-------- help: use a char literal instead: `'/u{1F888}'`
src/librustc_lint/types.rs
Outdated
err.span_suggestion(e.span, | ||
&format!("try with `'\\u{{{:X}}}'`", | ||
lit_val), | ||
"".to_owned()); |
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 should be format!("'\\u{{{:X}}}'", lit_val)
. Otherwise when tools try to apply this will remove the code from the users file.
src/librustc_lint/types.rs
Outdated
OVERFLOWING_LITERALS, | ||
e.span, | ||
"only u8 can be casted into char"); | ||
err.span_suggestion(e.span, |
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 should be a span enclosing all of lit as char
(probably the parent_expr
's span). If you don't want to have overlapping spans, use the same span in the DiagnosticBuilder
and for the suggestion.
src/librustc_lint/types.rs
Outdated
e.span, | ||
"only u8 can be casted into char"); | ||
err.span_suggestion(e.span, | ||
&format!("try with `'\\u{{{:X}}}'`", |
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.
The text could be something along the lines of: "use a char literal instead"
.
#![deny(overflowing_literals)] | ||
|
||
fn main() { | ||
const XYZ: char = 0x1F888 as char; |
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.
Please also add another case where the numeric literal is not formatted in hexadecimal. That way we can verify that the suggestion will be correct, even if the literal was originally decimal.
d665ea8
to
0cccd9a
Compare
Updated. |
@bors r+ rollup |
📌 Commit 0cccd9a has been approved by |
…sage, r=estebank Show better warning for trying to cast non-u8 scalar to char Fixes rust-lang#44201.
…sage, r=estebank Show better warning for trying to cast non-u8 scalar to char Fixes rust-lang#44201.
…sage, r=estebank Show better warning for trying to cast non-u8 scalar to char Fixes rust-lang#44201.
let mut err = cx.struct_span_lint( | ||
OVERFLOWING_LITERALS, | ||
parent_expr.span, | ||
"only u8 can be casted into char"); |
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.
It should read "can be cast into char", not "casted"
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.
Please open an issue for it or fix the spelling directly. :) (good catch!)
Fixes #44201.