about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcsmoe <35686186+csmoe@users.noreply.github.com>2018-09-16 20:03:44 +0800
committercsmoe <35686186+csmoe@users.noreply.github.com>2018-09-16 20:06:31 +0800
commit19840793e57e4903e00edac963f30c9de34b1ea3 (patch)
treee710d983e1a8cf5384ac204c9d58d2c0579fd3d3
parente4ba1d41e3c2a66215943e43de70f93bb195b1f0 (diff)
downloadrust-19840793e57e4903e00edac963f30c9de34b1ea3.tar.gz
rust-19840793e57e4903e00edac963f30c9de34b1ea3.zip
lint to change numeric literal instead of into
-rw-r--r--src/librustc_typeck/check/demand.rs90
1 files changed, 72 insertions, 18 deletions
diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs
index 4e22ead8db9..2f80edd5433 100644
--- a/src/librustc_typeck/check/demand.rs
+++ b/src/librustc_typeck/check/demand.rs
@@ -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,
+                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,
+                            );
+                        } 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,