-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Suggest to change numeric literal instead of casting #54273
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
This comment has been minimized.
This comment has been minimized.
src/librustc_typeck/check/demand.rs
Outdated
{ | ||
// 42u8 | ||
// ^^ | ||
let lit_offset = src.len() - checked_ty.to_string().len(); |
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.
oops, brute sub should be optimized.
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 it!
A couple of nitpicks left. After addressing r=me.
src/librustc_typeck/check/demand.rs
Outdated
expected_ty, | ||
), | ||
suffix_suggestion, | ||
Applicability::MaybeIncorrect, |
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 feel these might count as MachineApplicable
...
src/librustc_typeck/check/demand.rs
Outdated
), | ||
suffix_suggestion, | ||
Applicability::MaybeIncorrect, | ||
); |
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 feel there's a bit of duplication on these, would it make sense to have is_suffixed
(with a more accurate name) take mut err
, checked_ty
and expected_ty
, and if it would return true now it would also apply the suggestion, and here you just call it as if !is_sufficed(expr) {
with the following block (as each one of these has slightly different wording).
fn foo(_: u16) {} | ||
fn main() { | ||
foo(8u8); | ||
} |
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.
Can you add a test for floats too?
src/librustc_typeck/check/demand.rs
Outdated
expr: &hir::Expr, | ||
expected_ty: Ty<'tcx>, | ||
checked_ty: Ty<'tcx>, | ||
note: Option<&str>| { |
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's the first time I've seen a closure that needed multiple arguments and I'm surprised rustfmt
suggests exactly this :)
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.
oh, expr
and *_ty
aren't needed actually, I'll remove them.
@bors r+ rollup |
📌 Commit c3b6e3f2ac2c8ef64168db4336645399380c5fdd has been approved by |
@bors r+ |
📌 Commit 2fb6585 has been approved by |
Suggest to change numeric literal instead of casting Closes rust-lang#54160 r? @estebank
Rollup of 9 pull requests Successful merges: - #53522 (Add doc for impl From for Addr) - #54097 (rustdoc: Remove namespace for keywords) - #54205 (Add treat-err-as-bug flag in rustdoc) - #54225 (Regression test for #53675.) - #54232 (add `-Z dont-buffer-diagnostics`) - #54273 (Suggest to change numeric literal instead of casting) - #54299 (Issue 54246) - #54311 (Remove README with now-out-of-date docs about docs.) - #54313 (OsStr: Document that it's not NUL terminated) Failed merges: r? @ghost
Closes #54160
r? @estebank