1 // Copyright 2012-2014 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.
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.
11 //! Lints built in to rustc.
13 //! This is a sibling of `lint::context` in order to ensure that
14 //! lints implemented here use the same public API as lint plugins.
16 //! To add a new lint to rustc, declare it here using `declare_lint!()`.
17 //! Then add code to emit the new lint in the appropriate circumstances.
18 //! You can do that in an existing `LintPass` if it makes sense, or in
19 //! a new `LintPass`, or using `Session::add_lint` elsewhere in the
20 //! compiler. Only do the latter if the check can't be written cleanly
23 //! If you define a new `LintPass`, you will also need to add it to the
24 //! `add_builtin!` or `add_builtin_with_new!` invocation in `context.rs`.
25 //! Use the former for unit-like structs and the latter for structs with
28 use metadata::csearch;
30 use middle::typeck::astconv::ast_ty_to_ty;
31 use middle::typeck::infer;
32 use middle::{typeck, ty, def, pat_util, stability};
33 use util::ppaux::{ty_to_string};
34 use util::nodemap::NodeSet;
35 use lint::{Context, LintPass, LintArray};
38 use std::collections::HashMap;
39 use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
43 use syntax::attr::AttrMetaMethods;
45 use syntax::codemap::Span;
46 use syntax::parse::token;
47 use syntax::{ast, ast_util, visit};
48 use syntax::visit::Visitor;
50 declare_lint!(WHILE_TRUE, Warn,
51 "suggest using `loop { }` instead of `while true { }`")
55 impl LintPass for WhileTrue {
56 fn get_lints(&self) -> LintArray {
57 lint_array!(WHILE_TRUE)
60 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
62 ast::ExprWhile(cond, _, _) => {
64 ast::ExprLit(lit) => {
66 ast::LitBool(true) => {
67 cx.span_lint(WHILE_TRUE, e.span,
68 "denote infinite loops with loop \
82 declare_lint!(UNNECESSARY_TYPECAST, Allow,
83 "detects unnecessary type casts, that can be removed")
85 pub struct UnusedCasts;
87 impl LintPass for UnusedCasts {
88 fn get_lints(&self) -> LintArray {
89 lint_array!(UNNECESSARY_TYPECAST)
92 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
94 ast::ExprCast(expr, ty) => {
95 let t_t = ast_ty_to_ty(cx, &infer::new_infer_ctxt(cx.tcx), &*ty);
96 if ty::get(ty::expr_ty(cx.tcx, &*expr)).sty == ty::get(t_t).sty {
97 cx.span_lint(UNNECESSARY_TYPECAST, ty.span, "unnecessary type cast");
105 declare_lint!(UNSIGNED_NEGATE, Warn,
106 "using an unary minus operator on unsigned type")
108 declare_lint!(TYPE_LIMITS, Warn,
109 "comparisons made useless by limits of the types involved")
111 declare_lint!(TYPE_OVERFLOW, Warn,
112 "literal out of range for its type")
114 pub struct TypeLimits {
115 /// Id of the last visited negated expression
116 negated_expr_id: ast::NodeId,
120 pub fn new() -> TypeLimits {
127 impl LintPass for TypeLimits {
128 fn get_lints(&self) -> LintArray {
129 lint_array!(UNSIGNED_NEGATE, TYPE_LIMITS, TYPE_OVERFLOW)
132 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
134 ast::ExprUnary(ast::UnNeg, expr) => {
136 ast::ExprLit(lit) => {
138 ast::LitInt(_, ast::UnsignedIntLit(_)) => {
139 cx.span_lint(UNSIGNED_NEGATE, e.span,
140 "negation of unsigned int literal may \
147 let t = ty::expr_ty(cx.tcx, &*expr);
148 match ty::get(t).sty {
150 cx.span_lint(UNSIGNED_NEGATE, e.span,
151 "negation of unsigned int variable may \
158 // propagate negation, if the negation itself isn't negated
159 if self.negated_expr_id != e.id {
160 self.negated_expr_id = expr.id;
163 ast::ExprParen(expr) if self.negated_expr_id == e.id => {
164 self.negated_expr_id = expr.id;
166 ast::ExprBinary(binop, l, r) => {
167 if is_comparison(binop) && !check_limits(cx.tcx, binop, &*l, &*r) {
168 cx.span_lint(TYPE_LIMITS, e.span,
169 "comparison is useless due to type limits");
172 ast::ExprLit(lit) => {
173 match ty::get(ty::expr_ty(cx.tcx, e)).sty {
176 ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
177 ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => {
178 let int_type = if t == ast::TyI {
179 cx.sess().targ_cfg.int_type
181 let (min, max) = int_ty_range(int_type);
182 let negative = self.negated_expr_id == e.id;
184 if (negative && v > (min.abs() as u64)) ||
185 (!negative && v > (max.abs() as u64)) {
186 cx.span_lint(TYPE_OVERFLOW, e.span,
187 "literal out of range for its type");
195 let uint_type = if t == ast::TyU {
196 cx.sess().targ_cfg.uint_type
198 let (min, max) = uint_ty_range(uint_type);
199 let lit_val: u64 = match lit.node {
200 ast::LitByte(_v) => return, // _v is u8, within range by definition
201 ast::LitInt(v, _) => v,
204 if lit_val < min || lit_val > max {
205 cx.span_lint(TYPE_OVERFLOW, e.span,
206 "literal out of range for its type");
210 let (min, max) = float_ty_range(t);
211 let lit_val: f64 = match lit.node {
212 ast::LitFloat(ref v, _) |
213 ast::LitFloatUnsuffixed(ref v) => match from_str(v.get()) {
219 if lit_val < min || lit_val > max {
220 cx.span_lint(TYPE_OVERFLOW, e.span,
221 "literal out of range for its type");
230 fn is_valid<T:cmp::PartialOrd>(binop: ast::BinOp, v: T,
231 min: T, max: T) -> bool {
233 ast::BiLt => v > min && v <= max,
234 ast::BiLe => v >= min && v < max,
235 ast::BiGt => v >= min && v < max,
236 ast::BiGe => v > min && v <= max,
237 ast::BiEq | ast::BiNe => v >= min && v <= max,
242 fn rev_binop(binop: ast::BinOp) -> ast::BinOp {
244 ast::BiLt => ast::BiGt,
245 ast::BiLe => ast::BiGe,
246 ast::BiGt => ast::BiLt,
247 ast::BiGe => ast::BiLe,
252 // for int & uint, be conservative with the warnings, so that the
253 // warnings are consistent between 32- and 64-bit platforms
254 fn int_ty_range(int_ty: ast::IntTy) -> (i64, i64) {
256 ast::TyI => (i64::MIN, i64::MAX),
257 ast::TyI8 => (i8::MIN as i64, i8::MAX as i64),
258 ast::TyI16 => (i16::MIN as i64, i16::MAX as i64),
259 ast::TyI32 => (i32::MIN as i64, i32::MAX as i64),
260 ast::TyI64 => (i64::MIN, i64::MAX)
264 fn uint_ty_range(uint_ty: ast::UintTy) -> (u64, u64) {
266 ast::TyU => (u64::MIN, u64::MAX),
267 ast::TyU8 => (u8::MIN as u64, u8::MAX as u64),
268 ast::TyU16 => (u16::MIN as u64, u16::MAX as u64),
269 ast::TyU32 => (u32::MIN as u64, u32::MAX as u64),
270 ast::TyU64 => (u64::MIN, u64::MAX)
274 fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
276 ast::TyF32 => (f32::MIN_VALUE as f64, f32::MAX_VALUE as f64),
277 ast::TyF64 => (f64::MIN_VALUE, f64::MAX_VALUE)
281 fn check_limits(tcx: &ty::ctxt, binop: ast::BinOp,
282 l: &ast::Expr, r: &ast::Expr) -> bool {
283 let (lit, expr, swap) = match (&l.node, &r.node) {
284 (&ast::ExprLit(_), _) => (l, r, true),
285 (_, &ast::ExprLit(_)) => (r, l, false),
288 // Normalize the binop so that the literal is always on the RHS in
290 let norm_binop = if swap { rev_binop(binop) } else { binop };
291 match ty::get(ty::expr_ty(tcx, expr)).sty {
292 ty::ty_int(int_ty) => {
293 let (min, max) = int_ty_range(int_ty);
294 let lit_val: i64 = match lit.node {
295 ast::ExprLit(li) => match li.node {
296 ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
297 ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => v as i64,
298 ast::LitInt(v, ast::SignedIntLit(_, ast::Minus)) |
299 ast::LitInt(v, ast::UnsuffixedIntLit(ast::Minus)) => -(v as i64),
304 is_valid(norm_binop, lit_val, min, max)
306 ty::ty_uint(uint_ty) => {
307 let (min, max): (u64, u64) = uint_ty_range(uint_ty);
308 let lit_val: u64 = match lit.node {
309 ast::ExprLit(li) => match li.node {
310 ast::LitInt(v, _) => v,
315 is_valid(norm_binop, lit_val, min, max)
321 fn is_comparison(binop: ast::BinOp) -> bool {
323 ast::BiEq | ast::BiLt | ast::BiLe |
324 ast::BiNe | ast::BiGe | ast::BiGt => true,
331 declare_lint!(CTYPES, Warn,
332 "proper use of libc types in foreign modules")
334 struct CTypesVisitor<'a, 'tcx: 'a> {
335 cx: &'a Context<'a, 'tcx>
338 impl<'a, 'tcx> CTypesVisitor<'a, 'tcx> {
339 fn check_def(&mut self, sp: Span, ty_id: ast::NodeId, path_id: ast::NodeId) {
340 match self.cx.tcx.def_map.borrow().get_copy(&path_id) {
341 def::DefPrimTy(ast::TyInt(ast::TyI)) => {
342 self.cx.span_lint(CTYPES, sp,
343 "found rust type `int` in foreign module, while \
344 libc::c_int or libc::c_long should be used");
346 def::DefPrimTy(ast::TyUint(ast::TyU)) => {
347 self.cx.span_lint(CTYPES, sp,
348 "found rust type `uint` in foreign module, while \
349 libc::c_uint or libc::c_ulong should be used");
352 let tty = match self.cx.tcx.ast_ty_to_ty_cache.borrow().find(&ty_id) {
353 Some(&ty::atttce_resolved(t)) => t,
354 _ => fail!("ast_ty_to_ty_cache was incomplete after typeck!")
357 if !ty::is_ffi_safe(self.cx.tcx, tty) {
358 self.cx.span_lint(CTYPES, sp,
359 "found type without foreign-function-safe
360 representation annotation in foreign module, consider \
361 adding a #[repr(...)] attribute to the type");
369 impl<'a, 'tcx> Visitor<()> for CTypesVisitor<'a, 'tcx> {
370 fn visit_ty(&mut self, ty: &ast::Ty, _: ()) {
372 ast::TyPath(_, _, id) => self.check_def(ty.span, ty.id, id),
375 visit::walk_ty(self, ty, ());
381 impl LintPass for CTypes {
382 fn get_lints(&self) -> LintArray {
386 fn check_item(&mut self, cx: &Context, it: &ast::Item) {
387 fn check_ty(cx: &Context, ty: &ast::Ty) {
388 let mut vis = CTypesVisitor { cx: cx };
389 vis.visit_ty(ty, ());
392 fn check_foreign_fn(cx: &Context, decl: &ast::FnDecl) {
393 for input in decl.inputs.iter() {
394 check_ty(cx, &*input.ty);
396 check_ty(cx, &*decl.output)
400 ast::ItemForeignMod(ref nmod) if nmod.abi != abi::RustIntrinsic => {
401 for ni in nmod.items.iter() {
403 ast::ForeignItemFn(decl, _) => check_foreign_fn(cx, &*decl),
404 ast::ForeignItemStatic(t, _) => check_ty(cx, &*t)
413 declare_lint!(MANAGED_HEAP_MEMORY, Allow,
414 "use of managed (@ type) heap memory")
416 declare_lint!(OWNED_HEAP_MEMORY, Allow,
417 "use of owned (Box type) heap memory")
419 declare_lint!(HEAP_MEMORY, Allow,
420 "use of any (Box type or @ type) heap memory")
422 pub struct HeapMemory;
425 fn check_heap_type(&self, cx: &Context, span: Span, ty: ty::t) {
428 ty::fold_ty(cx.tcx, ty, |t| {
429 match ty::get(t).sty {
434 ty::ty_closure(box ty::ClosureTy {
435 store: ty::UniqTraitStore,
447 let s = ty_to_string(cx.tcx, ty);
448 let m = format!("type uses owned (Box type) pointers: {}", s);
449 cx.span_lint(OWNED_HEAP_MEMORY, span, m.as_slice());
450 cx.span_lint(HEAP_MEMORY, span, m.as_slice());
454 let s = ty_to_string(cx.tcx, ty);
455 let m = format!("type uses managed (@ type) pointers: {}", s);
456 cx.span_lint(MANAGED_HEAP_MEMORY, span, m.as_slice());
457 cx.span_lint(HEAP_MEMORY, span, m.as_slice());
462 impl LintPass for HeapMemory {
463 fn get_lints(&self) -> LintArray {
464 lint_array!(MANAGED_HEAP_MEMORY, OWNED_HEAP_MEMORY, HEAP_MEMORY)
467 fn check_item(&mut self, cx: &Context, it: &ast::Item) {
472 ast::ItemStruct(..) =>
473 self.check_heap_type(cx, it.span,
474 ty::node_id_to_type(cx.tcx, it.id)),
478 // If it's a struct, we also have to check the fields' types
480 ast::ItemStruct(struct_def, _) => {
481 for struct_field in struct_def.fields.iter() {
482 self.check_heap_type(cx, struct_field.span,
483 ty::node_id_to_type(cx.tcx, struct_field.node.id));
490 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
491 let ty = ty::expr_ty(cx.tcx, e);
492 self.check_heap_type(cx, e.span, ty);
496 declare_lint!(RAW_POINTER_DERIVING, Warn,
497 "uses of #[deriving] with raw pointers are rarely correct")
499 struct RawPtrDerivingVisitor<'a, 'tcx: 'a> {
500 cx: &'a Context<'a, 'tcx>
503 impl<'a, 'tcx> Visitor<()> for RawPtrDerivingVisitor<'a, 'tcx> {
504 fn visit_ty(&mut self, ty: &ast::Ty, _: ()) {
505 static MSG: &'static str = "use of `#[deriving]` with a raw pointer";
507 ast::TyPtr(..) => self.cx.span_lint(RAW_POINTER_DERIVING, ty.span, MSG),
510 visit::walk_ty(self, ty, ());
512 // explicit override to a no-op to reduce code bloat
513 fn visit_expr(&mut self, _: &ast::Expr, _: ()) {}
514 fn visit_block(&mut self, _: &ast::Block, _: ()) {}
517 pub struct RawPointerDeriving {
518 checked_raw_pointers: NodeSet,
521 impl RawPointerDeriving {
522 pub fn new() -> RawPointerDeriving {
524 checked_raw_pointers: NodeSet::new(),
529 impl LintPass for RawPointerDeriving {
530 fn get_lints(&self) -> LintArray {
531 lint_array!(RAW_POINTER_DERIVING)
534 fn check_item(&mut self, cx: &Context, item: &ast::Item) {
535 if !attr::contains_name(item.attrs.as_slice(), "automatically_derived") {
538 let did = match item.node {
539 ast::ItemImpl(..) => {
540 match ty::get(ty::node_id_to_type(cx.tcx, item.id)).sty {
541 ty::ty_enum(did, _) => did,
542 ty::ty_struct(did, _) => did,
548 if !ast_util::is_local(did) { return }
549 let item = match cx.tcx.map.find(did.node) {
550 Some(ast_map::NodeItem(item)) => item,
553 if !self.checked_raw_pointers.insert(item.id) { return }
555 ast::ItemStruct(..) | ast::ItemEnum(..) => {
556 let mut visitor = RawPtrDerivingVisitor { cx: cx };
557 visit::walk_item(&mut visitor, &*item, ());
564 declare_lint!(UNUSED_ATTRIBUTE, Warn,
565 "detects attributes that were not used by the compiler")
567 pub struct UnusedAttribute;
569 impl LintPass for UnusedAttribute {
570 fn get_lints(&self) -> LintArray {
571 lint_array!(UNUSED_ATTRIBUTE)
574 fn check_attribute(&mut self, cx: &Context, attr: &ast::Attribute) {
575 static ATTRIBUTE_WHITELIST: &'static [&'static str] = &[
576 // FIXME: #14408 whitelist docs since rustdoc looks at them
579 // FIXME: #14406 these are processed in trans, which happens after the
598 // not used anywhere (!?) but apparently we want to keep them around
603 // FIXME: #14407 these are only looked at on-demand so we can't
604 // guarantee they'll have already been checked
614 static CRATE_ATTRS: &'static [&'static str] = &[
628 for &name in ATTRIBUTE_WHITELIST.iter() {
629 if attr.check_name(name) {
634 if !attr::is_used(attr) {
635 cx.span_lint(UNUSED_ATTRIBUTE, attr.span, "unused attribute");
636 if CRATE_ATTRS.contains(&attr.name().get()) {
637 let msg = match attr.node.style {
638 ast::AttrOuter => "crate-level attribute should be an inner \
639 attribute: add an exclamation mark: #![foo]",
640 ast::AttrInner => "crate-level attribute should be in the \
643 cx.span_lint(UNUSED_ATTRIBUTE, attr.span, msg);
649 declare_lint!(PATH_STATEMENT, Warn,
650 "path statements with no effect")
652 pub struct PathStatement;
654 impl LintPass for PathStatement {
655 fn get_lints(&self) -> LintArray {
656 lint_array!(PATH_STATEMENT)
659 fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
661 ast::StmtSemi(expr, _) => {
663 ast::ExprPath(_) => cx.span_lint(PATH_STATEMENT, s.span,
664 "path statement with no effect"),
673 declare_lint!(UNUSED_MUST_USE, Warn,
674 "unused result of a type flagged as #[must_use]")
676 declare_lint!(UNUSED_RESULT, Allow,
677 "unused result of an expression in a statement")
679 pub struct UnusedResult;
681 impl LintPass for UnusedResult {
682 fn get_lints(&self) -> LintArray {
683 lint_array!(UNUSED_MUST_USE, UNUSED_RESULT)
686 fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
687 let expr = match s.node {
688 ast::StmtSemi(expr, _) => expr,
691 let t = ty::expr_ty(cx.tcx, &*expr);
692 match ty::get(t).sty {
693 ty::ty_nil | ty::ty_bot | ty::ty_bool => return,
697 ast::ExprRet(..) => return,
701 let t = ty::expr_ty(cx.tcx, &*expr);
702 let mut warned = false;
703 match ty::get(t).sty {
704 ty::ty_struct(did, _) |
705 ty::ty_enum(did, _) => {
706 if ast_util::is_local(did) {
707 match cx.tcx.map.get(did.node) {
708 ast_map::NodeItem(it) => {
709 warned |= check_must_use(cx, it.attrs.as_slice(), s.span);
714 csearch::get_item_attrs(&cx.sess().cstore, did, |attrs| {
715 warned |= check_must_use(cx, attrs.as_slice(), s.span);
722 cx.span_lint(UNUSED_RESULT, s.span, "unused result");
725 fn check_must_use(cx: &Context, attrs: &[ast::Attribute], sp: Span) -> bool {
726 for attr in attrs.iter() {
727 if attr.check_name("must_use") {
728 let mut msg = "unused result which must be used".to_string();
729 // check for #[must_use="..."]
730 match attr.value_str() {
734 msg.push_str(s.get());
737 cx.span_lint(UNUSED_MUST_USE, sp, msg.as_slice());
746 declare_lint!(pub NON_CAMEL_CASE_TYPES, Warn,
747 "types, variants, traits and type parameters should have camel case names")
749 pub struct NonCamelCaseTypes;
751 impl NonCamelCaseTypes {
752 fn check_case(&self, cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
753 fn is_camel_case(ident: ast::Ident) -> bool {
754 let ident = token::get_ident(ident);
755 if ident.get().is_empty() { return true; }
756 let ident = ident.get().trim_chars('_');
758 // start with a non-lowercase letter rather than non-uppercase
759 // ones (some scripts don't have a concept of upper/lowercase)
760 ident.len() > 0 && !ident.char_at(0).is_lowercase() && !ident.contains_char('_')
763 fn to_camel_case(s: &str) -> String {
764 s.split('_').flat_map(|word| word.chars().enumerate().map(|(i, c)|
765 if i == 0 { c.to_uppercase() }
770 let s = token::get_ident(ident);
772 if !is_camel_case(ident) {
773 let c = to_camel_case(s.get());
774 let m = if c.is_empty() {
775 format!("{} `{}` should have a camel case name such as `CamelCase`", sort, s)
777 format!("{} `{}` should have a camel case name such as `{}`", sort, s, c)
779 cx.span_lint(NON_CAMEL_CASE_TYPES, span, m.as_slice());
784 impl LintPass for NonCamelCaseTypes {
785 fn get_lints(&self) -> LintArray {
786 lint_array!(NON_CAMEL_CASE_TYPES)
789 fn check_item(&mut self, cx: &Context, it: &ast::Item) {
790 let has_extern_repr = it.attrs.iter().map(|attr| {
791 attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr).iter()
792 .any(|r| r == &attr::ReprExtern)
794 if has_extern_repr { return }
797 ast::ItemTy(..) | ast::ItemStruct(..) => {
798 self.check_case(cx, "type", it.ident, it.span)
800 ast::ItemTrait(..) => {
801 self.check_case(cx, "trait", it.ident, it.span)
803 ast::ItemEnum(ref enum_definition, _) => {
804 if has_extern_repr { return }
805 self.check_case(cx, "type", it.ident, it.span);
806 for variant in enum_definition.variants.iter() {
807 self.check_case(cx, "variant", variant.node.name, variant.span);
814 fn check_generics(&mut self, cx: &Context, it: &ast::Generics) {
815 for gen in it.ty_params.iter() {
816 self.check_case(cx, "type parameter", gen.ident, gen.span);
821 #[deriving(PartialEq)]
828 fn method_context(cx: &Context, m: &ast::Method) -> MethodContext {
829 let did = ast::DefId {
830 krate: ast::LOCAL_CRATE,
834 match cx.tcx.impl_or_trait_items.borrow().find_copy(&did) {
835 None => cx.sess().span_bug(m.span, "missing method descriptor?!"),
838 ty::MethodTraitItem(md) => {
840 ty::TraitContainer(..) => TraitDefaultImpl,
841 ty::ImplContainer(cid) => {
842 match ty::impl_trait_ref(cx.tcx, cid) {
843 Some(..) => TraitImpl,
854 declare_lint!(pub NON_SNAKE_CASE, Warn,
855 "methods, functions, lifetime parameters and modules should have snake case names")
857 pub struct NonSnakeCase;
860 fn check_snake_case(&self, cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
861 fn is_snake_case(ident: ast::Ident) -> bool {
862 let ident = token::get_ident(ident);
863 if ident.get().is_empty() { return true; }
864 let ident = ident.get().trim_left_chars('\'');
865 let ident = ident.trim_chars('_');
867 let mut allow_underscore = true;
868 ident.chars().all(|c| {
869 allow_underscore = match c {
870 c if c.is_lowercase() || c.is_digit() => true,
871 '_' if allow_underscore => false,
878 fn to_snake_case(str: &str) -> String {
879 let mut words = vec![];
880 for s in str.split('_') {
881 let mut buf = String::new();
882 if s.is_empty() { continue; }
883 for ch in s.chars() {
884 if !buf.is_empty() && buf.as_slice() != "'" && ch.is_uppercase() {
888 buf.push_char(ch.to_lowercase());
895 let s = token::get_ident(ident);
897 if !is_snake_case(ident) {
898 cx.span_lint(NON_SNAKE_CASE, span,
899 format!("{} `{}` should have a snake case name such as `{}`",
900 sort, s, to_snake_case(s.get())).as_slice());
905 impl LintPass for NonSnakeCase {
906 fn get_lints(&self) -> LintArray {
907 lint_array!(NON_SNAKE_CASE)
910 fn check_fn(&mut self, cx: &Context,
911 fk: &visit::FnKind, _: &ast::FnDecl,
912 _: &ast::Block, span: Span, _: ast::NodeId) {
914 visit::FkMethod(ident, _, m) => match method_context(cx, m) {
916 => self.check_snake_case(cx, "method", ident, span),
918 => self.check_snake_case(cx, "trait method", ident, span),
921 visit::FkItemFn(ident, _, _, _)
922 => self.check_snake_case(cx, "function", ident, span),
927 fn check_item(&mut self, cx: &Context, it: &ast::Item) {
930 self.check_snake_case(cx, "module", it.ident, it.span);
936 fn check_ty_method(&mut self, cx: &Context, t: &ast::TypeMethod) {
937 self.check_snake_case(cx, "trait method", t.ident, t.span);
940 fn check_lifetime_decl(&mut self, cx: &Context, t: &ast::LifetimeDef) {
941 self.check_snake_case(cx, "lifetime", t.lifetime.name.ident(), t.lifetime.span);
944 fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
946 &ast::PatIdent(_, ref path1, _) => {
947 match cx.tcx.def_map.borrow().find(&p.id) {
948 Some(&def::DefLocal(_, _)) | Some(&def::DefBinding(_, _)) |
949 Some(&def::DefArg(_, _)) => {
950 self.check_snake_case(cx, "variable", path1.node, p.span);
959 fn check_struct_def(&mut self, cx: &Context, s: &ast::StructDef,
960 _: ast::Ident, _: &ast::Generics, _: ast::NodeId) {
961 for sf in s.fields.iter() {
963 ast::StructField_ { kind: ast::NamedField(ident, _), .. } => {
964 self.check_snake_case(cx, "structure field", ident, sf.span);
972 declare_lint!(pub NON_UPPERCASE_STATICS, Allow,
973 "static constants should have uppercase identifiers")
975 pub struct NonUppercaseStatics;
977 impl LintPass for NonUppercaseStatics {
978 fn get_lints(&self) -> LintArray {
979 lint_array!(NON_UPPERCASE_STATICS)
982 fn check_item(&mut self, cx: &Context, it: &ast::Item) {
984 // only check static constants
985 ast::ItemStatic(_, ast::MutImmutable, _) => {
986 let s = token::get_ident(it.ident);
987 // check for lowercase letters rather than non-uppercase
988 // ones (some scripts don't have a concept of
990 if s.get().chars().any(|c| c.is_lowercase()) {
991 cx.span_lint(NON_UPPERCASE_STATICS, it.span,
992 format!("static constant `{}` should have an uppercase name \
994 s.get(), s.get().chars().map(|c| c.to_uppercase())
995 .collect::<String>().as_slice()).as_slice());
1002 fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
1003 // Lint for constants that look like binding identifiers (#7526)
1004 match (&p.node, cx.tcx.def_map.borrow().find(&p.id)) {
1005 (&ast::PatIdent(_, ref path1, _), Some(&def::DefStatic(_, false))) => {
1006 let s = token::get_ident(path1.node);
1007 if s.get().chars().any(|c| c.is_lowercase()) {
1008 cx.span_lint(NON_UPPERCASE_STATICS, path1.span,
1009 format!("static constant in pattern `{}` should have an uppercase \
1011 s.get(), s.get().chars().map(|c| c.to_uppercase())
1012 .collect::<String>().as_slice()).as_slice());
1020 declare_lint!(UNNECESSARY_PARENS, Warn,
1021 "`if`, `match`, `while` and `return` do not need parentheses")
1023 pub struct UnnecessaryParens;
1025 impl UnnecessaryParens {
1026 fn check_unnecessary_parens_core(&self, cx: &Context, value: &ast::Expr, msg: &str,
1027 struct_lit_needs_parens: bool) {
1029 ast::ExprParen(ref inner) => {
1030 let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&**inner);
1032 cx.span_lint(UNNECESSARY_PARENS, value.span,
1033 format!("unnecessary parentheses around {}",
1040 /// Expressions that syntactically contain an "exterior" struct
1041 /// literal i.e. not surrounded by any parens or other
1042 /// delimiters, e.g. `X { y: 1 }`, `X { y: 1 }.method()`, `foo
1043 /// == X { y: 1 }` and `X { y: 1 } == foo` all do, but `(X {
1044 /// y: 1 }) == foo` does not.
1045 fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
1047 ast::ExprStruct(..) => true,
1049 ast::ExprAssign(ref lhs, ref rhs) |
1050 ast::ExprAssignOp(_, ref lhs, ref rhs) |
1051 ast::ExprBinary(_, ref lhs, ref rhs) => {
1052 // X { y: 1 } + X { y: 2 }
1053 contains_exterior_struct_lit(&**lhs) ||
1054 contains_exterior_struct_lit(&**rhs)
1056 ast::ExprUnary(_, ref x) |
1057 ast::ExprCast(ref x, _) |
1058 ast::ExprField(ref x, _, _) |
1059 ast::ExprIndex(ref x, _) => {
1060 // &X { y: 1 }, X { y: 1 }.y
1061 contains_exterior_struct_lit(&**x)
1064 ast::ExprMethodCall(_, _, ref exprs) => {
1065 // X { y: 1 }.bar(...)
1066 contains_exterior_struct_lit(&**exprs.get(0))
1075 impl LintPass for UnnecessaryParens {
1076 fn get_lints(&self) -> LintArray {
1077 lint_array!(UNNECESSARY_PARENS)
1080 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1081 let (value, msg, struct_lit_needs_parens) = match e.node {
1082 ast::ExprIf(cond, _, _) => (cond, "`if` condition", true),
1083 ast::ExprWhile(cond, _, _) => (cond, "`while` condition", true),
1084 ast::ExprMatch(head, _) => (head, "`match` head expression", true),
1085 ast::ExprRet(Some(value)) => (value, "`return` value", false),
1086 ast::ExprAssign(_, value) => (value, "assigned value", false),
1087 ast::ExprAssignOp(_, _, value) => (value, "assigned value", false),
1090 self.check_unnecessary_parens_core(cx, &*value, msg, struct_lit_needs_parens);
1093 fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
1094 let (value, msg) = match s.node {
1095 ast::StmtDecl(decl, _) => match decl.node {
1096 ast::DeclLocal(local) => match local.init {
1097 Some(value) => (value, "assigned value"),
1104 self.check_unnecessary_parens_core(cx, &*value, msg, false);
1108 declare_lint!(UNUSED_UNSAFE, Warn,
1109 "unnecessary use of an `unsafe` block")
1111 pub struct UnusedUnsafe;
1113 impl LintPass for UnusedUnsafe {
1114 fn get_lints(&self) -> LintArray {
1115 lint_array!(UNUSED_UNSAFE)
1118 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1120 // Don't warn about generated blocks, that'll just pollute the output.
1121 ast::ExprBlock(ref blk) => {
1122 if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
1123 !cx.tcx.used_unsafe.borrow().contains(&blk.id) {
1124 cx.span_lint(UNUSED_UNSAFE, blk.span, "unnecessary `unsafe` block");
1132 declare_lint!(UNSAFE_BLOCK, Allow,
1133 "usage of an `unsafe` block")
1135 pub struct UnsafeBlock;
1137 impl LintPass for UnsafeBlock {
1138 fn get_lints(&self) -> LintArray {
1139 lint_array!(UNSAFE_BLOCK)
1142 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1144 // Don't warn about generated blocks, that'll just pollute the output.
1145 ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(ast::UserProvided) => {
1146 cx.span_lint(UNSAFE_BLOCK, blk.span, "usage of an `unsafe` block");
1153 declare_lint!(pub UNUSED_MUT, Warn,
1154 "detect mut variables which don't need to be mutable")
1156 pub struct UnusedMut;
1159 fn check_unused_mut_pat(&self, cx: &Context, pats: &[Gc<ast::Pat>]) {
1160 // collect all mutable pattern and group their NodeIDs by their Identifier to
1161 // avoid false warnings in match arms with multiple patterns
1162 let mut mutables = HashMap::new();
1163 for &p in pats.iter() {
1164 pat_util::pat_bindings(&cx.tcx.def_map, &*p, |mode, id, _, path1| {
1165 let ident = path1.node;
1167 ast::BindByValue(ast::MutMutable) => {
1168 if !token::get_ident(ident).get().starts_with("_") {
1169 mutables.insert_or_update_with(ident.name.uint(),
1170 vec!(id), |_, old| { old.push(id); });
1179 let used_mutables = cx.tcx.used_mut_nodes.borrow();
1180 for (_, v) in mutables.iter() {
1181 if !v.iter().any(|e| used_mutables.contains(e)) {
1182 cx.span_lint(UNUSED_MUT, cx.tcx.map.span(*v.get(0)),
1183 "variable does not need to be mutable");
1189 impl LintPass for UnusedMut {
1190 fn get_lints(&self) -> LintArray {
1191 lint_array!(UNUSED_MUT)
1194 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1196 ast::ExprMatch(_, ref arms) => {
1197 for a in arms.iter() {
1198 self.check_unused_mut_pat(cx, a.pats.as_slice())
1205 fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
1207 ast::StmtDecl(d, _) => {
1209 ast::DeclLocal(l) => {
1210 self.check_unused_mut_pat(cx, &[l.pat]);
1219 fn check_fn(&mut self, cx: &Context,
1220 _: &visit::FnKind, decl: &ast::FnDecl,
1221 _: &ast::Block, _: Span, _: ast::NodeId) {
1222 for a in decl.inputs.iter() {
1223 self.check_unused_mut_pat(cx, &[a.pat]);
1233 declare_lint!(UNNECESSARY_ALLOCATION, Warn,
1234 "detects unnecessary allocations that can be eliminated")
1236 pub struct UnnecessaryAllocation;
1238 impl LintPass for UnnecessaryAllocation {
1239 fn get_lints(&self) -> LintArray {
1240 lint_array!(UNNECESSARY_ALLOCATION)
1243 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1244 // Warn if boxing expressions are immediately borrowed.
1245 let allocation = match e.node {
1246 ast::ExprUnary(ast::UnUniq, _) |
1247 ast::ExprUnary(ast::UnBox, _) => BoxAllocation,
1252 match cx.tcx.adjustments.borrow().find(&e.id) {
1253 Some(adjustment) => {
1255 ty::AutoDerefRef(ty::AutoDerefRef { ref autoref, .. }) => {
1256 match (allocation, autoref) {
1257 (VectorAllocation, &Some(ty::AutoPtr(_, _, None))) => {
1258 cx.span_lint(UNNECESSARY_ALLOCATION, e.span,
1259 "unnecessary allocation, the sigil can be removed");
1262 &Some(ty::AutoPtr(_, ast::MutImmutable, None))) => {
1263 cx.span_lint(UNNECESSARY_ALLOCATION, e.span,
1264 "unnecessary allocation, use & instead");
1267 &Some(ty::AutoPtr(_, ast::MutMutable, None))) => {
1268 cx.span_lint(UNNECESSARY_ALLOCATION, e.span,
1269 "unnecessary allocation, use &mut instead");
1282 declare_lint!(MISSING_DOC, Allow,
1283 "detects missing documentation for public members")
1285 pub struct MissingDoc {
1286 /// Stack of IDs of struct definitions.
1287 struct_def_stack: Vec<ast::NodeId>,
1289 /// Stack of whether #[doc(hidden)] is set
1290 /// at each level which has lint attributes.
1291 doc_hidden_stack: Vec<bool>,
1295 pub fn new() -> MissingDoc {
1297 struct_def_stack: vec!(),
1298 doc_hidden_stack: vec!(false),
1302 fn doc_hidden(&self) -> bool {
1303 *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
1306 fn check_missing_doc_attrs(&self,
1308 id: Option<ast::NodeId>,
1309 attrs: &[ast::Attribute],
1311 desc: &'static str) {
1312 // If we're building a test harness, then warning about
1313 // documentation is probably not really relevant right now.
1314 if cx.sess().opts.test { return }
1316 // `#[doc(hidden)]` disables missing_doc check.
1317 if self.doc_hidden() { return }
1319 // Only check publicly-visible items, using the result from the privacy pass.
1320 // It's an option so the crate root can also use this function (it doesn't
1323 Some(ref id) if !cx.exported_items.contains(id) => return,
1327 let has_doc = attrs.iter().any(|a| {
1328 match a.node.value.node {
1329 ast::MetaNameValue(ref name, _) if name.equiv(&("doc")) => true,
1334 cx.span_lint(MISSING_DOC, sp,
1335 format!("missing documentation for {}", desc).as_slice());
1340 impl LintPass for MissingDoc {
1341 fn get_lints(&self) -> LintArray {
1342 lint_array!(MISSING_DOC)
1345 fn enter_lint_attrs(&mut self, _: &Context, attrs: &[ast::Attribute]) {
1346 let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
1347 attr.check_name("doc") && match attr.meta_item_list() {
1349 Some(l) => attr::contains_name(l.as_slice(), "hidden"),
1352 self.doc_hidden_stack.push(doc_hidden);
1355 fn exit_lint_attrs(&mut self, _: &Context, _: &[ast::Attribute]) {
1356 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
1359 fn check_struct_def(&mut self, _: &Context,
1360 _: &ast::StructDef, _: ast::Ident, _: &ast::Generics, id: ast::NodeId) {
1361 self.struct_def_stack.push(id);
1364 fn check_struct_def_post(&mut self, _: &Context,
1365 _: &ast::StructDef, _: ast::Ident, _: &ast::Generics, id: ast::NodeId) {
1366 let popped = self.struct_def_stack.pop().expect("empty struct_def_stack");
1367 assert!(popped == id);
1370 fn check_crate(&mut self, cx: &Context, krate: &ast::Crate) {
1371 self.check_missing_doc_attrs(cx, None, krate.attrs.as_slice(),
1372 krate.span, "crate");
1375 fn check_item(&mut self, cx: &Context, it: &ast::Item) {
1376 let desc = match it.node {
1377 ast::ItemFn(..) => "a function",
1378 ast::ItemMod(..) => "a module",
1379 ast::ItemEnum(..) => "an enum",
1380 ast::ItemStruct(..) => "a struct",
1381 ast::ItemTrait(..) => "a trait",
1384 self.check_missing_doc_attrs(cx, Some(it.id), it.attrs.as_slice(),
1388 fn check_fn(&mut self, cx: &Context,
1389 fk: &visit::FnKind, _: &ast::FnDecl,
1390 _: &ast::Block, _: Span, _: ast::NodeId) {
1392 visit::FkMethod(_, _, m) => {
1393 // If the method is an impl for a trait, don't doc.
1394 if method_context(cx, m) == TraitImpl { return; }
1396 // Otherwise, doc according to privacy. This will also check
1397 // doc for default methods defined on traits.
1398 self.check_missing_doc_attrs(cx, Some(m.id), m.attrs.as_slice(),
1399 m.span, "a method");
1405 fn check_ty_method(&mut self, cx: &Context, tm: &ast::TypeMethod) {
1406 self.check_missing_doc_attrs(cx, Some(tm.id), tm.attrs.as_slice(),
1407 tm.span, "a type method");
1410 fn check_struct_field(&mut self, cx: &Context, sf: &ast::StructField) {
1411 match sf.node.kind {
1412 ast::NamedField(_, vis) if vis == ast::Public => {
1413 let cur_struct_def = *self.struct_def_stack.last()
1414 .expect("empty struct_def_stack");
1415 self.check_missing_doc_attrs(cx, Some(cur_struct_def),
1416 sf.node.attrs.as_slice(), sf.span,
1423 fn check_variant(&mut self, cx: &Context, v: &ast::Variant, _: &ast::Generics) {
1424 self.check_missing_doc_attrs(cx, Some(v.node.id), v.node.attrs.as_slice(),
1425 v.span, "a variant");
1429 declare_lint!(DEPRECATED, Warn,
1430 "detects use of #[deprecated] items")
1432 // FIXME #6875: Change to Warn after std library stabilization is complete
1433 declare_lint!(EXPERIMENTAL, Allow,
1434 "detects use of #[experimental] items")
1436 declare_lint!(UNSTABLE, Allow,
1437 "detects use of #[unstable] items (incl. items with no stability attribute)")
1439 /// Checks for use of items with `#[deprecated]`, `#[experimental]` and
1440 /// `#[unstable]` attributes, or no stability attribute.
1441 pub struct Stability;
1443 impl LintPass for Stability {
1444 fn get_lints(&self) -> LintArray {
1445 lint_array!(DEPRECATED, EXPERIMENTAL, UNSTABLE)
1448 fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
1449 // if the expression was produced by a macro expansion,
1450 if e.span.expn_info.is_some() { return }
1452 let id = match e.node {
1453 ast::ExprPath(..) | ast::ExprStruct(..) => {
1454 match cx.tcx.def_map.borrow().find(&e.id) {
1455 Some(&def) => def.def_id(),
1459 ast::ExprMethodCall(..) => {
1460 let method_call = typeck::MethodCall::expr(e.id);
1461 match cx.tcx.method_map.borrow().find(&method_call) {
1463 match method.origin {
1464 typeck::MethodStatic(def_id) => {
1467 typeck::MethodStaticUnboxedClosure(def_id) => {
1470 typeck::MethodParam(typeck::MethodParam {
1475 | typeck::MethodObject(typeck::MethodObject {
1480 match ty::trait_item(cx.tcx,
1483 ty::MethodTraitItem(method) => {
1496 let stability = stability::lookup(cx.tcx, id);
1497 let cross_crate = !ast_util::is_local(id);
1499 // stability attributes are promises made across crates; only
1500 // check DEPRECATED for crate-local usage.
1501 let (lint, label) = match stability {
1502 // no stability attributes == Unstable
1503 None if cross_crate => (UNSTABLE, "unmarked"),
1504 Some(attr::Stability { level: attr::Unstable, .. }) if cross_crate =>
1505 (UNSTABLE, "unstable"),
1506 Some(attr::Stability { level: attr::Experimental, .. }) if cross_crate =>
1507 (EXPERIMENTAL, "experimental"),
1508 Some(attr::Stability { level: attr::Deprecated, .. }) =>
1509 (DEPRECATED, "deprecated"),
1513 let msg = match stability {
1514 Some(attr::Stability { text: Some(ref s), .. }) => {
1515 format!("use of {} item: {}", label, *s)
1517 _ => format!("use of {} item", label)
1520 cx.span_lint(lint, e.span, msg.as_slice());
1524 declare_lint!(pub UNUSED_IMPORTS, Warn,
1525 "imports that are never used")
1527 declare_lint!(pub UNNECESSARY_QUALIFICATION, Allow,
1528 "detects unnecessarily qualified names")
1530 declare_lint!(pub UNRECOGNIZED_LINT, Warn,
1531 "unrecognized lint attribute")
1533 declare_lint!(pub UNUSED_VARIABLE, Warn,
1534 "detect variables which are not used in any way")
1536 declare_lint!(pub DEAD_ASSIGNMENT, Warn,
1537 "detect assignments that will never be read")
1539 declare_lint!(pub DEAD_CODE, Warn,
1540 "detect piece of code that will never be used")
1542 declare_lint!(pub VISIBLE_PRIVATE_TYPES, Warn,
1543 "detect use of private types in exported type signatures")
1545 declare_lint!(pub UNREACHABLE_CODE, Warn,
1546 "detects unreachable code")
1548 declare_lint!(pub WARNINGS, Warn,
1549 "mass-change the level for lints which produce warnings")
1551 declare_lint!(pub UNKNOWN_FEATURES, Deny,
1552 "unknown features found in crate-level #[feature] directives")
1554 declare_lint!(pub UNKNOWN_CRATE_TYPE, Deny,
1555 "unknown crate type found in #[crate_type] directive")
1557 declare_lint!(pub VARIANT_SIZE_DIFFERENCE, Allow,
1558 "detects enums with widely varying variant sizes")
1560 declare_lint!(pub TRANSMUTE_FAT_PTR, Allow,
1561 "detects transmutes of fat pointers")
1563 /// Does nothing as a lint pass, but registers some `Lint`s
1564 /// which are used by other parts of the compiler.
1565 pub struct HardwiredLints;
1567 impl LintPass for HardwiredLints {
1568 fn get_lints(&self) -> LintArray {
1571 UNNECESSARY_QUALIFICATION,
1576 VISIBLE_PRIVATE_TYPES,
1581 VARIANT_SIZE_DIFFERENCE