]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/demand.rs
Improve some compiletest documentation
[rust.git] / src / librustc_typeck / check / demand.rs
1 use crate::check::FnCtxt;
2 use rustc::infer::InferOk;
3 use rustc::traits::{ObligationCause, ObligationCauseCode};
4
5 use syntax::util::parser::PREC_POSTFIX;
6 use syntax_pos::Span;
7 use rustc::hir;
8 use rustc::hir::def::Def;
9 use rustc::hir::Node;
10 use rustc::hir::{Item, ItemKind, print};
11 use rustc::ty::{self, Ty, AssociatedItem};
12 use rustc::ty::adjustment::AllowTwoPhase;
13 use errors::{Applicability, DiagnosticBuilder, SourceMapper};
14
15 use super::method::probe;
16
17 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
18     // Requires that the two types unify, and prints an error message if
19     // they don't.
20     pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
21         self.demand_suptype_diag(sp, expected, actual).map(|mut e| e.emit());
22     }
23
24     pub fn demand_suptype_diag(&self,
25                                sp: Span,
26                                expected: Ty<'tcx>,
27                                actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
28         let cause = &self.misc(sp);
29         match self.at(cause, self.param_env).sup(expected, actual) {
30             Ok(InferOk { obligations, value: () }) => {
31                 self.register_predicates(obligations);
32                 None
33             },
34             Err(e) => {
35                 Some(self.report_mismatched_types(&cause, expected, actual, e))
36             }
37         }
38     }
39
40     pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
41         if let Some(mut err) = self.demand_eqtype_diag(sp, expected, actual) {
42             err.emit();
43         }
44     }
45
46     pub fn demand_eqtype_diag(&self,
47                              sp: Span,
48                              expected: Ty<'tcx>,
49                              actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
50         self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
51     }
52
53     pub fn demand_eqtype_with_origin(&self,
54                                      cause: &ObligationCause<'tcx>,
55                                      expected: Ty<'tcx>,
56                                      actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
57         match self.at(cause, self.param_env).eq(expected, actual) {
58             Ok(InferOk { obligations, value: () }) => {
59                 self.register_predicates(obligations);
60                 None
61             }
62             Err(e) => {
63                 Some(self.report_mismatched_types(cause, expected, actual, e))
64             }
65         }
66     }
67
68     pub fn demand_eqtype_pat(
69         &self,
70         cause_span: Span,
71         expected: Ty<'tcx>,
72         actual: Ty<'tcx>,
73         match_expr_span: Option<Span>,
74     ) {
75         let cause = if let Some(span) = match_expr_span {
76             self.cause(
77                 cause_span,
78                 ObligationCauseCode::MatchExpressionArmPattern { span, ty: expected },
79             )
80         } else {
81             self.misc(cause_span)
82         };
83         self.demand_eqtype_with_origin(&cause, expected, actual).map(|mut err| err.emit());
84     }
85
86
87     pub fn demand_coerce(&self,
88                          expr: &hir::Expr,
89                          checked_ty: Ty<'tcx>,
90                          expected: Ty<'tcx>,
91                          allow_two_phase: AllowTwoPhase)
92                          -> Ty<'tcx> {
93         let (ty, err) = self.demand_coerce_diag(expr, checked_ty, expected, allow_two_phase);
94         if let Some(mut err) = err {
95             err.emit();
96         }
97         ty
98     }
99
100     // Checks that the type of `expr` can be coerced to `expected`.
101     //
102     // N.B., this code relies on `self.diverges` to be accurate. In
103     // particular, assignments to `!` will be permitted if the
104     // diverges flag is currently "always".
105     pub fn demand_coerce_diag(&self,
106                               expr: &hir::Expr,
107                               checked_ty: Ty<'tcx>,
108                               expected: Ty<'tcx>,
109                               allow_two_phase: AllowTwoPhase)
110                               -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
111         let expected = self.resolve_type_vars_with_obligations(expected);
112
113         let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
114             Ok(ty) => return (ty, None),
115             Err(e) => e
116         };
117
118         let cause = self.misc(expr.span);
119         let expr_ty = self.resolve_type_vars_with_obligations(checked_ty);
120         let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
121
122         // If the expected type is an enum (Issue #55250) with any variants whose
123         // sole field is of the found type, suggest such variants. (Issue #42764)
124         if let ty::Adt(expected_adt, substs) = expected.sty {
125             if expected_adt.is_enum() {
126                 let mut compatible_variants = expected_adt.variants
127                     .iter()
128                     .filter(|variant| variant.fields.len() == 1)
129                     .filter_map(|variant| {
130                         let sole_field = &variant.fields[0];
131                         let sole_field_ty = sole_field.ty(self.tcx, substs);
132                         if self.can_coerce(expr_ty, sole_field_ty) {
133                             let variant_path = self.tcx.def_path_str(variant.did);
134                             // FIXME #56861: DRYer prelude filtering
135                             Some(variant_path.trim_start_matches("std::prelude::v1::").to_string())
136                         } else {
137                             None
138                         }
139                     }).peekable();
140
141                 if compatible_variants.peek().is_some() {
142                     let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
143                     let suggestions = compatible_variants
144                         .map(|v| format!("{}({})", v, expr_text));
145                     err.span_suggestions(
146                         expr.span,
147                         "try using a variant of the expected type",
148                         suggestions,
149                         Applicability::MaybeIncorrect,
150                     );
151                 }
152             }
153         }
154
155         self.suggest_ref_or_into(&mut err, expr, expected, expr_ty);
156
157         (expected, Some(err))
158     }
159
160     pub fn get_conversion_methods(&self, span: Span, expected: Ty<'tcx>, checked_ty: Ty<'tcx>)
161                               -> Vec<AssociatedItem> {
162         let mut methods = self.probe_for_return_type(span,
163                                                      probe::Mode::MethodCall,
164                                                      expected,
165                                                      checked_ty,
166                                                      hir::DUMMY_HIR_ID);
167         methods.retain(|m| {
168             self.has_no_input_arg(m) &&
169                 self.tcx.get_attrs(m.def_id).iter()
170                 // This special internal attribute is used to whitelist
171                 // "identity-like" conversion methods to be suggested here.
172                 //
173                 // FIXME (#46459 and #46460): ideally
174                 // `std::convert::Into::into` and `std::borrow:ToOwned` would
175                 // also be `#[rustc_conversion_suggestion]`, if not for
176                 // method-probing false-positives and -negatives (respectively).
177                 //
178                 // FIXME? Other potential candidate methods: `as_ref` and
179                 // `as_mut`?
180                 .find(|a| a.check_name("rustc_conversion_suggestion")).is_some()
181         });
182
183         methods
184     }
185
186     // This function checks if the method isn't static and takes other arguments than `self`.
187     fn has_no_input_arg(&self, method: &AssociatedItem) -> bool {
188         match method.def() {
189             Def::Method(def_id) => {
190                 self.tcx.fn_sig(def_id).inputs().skip_binder().len() == 1
191             }
192             _ => false,
193         }
194     }
195
196     /// Identify some cases where `as_ref()` would be appropriate and suggest it.
197     ///
198     /// Given the following code:
199     /// ```
200     /// struct Foo;
201     /// fn takes_ref(_: &Foo) {}
202     /// let ref opt = Some(Foo);
203     ///
204     /// opt.map(|arg| takes_ref(arg));
205     /// ```
206     /// Suggest using `opt.as_ref().map(|arg| takes_ref(arg));` instead.
207     ///
208     /// It only checks for `Option` and `Result` and won't work with
209     /// ```
210     /// opt.map(|arg| { takes_ref(arg) });
211     /// ```
212     fn can_use_as_ref(
213         &self,
214         expr: &hir::Expr,
215     ) -> Option<(Span, &'static str, String)> {
216         if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
217             if let hir::def::Def::Local(id) = path.def {
218                 let parent = self.tcx.hir().get_parent_node(id);
219                 if let Some(Node::Expr(hir::Expr {
220                     hir_id,
221                     node: hir::ExprKind::Closure(_, decl, ..),
222                     ..
223                 })) = self.tcx.hir().find(parent) {
224                     let parent = self.tcx.hir().get_parent_node_by_hir_id(*hir_id);
225                     if let (Some(Node::Expr(hir::Expr {
226                         node: hir::ExprKind::MethodCall(path, span, expr),
227                         ..
228                     })), 1) = (self.tcx.hir().find_by_hir_id(parent), decl.inputs.len()) {
229                         let self_ty = self.tables.borrow().node_type(expr[0].hir_id);
230                         let self_ty = format!("{:?}", self_ty);
231                         let name = path.ident.as_str();
232                         let is_as_ref_able = (
233                             self_ty.starts_with("&std::option::Option") ||
234                             self_ty.starts_with("&std::result::Result") ||
235                             self_ty.starts_with("std::option::Option") ||
236                             self_ty.starts_with("std::result::Result")
237                         ) && (name == "map" || name == "and_then");
238                         match (is_as_ref_able, self.sess().source_map().span_to_snippet(*span)) {
239                             (true, Ok(src)) => {
240                                 return Some((*span, "consider using `as_ref` instead",
241                                              format!("as_ref().{}", src)));
242                             },
243                             _ => ()
244                         }
245                     }
246                 }
247             }
248         }
249         None
250     }
251
252     /// This function is used to determine potential "simple" improvements or users' errors and
253     /// provide them useful help. For example:
254     ///
255     /// ```
256     /// fn some_fn(s: &str) {}
257     ///
258     /// let x = "hey!".to_owned();
259     /// some_fn(x); // error
260     /// ```
261     ///
262     /// No need to find every potential function which could make a coercion to transform a
263     /// `String` into a `&str` since a `&` would do the trick!
264     ///
265     /// In addition of this check, it also checks between references mutability state. If the
266     /// expected is mutable but the provided isn't, maybe we could just say "Hey, try with
267     /// `&mut`!".
268     pub fn check_ref(&self,
269                  expr: &hir::Expr,
270                  checked_ty: Ty<'tcx>,
271                  expected: Ty<'tcx>)
272                  -> Option<(Span, &'static str, String)> {
273         let cm = self.sess().source_map();
274         // Use the callsite's span if this is a macro call. #41858
275         let sp = cm.call_span_if_macro(expr.span);
276         if !cm.span_to_filename(sp).is_real() {
277             return None;
278         }
279
280         match (&expected.sty, &checked_ty.sty) {
281             (&ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.sty, &check.sty) {
282                 (&ty::Str, &ty::Array(arr, _)) |
283                 (&ty::Str, &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
284                     if let hir::ExprKind::Lit(_) = expr.node {
285                         if let Ok(src) = cm.span_to_snippet(sp) {
286                             if src.starts_with("b\"") {
287                                 return Some((sp,
288                                              "consider removing the leading `b`",
289                                              src[1..].to_string()));
290                             }
291                         }
292                     }
293                 },
294                 (&ty::Array(arr, _), &ty::Str) |
295                 (&ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
296                     if let hir::ExprKind::Lit(_) = expr.node {
297                         if let Ok(src) = cm.span_to_snippet(sp) {
298                             if src.starts_with("\"") {
299                                 return Some((sp,
300                                              "consider adding a leading `b`",
301                                              format!("b{}", src)));
302                             }
303                         }
304                     }
305                 }
306                 _ => {}
307             },
308             (&ty::Ref(_, _, mutability), _) => {
309                 // Check if it can work when put into a ref. For example:
310                 //
311                 // ```
312                 // fn bar(x: &mut i32) {}
313                 //
314                 // let x = 0u32;
315                 // bar(&x); // error, expected &mut
316                 // ```
317                 let ref_ty = match mutability {
318                     hir::Mutability::MutMutable => self.tcx.mk_mut_ref(
319                                                        self.tcx.mk_region(ty::ReStatic),
320                                                        checked_ty),
321                     hir::Mutability::MutImmutable => self.tcx.mk_imm_ref(
322                                                        self.tcx.mk_region(ty::ReStatic),
323                                                        checked_ty),
324                 };
325                 if self.can_coerce(ref_ty, expected) {
326                     if let Ok(src) = cm.span_to_snippet(sp) {
327                         let needs_parens = match expr.node {
328                             // parenthesize if needed (Issue #46756)
329                             hir::ExprKind::Cast(_, _) |
330                             hir::ExprKind::Binary(_, _, _) => true,
331                             // parenthesize borrows of range literals (Issue #54505)
332                             _ if self.is_range_literal(expr) => true,
333                             _ => false,
334                         };
335                         let sugg_expr = if needs_parens {
336                             format!("({})", src)
337                         } else {
338                             src
339                         };
340
341                         if let Some(sugg) = self.can_use_as_ref(expr) {
342                             return Some(sugg);
343                         }
344                         return Some(match mutability {
345                             hir::Mutability::MutMutable => {
346                                 (sp, "consider mutably borrowing here", format!("&mut {}",
347                                                                                 sugg_expr))
348                             }
349                             hir::Mutability::MutImmutable => {
350                                 (sp, "consider borrowing here", format!("&{}", sugg_expr))
351                             }
352                         });
353                     }
354                 }
355             }
356             (_, &ty::Ref(_, checked, _)) => {
357                 // We have `&T`, check if what was expected was `T`. If so,
358                 // we may want to suggest adding a `*`, or removing
359                 // a `&`.
360                 //
361                 // (But, also check the `expn_info()` to see if this is
362                 // a macro; if so, it's hard to extract the text and make a good
363                 // suggestion, so don't bother.)
364                 if self.infcx.can_sub(self.param_env, checked, &expected).is_ok() &&
365                    sp.ctxt().outer().expn_info().is_none() {
366                     match expr.node {
367                         // Maybe remove `&`?
368                         hir::ExprKind::AddrOf(_, ref expr) => {
369                             if !cm.span_to_filename(expr.span).is_real() {
370                                 if let Ok(code) = cm.span_to_snippet(sp) {
371                                     if code.chars().next() == Some('&') {
372                                         return Some((
373                                             sp,
374                                             "consider removing the borrow",
375                                             code[1..].to_string()),
376                                         );
377                                     }
378                                 }
379                                 return None;
380                             }
381                             if let Ok(code) = cm.span_to_snippet(expr.span) {
382                                 return Some((sp, "consider removing the borrow", code));
383                             }
384                         }
385
386                         // Maybe add `*`? Only if `T: Copy`.
387                         _ => {
388                             if self.infcx.type_is_copy_modulo_regions(self.param_env,
389                                                                       checked,
390                                                                       sp) {
391                                 // do not suggest if the span comes from a macro (#52783)
392                                 if let (Ok(code),
393                                         true) = (cm.span_to_snippet(sp), sp == expr.span) {
394                                     return Some((
395                                         sp,
396                                         "consider dereferencing the borrow",
397                                         format!("*{}", code),
398                                     ));
399                                 }
400                             }
401                         }
402                     }
403                 }
404             }
405             _ => {}
406         }
407         None
408     }
409
410     /// This function checks if the specified expression is a built-in range literal.
411     /// (See: `LoweringContext::lower_expr()` in `src/librustc/hir/lowering.rs`).
412     fn is_range_literal(&self, expr: &hir::Expr) -> bool {
413         use hir::{Path, QPath, ExprKind, TyKind};
414
415         // We support `::std::ops::Range` and `::core::ops::Range` prefixes
416         let is_range_path = |path: &Path| {
417             let mut segs = path.segments.iter()
418                 .map(|seg| seg.ident.as_str());
419
420             if let (Some(root), Some(std_core), Some(ops), Some(range), None) =
421                 (segs.next(), segs.next(), segs.next(), segs.next(), segs.next())
422             {
423                 // "{{root}}" is the equivalent of `::` prefix in Path
424                 root == "{{root}}" && (std_core == "std" || std_core == "core")
425                     && ops == "ops" && range.starts_with("Range")
426             } else {
427                 false
428             }
429         };
430
431         let span_is_range_literal = |span: &Span| {
432             // Check whether a span corresponding to a range expression
433             // is a range literal, rather than an explicit struct or `new()` call.
434             let source_map = self.tcx.sess.source_map();
435             let end_point = source_map.end_point(*span);
436
437             if let Ok(end_string) = source_map.span_to_snippet(end_point) {
438                 !(end_string.ends_with("}") || end_string.ends_with(")"))
439             } else {
440                 false
441             }
442         };
443
444         match expr.node {
445             // All built-in range literals but `..=` and `..` desugar to Structs
446             ExprKind::Struct(ref qpath, _, _) => {
447                 if let QPath::Resolved(None, ref path) = **qpath {
448                     return is_range_path(&path) && span_is_range_literal(&expr.span);
449                 }
450             }
451             // `..` desugars to its struct path
452             ExprKind::Path(QPath::Resolved(None, ref path)) => {
453                 return is_range_path(&path) && span_is_range_literal(&expr.span);
454             }
455
456             // `..=` desugars into `::std::ops::RangeInclusive::new(...)`
457             ExprKind::Call(ref func, _) => {
458                 if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
459                     if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node {
460                         let call_to_new = segment.ident.as_str() == "new";
461
462                         return is_range_path(&path) && span_is_range_literal(&expr.span)
463                             && call_to_new;
464                     }
465                 }
466             }
467
468             _ => {}
469         }
470
471         false
472     }
473
474     pub fn check_for_cast(
475         &self,
476         err: &mut DiagnosticBuilder<'tcx>,
477         expr: &hir::Expr,
478         checked_ty: Ty<'tcx>,
479         expected_ty: Ty<'tcx>,
480     ) -> bool {
481         let parent_id = self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
482         if let Some(parent) = self.tcx.hir().find_by_hir_id(parent_id) {
483             // Shouldn't suggest `.into()` on `const`s.
484             if let Node::Item(Item { node: ItemKind::Const(_, _), .. }) = parent {
485                 // FIXME(estebank): modify once we decide to suggest `as` casts
486                 return false;
487             }
488         };
489
490         let will_truncate = "will truncate the source value";
491         let depending_on_isize = "will truncate or zero-extend depending on the bit width of \
492                                   `isize`";
493         let depending_on_usize = "will truncate or zero-extend depending on the bit width of \
494                                   `usize`";
495         let will_sign_extend = "will sign-extend the source value";
496         let will_zero_extend = "will zero-extend the source value";
497
498         // If casting this expression to a given numeric type would be appropriate in case of a type
499         // mismatch.
500         //
501         // We want to minimize the amount of casting operations that are suggested, as it can be a
502         // lossy operation with potentially bad side effects, so we only suggest when encountering
503         // an expression that indicates that the original type couldn't be directly changed.
504         //
505         // For now, don't suggest casting with `as`.
506         let can_cast = false;
507
508         let mut prefix = String::new();
509         if let Some(hir::Node::Expr(hir::Expr {
510             node: hir::ExprKind::Struct(_, fields, _),
511             ..
512         })) = self.tcx.hir().find_by_hir_id(self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id)) {
513             // `expr` is a literal field for a struct, only suggest if appropriate
514             for field in fields {
515                 if field.expr.hir_id == expr.hir_id && field.is_shorthand {
516                     // This is a field literal
517                     prefix = format!("{}: ", field.ident);
518                     break;
519                 }
520             }
521             if &prefix == "" {
522                 // Likely a field was meant, but this field wasn't found. Do not suggest anything.
523                 return false;
524             }
525         }
526
527         let needs_paren = expr.precedence().order() < (PREC_POSTFIX as i8);
528
529         if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
530             let msg = format!("you can cast an `{}` to `{}`", checked_ty, expected_ty);
531             let cast_suggestion = format!(
532                 "{}{}{}{} as {}",
533                 prefix,
534                 if needs_paren { "(" } else { "" },
535                 src,
536                 if needs_paren { ")" } else { "" },
537                 expected_ty,
538             );
539             let into_suggestion = format!(
540                 "{}{}{}{}.into()",
541                 prefix,
542                 if needs_paren { "(" } else { "" },
543                 src,
544                 if needs_paren { ")" } else { "" },
545             );
546             let literal_is_ty_suffixed = |expr: &hir::Expr| {
547                 if let hir::ExprKind::Lit(lit) = &expr.node {
548                     lit.node.is_suffixed()
549                 } else {
550                     false
551                 }
552             };
553
554             let into_sugg = into_suggestion.clone();
555             let suggest_to_change_suffix_or_into = |err: &mut DiagnosticBuilder<'_>,
556                                                     note: Option<&str>| {
557                 let suggest_msg = if literal_is_ty_suffixed(expr) {
558                     format!(
559                         "change the type of the numeric literal from `{}` to `{}`",
560                         checked_ty,
561                         expected_ty,
562                     )
563                 } else {
564                     match note {
565                         Some(note) => format!("{}, which {}", msg, note),
566                         _ => format!("{} in a lossless way", msg),
567                     }
568                 };
569
570                 let suffix_suggestion = format!(
571                     "{}{}{}{}",
572                     if needs_paren { "(" } else { "" },
573                     src.trim_end_matches(&checked_ty.to_string()),
574                     expected_ty,
575                     if needs_paren { ")" } else { "" },
576                 );
577
578                 err.span_suggestion(
579                     expr.span,
580                     &suggest_msg,
581                     if literal_is_ty_suffixed(expr) {
582                         suffix_suggestion
583                     } else {
584                         into_sugg
585                     },
586                     Applicability::MachineApplicable,
587                 );
588             };
589
590             match (&expected_ty.sty, &checked_ty.sty) {
591                 (&ty::Int(ref exp), &ty::Int(ref found)) => {
592                     match (found.bit_width(), exp.bit_width()) {
593                         (Some(found), Some(exp)) if found > exp => {
594                             if can_cast {
595                                 err.span_suggestion(
596                                     expr.span,
597                                     &format!("{}, which {}", msg, will_truncate),
598                                     cast_suggestion,
599                                     Applicability::MaybeIncorrect // lossy conversion
600                                 );
601                             }
602                         }
603                         (None, _) | (_, None) => {
604                             if can_cast {
605                                 err.span_suggestion(
606                                     expr.span,
607                                     &format!("{}, which {}", msg, depending_on_isize),
608                                     cast_suggestion,
609                                     Applicability::MaybeIncorrect // lossy conversion
610                                 );
611                             }
612                         }
613                         _ => {
614                             suggest_to_change_suffix_or_into(
615                                 err,
616                                 Some(will_sign_extend),
617                             );
618                         }
619                     }
620                     true
621                 }
622                 (&ty::Uint(ref exp), &ty::Uint(ref found)) => {
623                     match (found.bit_width(), exp.bit_width()) {
624                         (Some(found), Some(exp)) if found > exp => {
625                             if can_cast {
626                                 err.span_suggestion(
627                                     expr.span,
628                                     &format!("{}, which {}", msg, will_truncate),
629                                     cast_suggestion,
630                                     Applicability::MaybeIncorrect  // lossy conversion
631                                 );
632                             }
633                         }
634                         (None, _) | (_, None) => {
635                             if can_cast {
636                                 err.span_suggestion(
637                                     expr.span,
638                                     &format!("{}, which {}", msg, depending_on_usize),
639                                     cast_suggestion,
640                                     Applicability::MaybeIncorrect  // lossy conversion
641                                 );
642                             }
643                         }
644                         _ => {
645                            suggest_to_change_suffix_or_into(
646                                err,
647                                Some(will_zero_extend),
648                            );
649                         }
650                     }
651                     true
652                 }
653                 (&ty::Int(ref exp), &ty::Uint(ref found)) => {
654                     if can_cast {
655                         match (found.bit_width(), exp.bit_width()) {
656                             (Some(found), Some(exp)) if found > exp - 1 => {
657                                 err.span_suggestion(
658                                     expr.span,
659                                     &format!("{}, which {}", msg, will_truncate),
660                                     cast_suggestion,
661                                     Applicability::MaybeIncorrect  // lossy conversion
662                                 );
663                             }
664                             (None, None) => {
665                                 err.span_suggestion(
666                                     expr.span,
667                                     &format!("{}, which {}", msg, will_truncate),
668                                     cast_suggestion,
669                                     Applicability::MaybeIncorrect  // lossy conversion
670                                 );
671                             }
672                             (None, _) => {
673                                 err.span_suggestion(
674                                     expr.span,
675                                     &format!("{}, which {}", msg, depending_on_isize),
676                                     cast_suggestion,
677                                     Applicability::MaybeIncorrect  // lossy conversion
678                                 );
679                             }
680                             (_, None) => {
681                                 err.span_suggestion(
682                                     expr.span,
683                                     &format!("{}, which {}", msg, depending_on_usize),
684                                     cast_suggestion,
685                                     Applicability::MaybeIncorrect  // lossy conversion
686                                 );
687                             }
688                             _ => {
689                                 err.span_suggestion(
690                                     expr.span,
691                                     &format!("{}, which {}", msg, will_zero_extend),
692                                     cast_suggestion,
693                                     Applicability::MachineApplicable
694                                 );
695                             }
696                         }
697                     }
698                     true
699                 }
700                 (&ty::Uint(ref exp), &ty::Int(ref found)) => {
701                     if can_cast {
702                         match (found.bit_width(), exp.bit_width()) {
703                             (Some(found), Some(exp)) if found - 1 > exp => {
704                                 err.span_suggestion(
705                                     expr.span,
706                                     &format!("{}, which {}", msg, will_truncate),
707                                     cast_suggestion,
708                                     Applicability::MaybeIncorrect  // lossy conversion
709                                 );
710                             }
711                             (None, None) => {
712                                 err.span_suggestion(
713                                     expr.span,
714                                     &format!("{}, which {}", msg, will_sign_extend),
715                                     cast_suggestion,
716                                     Applicability::MachineApplicable  // lossy conversion
717                                 );
718                             }
719                             (None, _) => {
720                                 err.span_suggestion(
721                                     expr.span,
722                                     &format!("{}, which {}", msg, depending_on_usize),
723                                     cast_suggestion,
724                                     Applicability::MaybeIncorrect  // lossy conversion
725                                 );
726                             }
727                             (_, None) => {
728                                 err.span_suggestion(
729                                     expr.span,
730                                     &format!("{}, which {}", msg, depending_on_isize),
731                                     cast_suggestion,
732                                     Applicability::MaybeIncorrect  // lossy conversion
733                                 );
734                             }
735                             _ => {
736                                 err.span_suggestion(
737                                     expr.span,
738                                     &format!("{}, which {}", msg, will_sign_extend),
739                                     cast_suggestion,
740                                     Applicability::MachineApplicable
741                                 );
742                             }
743                         }
744                     }
745                     true
746                 }
747                 (&ty::Float(ref exp), &ty::Float(ref found)) => {
748                     if found.bit_width() < exp.bit_width() {
749                        suggest_to_change_suffix_or_into(
750                            err,
751                            None,
752                        );
753                     } else if can_cast {
754                         err.span_suggestion(
755                             expr.span,
756                             &format!("{}, producing the closest possible value", msg),
757                             cast_suggestion,
758                             Applicability::MaybeIncorrect  // lossy conversion
759                         );
760                     }
761                     true
762                 }
763                 (&ty::Uint(_), &ty::Float(_)) | (&ty::Int(_), &ty::Float(_)) => {
764                     if can_cast {
765                         err.span_suggestion(
766                             expr.span,
767                             &format!("{}, rounding the float towards zero", msg),
768                             cast_suggestion,
769                             Applicability::MaybeIncorrect  // lossy conversion
770                         );
771                         err.warn("casting here will cause undefined behavior if the rounded value \
772                                   cannot be represented by the target integer type, including \
773                                   `Inf` and `NaN` (this is a bug and will be fixed)");
774                     }
775                     true
776                 }
777                 (&ty::Float(ref exp), &ty::Uint(ref found)) => {
778                     // if `found` is `None` (meaning found is `usize`), don't suggest `.into()`
779                     if exp.bit_width() > found.bit_width().unwrap_or(256) {
780                         err.span_suggestion(
781                             expr.span,
782                             &format!("{}, producing the floating point representation of the \
783                                       integer",
784                                      msg),
785                             into_suggestion,
786                             Applicability::MachineApplicable
787                         );
788                     } else if can_cast {
789                         err.span_suggestion(
790                             expr.span,
791                             &format!("{}, producing the floating point representation of the \
792                                       integer, rounded if necessary",
793                                      msg),
794                             cast_suggestion,
795                             Applicability::MaybeIncorrect  // lossy conversion
796                         );
797                     }
798                     true
799                 }
800                 (&ty::Float(ref exp), &ty::Int(ref found)) => {
801                     // if `found` is `None` (meaning found is `isize`), don't suggest `.into()`
802                     if exp.bit_width() > found.bit_width().unwrap_or(256) {
803                         err.span_suggestion(
804                             expr.span,
805                             &format!("{}, producing the floating point representation of the \
806                                       integer",
807                                      msg),
808                             into_suggestion,
809                             Applicability::MachineApplicable
810                         );
811                     } else if can_cast {
812                         err.span_suggestion(
813                             expr.span,
814                             &format!("{}, producing the floating point representation of the \
815                                       integer, rounded if necessary",
816                                      msg),
817                             cast_suggestion,
818                             Applicability::MaybeIncorrect  // lossy conversion
819                         );
820                     }
821                     true
822                 }
823                 _ => false,
824             }
825         } else {
826             false
827         }
828     }
829 }