-
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? 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
Changes from 4 commits
1984079
0ff0669
17a28f7
d31a2a0
9d6c4f0
2fb6585
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -419,6 +419,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
if needs_paren { "(" } else { "" }, | ||
src, | ||
if needs_paren { ")" } else { "" }); | ||
let suffix_suggestion = format!( | ||
"{}{}{}{}", | ||
if needs_paren { "(" } else { "" }, | ||
src.trim_right_matches(&checked_ty.to_string()), | ||
expected_ty, | ||
if needs_paren { ")" } else { "" }, | ||
); | ||
|
||
let is_suffixed = |expr: &hir::Expr| { | ||
if let hir::ExprKind::Lit(lit) = &expr.node { | ||
lit.node.is_suffixed() | ||
} else { | ||
false | ||
} | ||
}; | ||
|
||
match (&expected_ty.sty, &checked_ty.sty) { | ||
(&ty::Int(ref exp), &ty::Int(ref found)) => { | ||
|
@@ -444,12 +459,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
} | ||
} | ||
_ => { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!("{}, which {}", msg, will_sign_extend), | ||
into_suggestion, | ||
Applicability::MachineApplicable | ||
); | ||
if is_suffixed(expr) { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!( | ||
"change the type of the numeric literal from `{}` to `{}`", | ||
checked_ty, | ||
expected_ty, | ||
), | ||
suffix_suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} else { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!("{}, which {}", msg, will_sign_extend), | ||
into_suggestion, | ||
Applicability::MachineApplicable | ||
); | ||
} | ||
} | ||
} | ||
true | ||
|
@@ -477,12 +505,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
} | ||
} | ||
_ => { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!("{}, which {}", msg, will_zero_extend), | ||
into_suggestion, | ||
Applicability::MachineApplicable | ||
); | ||
if is_suffixed(expr) { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!( | ||
"change the type of the numeric literal from `{}` to `{}`", | ||
checked_ty, | ||
expected_ty, | ||
), | ||
suffix_suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
} else { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!("{}, which {}", msg, will_zero_extend), | ||
into_suggestion, | ||
Applicability::MachineApplicable | ||
); | ||
} | ||
} | ||
} | ||
true | ||
|
@@ -583,12 +624,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
} | ||
(&ty::Float(ref exp), &ty::Float(ref found)) => { | ||
if found.bit_width() < exp.bit_width() { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!("{} in a lossless way", msg), | ||
into_suggestion, | ||
Applicability::MachineApplicable | ||
); | ||
if is_suffixed(expr) { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!( | ||
"change the type of the numeric literal from `{}` to `{}`", | ||
checked_ty, | ||
expected_ty, | ||
), | ||
suffix_suggestion, | ||
Applicability::MaybeIncorrect, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} else { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
&format!("{} in a lossless way", msg), | ||
into_suggestion, | ||
Applicability::MachineApplicable | ||
); | ||
} | ||
} else if can_cast { | ||
err.span_suggestion_with_applicability( | ||
expr.span, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
fn foo(_: u16) {} | ||
fn main() { | ||
foo(8u8); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test for floats too? |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/numeric-literal-cast.rs:13:9 | ||
| | ||
LL | foo(8u8); | ||
| ^^^ expected u16, found u8 | ||
help: change the type of the numeric literal from `u8` to `u16` | ||
| | ||
LL | foo(8u16); | ||
| ^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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
...