]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/types.rs
8b44157a4058e596b29e8ba69ce3164dd84e1666
[rust.git] / src / librustc_lint / types.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(non_snake_case)]
12
13 use rustc::hir::def_id::DefId;
14 use rustc::ty::subst::Substs;
15 use rustc::ty::{self, Ty, TyCtxt};
16 use rustc::ty::layout::{Layout, Primitive};
17 use rustc::traits::ProjectionMode;
18 use middle::const_val::ConstVal;
19 use rustc_const_eval::eval_const_expr_partial;
20 use rustc_const_eval::EvalHint::ExprTypeChecked;
21 use util::common::slice_pat;
22 use util::nodemap::{FnvHashSet};
23 use lint::{LateContext, LintContext, LintArray};
24 use lint::{LintPass, LateLintPass};
25
26 use std::cmp;
27 use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
28
29 use syntax::ast;
30 use syntax::abi::Abi;
31 use syntax::attr;
32 use syntax_pos::Span;
33 use syntax::codemap;
34
35 use rustc::hir;
36
37 register_long_diagnostics! {
38 E0519: r##"
39 It is not allowed to negate an unsigned integer.
40 You can negate a signed integer and cast it to an
41 unsigned integer or use the `!` operator.
42
43 ```
44 let x: usize = -1isize as usize;
45 let y: usize = !0;
46 assert_eq!(x, y);
47 ```
48
49 Alternatively you can use the `Wrapping` newtype
50 or the `wrapping_neg` operation that all
51 integral types support:
52
53 ```
54 use std::num::Wrapping;
55 let x: Wrapping<usize> = -Wrapping(1);
56 let Wrapping(x) = x;
57 let y: usize = 1.wrapping_neg();
58 assert_eq!(x, y);
59 ```
60
61 "##
62 }
63
64 declare_lint! {
65     UNUSED_COMPARISONS,
66     Warn,
67     "comparisons made useless by limits of the types involved"
68 }
69
70 declare_lint! {
71     OVERFLOWING_LITERALS,
72     Warn,
73     "literal out of range for its type"
74 }
75
76 declare_lint! {
77     EXCEEDING_BITSHIFTS,
78     Deny,
79     "shift exceeds the type's number of bits"
80 }
81
82 declare_lint! {
83     VARIANT_SIZE_DIFFERENCES,
84     Allow,
85     "detects enums with widely varying variant sizes"
86 }
87
88 #[derive(Copy, Clone)]
89 pub struct TypeLimits {
90     /// Id of the last visited negated expression
91     negated_expr_id: ast::NodeId,
92 }
93
94 impl TypeLimits {
95     pub fn new() -> TypeLimits {
96         TypeLimits {
97             negated_expr_id: !0,
98         }
99     }
100 }
101
102 impl LintPass for TypeLimits {
103     fn get_lints(&self) -> LintArray {
104         lint_array!(UNUSED_COMPARISONS, OVERFLOWING_LITERALS, EXCEEDING_BITSHIFTS)
105     }
106 }
107
108 impl LateLintPass for TypeLimits {
109     fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
110         match e.node {
111             hir::ExprUnary(hir::UnNeg, ref expr) => {
112                 if let hir::ExprLit(ref lit) = expr.node {
113                     match lit.node {
114                         ast::LitKind::Int(_, ast::LitIntType::Unsigned(_)) => {
115                             forbid_unsigned_negation(cx, e.span);
116                         },
117                         ast::LitKind::Int(_, ast::LitIntType::Unsuffixed) => {
118                             if let ty::TyUint(_) = cx.tcx.node_id_to_type(e.id).sty {
119                                 forbid_unsigned_negation(cx, e.span);
120                             }
121                         },
122                         _ => ()
123                     }
124                 } else {
125                     let t = cx.tcx.node_id_to_type(expr.id);
126                     if let ty::TyUint(_) = t.sty {
127                         forbid_unsigned_negation(cx, e.span);
128                     }
129                 }
130                 // propagate negation, if the negation itself isn't negated
131                 if self.negated_expr_id != e.id {
132                     self.negated_expr_id = expr.id;
133                 }
134             },
135             hir::ExprBinary(binop, ref l, ref r) => {
136                 if is_comparison(binop) && !check_limits(cx.tcx, binop, &l, &r) {
137                     cx.span_lint(UNUSED_COMPARISONS, e.span,
138                                  "comparison is useless due to type limits");
139                 }
140
141                 if binop.node.is_shift() {
142                     let opt_ty_bits = match cx.tcx.node_id_to_type(l.id).sty {
143                         ty::TyInt(t) => Some(int_ty_bits(t, cx.sess().target.int_type)),
144                         ty::TyUint(t) => Some(uint_ty_bits(t, cx.sess().target.uint_type)),
145                         _ => None
146                     };
147
148                     if let Some(bits) = opt_ty_bits {
149                         let exceeding = if let hir::ExprLit(ref lit) = r.node {
150                             if let ast::LitKind::Int(shift, _) = lit.node { shift >= bits }
151                             else { false }
152                         } else {
153                             match eval_const_expr_partial(cx.tcx, &r, ExprTypeChecked, None) {
154                                 Ok(ConstVal::Integral(i)) => {
155                                     i.is_negative() || i.to_u64()
156                                                         .map(|i| i >= bits)
157                                                         .unwrap_or(true)
158                                 },
159                                 _ => { false }
160                             }
161                         };
162                         if exceeding {
163                             cx.span_lint(EXCEEDING_BITSHIFTS, e.span,
164                                          "bitshift exceeds the type's number of bits");
165                         }
166                     };
167                 }
168             },
169             hir::ExprLit(ref lit) => {
170                 match cx.tcx.node_id_to_type(e.id).sty {
171                     ty::TyInt(t) => {
172                         match lit.node {
173                             ast::LitKind::Int(v, ast::LitIntType::Signed(_)) |
174                             ast::LitKind::Int(v, ast::LitIntType::Unsuffixed) => {
175                                 let int_type = if let ast::IntTy::Is = t {
176                                     cx.sess().target.int_type
177                                 } else {
178                                     t
179                                 };
180                                 let (_, max) = int_ty_range(int_type);
181                                 let negative = self.negated_expr_id == e.id;
182
183                                 // Detect literal value out of range [min, max] inclusive
184                                 // avoiding use of -min to prevent overflow/panic
185                                 if (negative && v > max as u64 + 1) ||
186                                    (!negative && v > max as u64) {
187                                     cx.span_lint(OVERFLOWING_LITERALS, e.span,
188                                                  &format!("literal out of range for {:?}", t));
189                                     return;
190                                 }
191                             }
192                             _ => bug!()
193                         };
194                     },
195                     ty::TyUint(t) => {
196                         let uint_type = if let ast::UintTy::Us = t {
197                             cx.sess().target.uint_type
198                         } else {
199                             t
200                         };
201                         let (min, max) = uint_ty_range(uint_type);
202                         let lit_val: u64 = match lit.node {
203                             // _v is u8, within range by definition
204                             ast::LitKind::Byte(_v) => return,
205                             ast::LitKind::Int(v, _) => v,
206                             _ => bug!()
207                         };
208                         if lit_val < min || lit_val > max {
209                             cx.span_lint(OVERFLOWING_LITERALS, e.span,
210                                          &format!("literal out of range for {:?}", t));
211                         }
212                     },
213                     ty::TyFloat(t) => {
214                         let (min, max) = float_ty_range(t);
215                         let lit_val: f64 = match lit.node {
216                             ast::LitKind::Float(ref v, _) |
217                             ast::LitKind::FloatUnsuffixed(ref v) => {
218                                 match v.parse() {
219                                     Ok(f) => f,
220                                     Err(_) => return
221                                 }
222                             }
223                             _ => bug!()
224                         };
225                         if lit_val < min || lit_val > max {
226                             cx.span_lint(OVERFLOWING_LITERALS, e.span,
227                                          &format!("literal out of range for {:?}", t));
228                         }
229                     },
230                     _ => ()
231                 };
232             },
233             _ => ()
234         };
235
236         fn is_valid<T:cmp::PartialOrd>(binop: hir::BinOp, v: T,
237                                 min: T, max: T) -> bool {
238             match binop.node {
239                 hir::BiLt => v >  min && v <= max,
240                 hir::BiLe => v >= min && v <  max,
241                 hir::BiGt => v >= min && v <  max,
242                 hir::BiGe => v >  min && v <= max,
243                 hir::BiEq | hir::BiNe => v >= min && v <= max,
244                 _ => bug!()
245             }
246         }
247
248         fn rev_binop(binop: hir::BinOp) -> hir::BinOp {
249             codemap::respan(binop.span, match binop.node {
250                 hir::BiLt => hir::BiGt,
251                 hir::BiLe => hir::BiGe,
252                 hir::BiGt => hir::BiLt,
253                 hir::BiGe => hir::BiLe,
254                 _ => return binop
255             })
256         }
257
258         // for isize & usize, be conservative with the warnings, so that the
259         // warnings are consistent between 32- and 64-bit platforms
260         fn int_ty_range(int_ty: ast::IntTy) -> (i64, i64) {
261             match int_ty {
262                 ast::IntTy::Is => (i64::MIN,        i64::MAX),
263                 ast::IntTy::I8 =>    (i8::MIN  as i64, i8::MAX  as i64),
264                 ast::IntTy::I16 =>   (i16::MIN as i64, i16::MAX as i64),
265                 ast::IntTy::I32 =>   (i32::MIN as i64, i32::MAX as i64),
266                 ast::IntTy::I64 =>   (i64::MIN,        i64::MAX)
267             }
268         }
269
270         fn uint_ty_range(uint_ty: ast::UintTy) -> (u64, u64) {
271             match uint_ty {
272                 ast::UintTy::Us => (u64::MIN,         u64::MAX),
273                 ast::UintTy::U8 =>    (u8::MIN   as u64, u8::MAX   as u64),
274                 ast::UintTy::U16 =>   (u16::MIN  as u64, u16::MAX  as u64),
275                 ast::UintTy::U32 =>   (u32::MIN  as u64, u32::MAX  as u64),
276                 ast::UintTy::U64 =>   (u64::MIN,         u64::MAX)
277             }
278         }
279
280         fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
281             match float_ty {
282                 ast::FloatTy::F32 => (f32::MIN as f64, f32::MAX as f64),
283                 ast::FloatTy::F64 => (f64::MIN,        f64::MAX)
284             }
285         }
286
287         fn int_ty_bits(int_ty: ast::IntTy, target_int_ty: ast::IntTy) -> u64 {
288             match int_ty {
289                 ast::IntTy::Is => int_ty_bits(target_int_ty, target_int_ty),
290                 ast::IntTy::I8 => 8,
291                 ast::IntTy::I16 => 16 as u64,
292                 ast::IntTy::I32 => 32,
293                 ast::IntTy::I64 => 64,
294             }
295         }
296
297         fn uint_ty_bits(uint_ty: ast::UintTy, target_uint_ty: ast::UintTy) -> u64 {
298             match uint_ty {
299                 ast::UintTy::Us => uint_ty_bits(target_uint_ty, target_uint_ty),
300                 ast::UintTy::U8 => 8,
301                 ast::UintTy::U16 => 16,
302                 ast::UintTy::U32 => 32,
303                 ast::UintTy::U64 => 64,
304             }
305         }
306
307         fn check_limits<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
308                                   binop: hir::BinOp,
309                                   l: &hir::Expr,
310                                   r: &hir::Expr) -> bool {
311             let (lit, expr, swap) = match (&l.node, &r.node) {
312                 (&hir::ExprLit(_), _) => (l, r, true),
313                 (_, &hir::ExprLit(_)) => (r, l, false),
314                 _ => return true
315             };
316             // Normalize the binop so that the literal is always on the RHS in
317             // the comparison
318             let norm_binop = if swap {
319                 rev_binop(binop)
320             } else {
321                 binop
322             };
323             match tcx.node_id_to_type(expr.id).sty {
324                 ty::TyInt(int_ty) => {
325                     let (min, max) = int_ty_range(int_ty);
326                     let lit_val: i64 = match lit.node {
327                         hir::ExprLit(ref li) => match li.node {
328                             ast::LitKind::Int(v, ast::LitIntType::Signed(_)) |
329                             ast::LitKind::Int(v, ast::LitIntType::Unsuffixed) => v as i64,
330                             _ => return true
331                         },
332                         _ => bug!()
333                     };
334                     is_valid(norm_binop, lit_val, min, max)
335                 }
336                 ty::TyUint(uint_ty) => {
337                     let (min, max): (u64, u64) = uint_ty_range(uint_ty);
338                     let lit_val: u64 = match lit.node {
339                         hir::ExprLit(ref li) => match li.node {
340                             ast::LitKind::Int(v, _) => v,
341                             _ => return true
342                         },
343                         _ => bug!()
344                     };
345                     is_valid(norm_binop, lit_val, min, max)
346                 }
347                 _ => true
348             }
349         }
350
351         fn is_comparison(binop: hir::BinOp) -> bool {
352             match binop.node {
353                 hir::BiEq | hir::BiLt | hir::BiLe |
354                 hir::BiNe | hir::BiGe | hir::BiGt => true,
355                 _ => false
356             }
357         }
358
359         fn forbid_unsigned_negation(cx: &LateContext, span: Span) {
360             cx.sess()
361               .struct_span_err_with_code(span, "unary negation of unsigned integer", "E0519")
362               .span_help(span, "use a cast or the `!` operator")
363               .emit();
364         }
365     }
366 }
367
368 declare_lint! {
369     IMPROPER_CTYPES,
370     Warn,
371     "proper use of libc types in foreign modules"
372 }
373
374 struct ImproperCTypesVisitor<'a, 'tcx: 'a> {
375     cx: &'a LateContext<'a, 'tcx>
376 }
377
378 enum FfiResult {
379     FfiSafe,
380     FfiUnsafe(&'static str),
381     FfiBadStruct(DefId, &'static str),
382     FfiBadEnum(DefId, &'static str)
383 }
384
385 /// Check if this enum can be safely exported based on the
386 /// "nullable pointer optimization". Currently restricted
387 /// to function pointers and references, but could be
388 /// expanded to cover NonZero raw pointers and newtypes.
389 /// FIXME: This duplicates code in trans.
390 fn is_repr_nullable_ptr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
391                                   def: ty::AdtDef<'tcx>,
392                                   substs: &Substs<'tcx>)
393                                   -> bool {
394     if def.variants.len() == 2 {
395         let data_idx;
396
397         if def.variants[0].fields.is_empty() {
398             data_idx = 1;
399         } else if def.variants[1].fields.is_empty() {
400             data_idx = 0;
401         } else {
402             return false;
403         }
404
405         if def.variants[data_idx].fields.len() == 1 {
406             match def.variants[data_idx].fields[0].ty(tcx, substs).sty {
407                 ty::TyFnPtr(_) => { return true; }
408                 ty::TyRef(..) => { return true; }
409                 _ => { }
410             }
411         }
412     }
413     false
414 }
415
416 impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
417     /// Check if the given type is "ffi-safe" (has a stable, well-defined
418     /// representation which can be exported to C code).
419     fn check_type_for_ffi(&self,
420                           cache: &mut FnvHashSet<Ty<'tcx>>,
421                           ty: Ty<'tcx>)
422                           -> FfiResult {
423         use self::FfiResult::*;
424         let cx = self.cx.tcx;
425
426         // Protect against infinite recursion, for example
427         // `struct S(*mut S);`.
428         // FIXME: A recursion limit is necessary as well, for irregular
429         // recusive types.
430         if !cache.insert(ty) {
431             return FfiSafe;
432         }
433
434         match ty.sty {
435             ty::TyStruct(def, substs) => {
436                 if !cx.lookup_repr_hints(def.did).contains(&attr::ReprExtern) {
437                     return FfiUnsafe(
438                         "found struct without foreign-function-safe \
439                          representation annotation in foreign module, \
440                          consider adding a #[repr(C)] attribute to \
441                          the type");
442                 }
443
444                 // We can't completely trust repr(C) markings; make sure the
445                 // fields are actually safe.
446                 if def.struct_variant().fields.is_empty() {
447                     return FfiUnsafe(
448                         "found zero-size struct in foreign module, consider \
449                          adding a member to this struct");
450                 }
451
452                 for field in &def.struct_variant().fields {
453                     let field_ty = cx.normalize_associated_type(&field.ty(cx, substs));
454                     let r = self.check_type_for_ffi(cache, field_ty);
455                     match r {
456                         FfiSafe => {}
457                         FfiBadStruct(..) | FfiBadEnum(..) => { return r; }
458                         FfiUnsafe(s) => { return FfiBadStruct(def.did, s); }
459                     }
460                 }
461                 FfiSafe
462             }
463             ty::TyEnum(def, substs) => {
464                 if def.variants.is_empty() {
465                     // Empty enums are okay... although sort of useless.
466                     return FfiSafe
467                 }
468
469                 // Check for a repr() attribute to specify the size of the
470                 // discriminant.
471                 let repr_hints = cx.lookup_repr_hints(def.did);
472                 match slice_pat(&&**repr_hints) {
473                     &[] => {
474                         // Special-case types like `Option<extern fn()>`.
475                         if !is_repr_nullable_ptr(cx, def, substs) {
476                             return FfiUnsafe(
477                                 "found enum without foreign-function-safe \
478                                  representation annotation in foreign module, \
479                                  consider adding a #[repr(...)] attribute to \
480                                  the type")
481                         }
482                     }
483                     &[ref hint] => {
484                         if !hint.is_ffi_safe() {
485                             // FIXME: This shouldn't be reachable: we should check
486                             // this earlier.
487                             return FfiUnsafe(
488                                 "enum has unexpected #[repr(...)] attribute")
489                         }
490
491                         // Enum with an explicitly sized discriminant; either
492                         // a C-style enum or a discriminated union.
493
494                         // The layout of enum variants is implicitly repr(C).
495                         // FIXME: Is that correct?
496                     }
497                     _ => {
498                         // FIXME: This shouldn't be reachable: we should check
499                         // this earlier.
500                         return FfiUnsafe(
501                             "enum has too many #[repr(...)] attributes");
502                     }
503                 }
504
505                 // Check the contained variants.
506                 for variant in &def.variants {
507                     for field in &variant.fields {
508                         let arg = cx.normalize_associated_type(&field.ty(cx, substs));
509                         let r = self.check_type_for_ffi(cache, arg);
510                         match r {
511                             FfiSafe => {}
512                             FfiBadStruct(..) | FfiBadEnum(..) => { return r; }
513                             FfiUnsafe(s) => { return FfiBadEnum(def.did, s); }
514                         }
515                     }
516                 }
517                 FfiSafe
518             }
519
520             ty::TyChar => {
521                 FfiUnsafe("found Rust type `char` in foreign module, while \
522                            `u32` or `libc::wchar_t` should be used")
523             }
524
525             // Primitive types with a stable representation.
526             ty::TyBool | ty::TyInt(..) | ty::TyUint(..) |
527             ty::TyFloat(..) => FfiSafe,
528
529             ty::TyBox(..) => {
530                 FfiUnsafe("found Rust type Box<_> in foreign module, \
531                            consider using a raw pointer instead")
532             }
533
534             ty::TySlice(_) => {
535                 FfiUnsafe("found Rust slice type in foreign module, \
536                            consider using a raw pointer instead")
537             }
538
539             ty::TyTrait(..) => {
540                 FfiUnsafe("found Rust trait type in foreign module, \
541                            consider using a raw pointer instead")
542             }
543
544             ty::TyStr => {
545                 FfiUnsafe("found Rust type `str` in foreign module; \
546                            consider using a `*const libc::c_char`")
547             }
548
549             ty::TyTuple(_) => {
550                 FfiUnsafe("found Rust tuple type in foreign module; \
551                            consider using a struct instead`")
552             }
553
554             ty::TyRawPtr(ref m) | ty::TyRef(_, ref m) => {
555                 self.check_type_for_ffi(cache, m.ty)
556             }
557
558             ty::TyArray(ty, _) => {
559                 self.check_type_for_ffi(cache, ty)
560             }
561
562             ty::TyFnPtr(bare_fn) => {
563                 match bare_fn.abi {
564                     Abi::Rust |
565                     Abi::RustIntrinsic |
566                     Abi::PlatformIntrinsic |
567                     Abi::RustCall => {
568                         return FfiUnsafe(
569                             "found function pointer with Rust calling \
570                              convention in foreign module; consider using an \
571                              `extern` function pointer")
572                     }
573                     _ => {}
574                 }
575
576                 let sig = cx.erase_late_bound_regions(&bare_fn.sig);
577                 match sig.output {
578                     ty::FnDiverging => {}
579                     ty::FnConverging(output) => {
580                         if !output.is_nil() {
581                             let r = self.check_type_for_ffi(cache, output);
582                             match r {
583                                 FfiSafe => {}
584                                 _ => { return r; }
585                             }
586                         }
587                     }
588                 }
589                 for arg in sig.inputs {
590                     let r = self.check_type_for_ffi(cache, arg);
591                     match r {
592                         FfiSafe => {}
593                         _ => { return r; }
594                     }
595                 }
596                 FfiSafe
597             }
598
599             ty::TyParam(..) | ty::TyInfer(..) | ty::TyError |
600             ty::TyClosure(..) | ty::TyProjection(..) |
601             ty::TyFnDef(..) => {
602                 bug!("Unexpected type in foreign function")
603             }
604         }
605     }
606
607     fn check_type_for_ffi_and_report_errors(&mut self, sp: Span, ty: Ty<'tcx>) {
608         // it is only OK to use this function because extern fns cannot have
609         // any generic types right now:
610         let ty = self.cx.tcx.normalize_associated_type(&ty);
611
612         match self.check_type_for_ffi(&mut FnvHashSet(), ty) {
613             FfiResult::FfiSafe => {}
614             FfiResult::FfiUnsafe(s) => {
615                 self.cx.span_lint(IMPROPER_CTYPES, sp, s);
616             }
617             FfiResult::FfiBadStruct(_, s) => {
618                 // FIXME: This diagnostic is difficult to read, and doesn't
619                 // point at the relevant field.
620                 self.cx.span_lint(IMPROPER_CTYPES, sp,
621                     &format!("found non-foreign-function-safe member in \
622                               struct marked #[repr(C)]: {}", s));
623             }
624             FfiResult::FfiBadEnum(_, s) => {
625                 // FIXME: This diagnostic is difficult to read, and doesn't
626                 // point at the relevant variant.
627                 self.cx.span_lint(IMPROPER_CTYPES, sp,
628                     &format!("found non-foreign-function-safe member in \
629                               enum: {}", s));
630             }
631         }
632     }
633
634     fn check_foreign_fn(&mut self, id: ast::NodeId, decl: &hir::FnDecl) {
635         let def_id = self.cx.tcx.map.local_def_id(id);
636         let scheme = self.cx.tcx.lookup_item_type(def_id);
637         let sig = scheme.ty.fn_sig();
638         let sig = self.cx.tcx.erase_late_bound_regions(&sig);
639
640         for (&input_ty, input_hir) in sig.inputs.iter().zip(&decl.inputs) {
641             self.check_type_for_ffi_and_report_errors(input_hir.ty.span, &input_ty);
642         }
643
644         if let hir::Return(ref ret_hir) = decl.output {
645             let ret_ty = sig.output.unwrap();
646             if !ret_ty.is_nil() {
647                 self.check_type_for_ffi_and_report_errors(ret_hir.span, ret_ty);
648             }
649         }
650     }
651
652     fn check_foreign_static(&mut self, id: ast::NodeId, span: Span) {
653         let def_id = self.cx.tcx.map.local_def_id(id);
654         let scheme = self.cx.tcx.lookup_item_type(def_id);
655         self.check_type_for_ffi_and_report_errors(span, scheme.ty);
656     }
657 }
658
659 #[derive(Copy, Clone)]
660 pub struct ImproperCTypes;
661
662 impl LintPass for ImproperCTypes {
663     fn get_lints(&self) -> LintArray {
664         lint_array!(IMPROPER_CTYPES)
665     }
666 }
667
668 impl LateLintPass for ImproperCTypes {
669     fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
670         let mut vis = ImproperCTypesVisitor { cx: cx };
671         if let hir::ItemForeignMod(ref nmod) = it.node {
672             if nmod.abi != Abi::RustIntrinsic && nmod.abi != Abi::PlatformIntrinsic {
673                 for ni in &nmod.items {
674                     match ni.node {
675                         hir::ForeignItemFn(ref decl, _) => {
676                             vis.check_foreign_fn(ni.id, decl);
677                         }
678                         hir::ForeignItemStatic(ref ty, _) => {
679                             vis.check_foreign_static(ni.id, ty.span);
680                         }
681                     }
682                 }
683             }
684         }
685     }
686 }
687
688 pub struct VariantSizeDifferences;
689
690 impl LintPass for VariantSizeDifferences {
691     fn get_lints(&self) -> LintArray {
692         lint_array!(VARIANT_SIZE_DIFFERENCES)
693     }
694 }
695
696 impl LateLintPass for VariantSizeDifferences {
697     fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
698         if let hir::ItemEnum(ref enum_definition, ref gens) = it.node {
699             if gens.ty_params.is_empty() {  // sizes only make sense for non-generic types
700                 let mut sizes = vec![];
701                 let t = cx.tcx.node_id_to_type(it.id);
702                 let layout = cx.tcx.normalizing_infer_ctxt(ProjectionMode::Any).enter(|infcx| {
703                     t.layout(&infcx).unwrap_or_else(|e| {
704                         bug!("failed to get layout for `{}`: {}", t, e)
705                     })
706                 });
707
708                 if let Layout::General { ref variants, ref size, discr, .. } = *layout {
709                     let discr_size = Primitive::Int(discr).size(&cx.tcx.data_layout).bytes();
710
711                     debug!("enum `{}` is {} bytes large", t, size.bytes());
712
713                     for (variant, variant_layout) in enum_definition.variants.iter().zip(variants) {
714                         // Subtract the size of the enum discriminant
715                         let bytes = variant_layout.min_size().bytes().saturating_sub(discr_size);
716                         sizes.push(bytes);
717
718                         debug!("- variant `{}` is {} bytes large", variant.node.name, bytes);
719                     }
720
721                     let (largest, slargest, largest_index) = sizes.iter()
722                                                                   .enumerate()
723                                                                   .fold((0, 0, 0),
724                         |(l, s, li), (idx, &size)|
725                             if size > l {
726                                 (size, l, idx)
727                             } else if size > s {
728                                 (l, size, li)
729                             } else {
730                                 (l, s, li)
731                             }
732                     );
733
734                     // we only warn if the largest variant is at least thrice as large as
735                     // the second-largest.
736                     if largest > slargest * 3 && slargest > 0 {
737                         cx.span_lint(VARIANT_SIZE_DIFFERENCES,
738                                      enum_definition.variants[largest_index].span,
739                                      &format!("enum variant is more than three times larger \
740                                                ({} bytes) than the next largest", largest));
741                     }
742                 }
743             }
744         }
745     }
746 }