Skip to content

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

Merged
merged 6 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 72 additions & 18 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) => {
Expand All @@ -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,
Copy link
Contributor

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...

);
} else {
err.span_suggestion_with_applicability(
expr.span,
&format!("{}, which {}", msg, will_sign_extend),
into_suggestion,
Applicability::MachineApplicable
);
}
}
}
true
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
);
Copy link
Contributor

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).

} 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,
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/mismatched_types/numeric-literal-cast.rs
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);
}
Copy link
Contributor

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?


13 changes: 13 additions & 0 deletions src/test/ui/mismatched_types/numeric-literal-cast.stderr
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`.