2015-02-25 22:44:44 +11:00
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-02-25 23:03:44 +11:00
|
|
|
//! Lints in the Rust compiler.
|
2015-02-25 22:44:44 +11:00
|
|
|
//!
|
2015-02-25 23:03:44 +11:00
|
|
|
//! This contains lints which can feasibly be implemented as their own
|
|
|
|
//! AST visitor. Also see `rustc::lint::builtin`, which contains the
|
|
|
|
//! definitions of lints that are emitted directly inside the main
|
|
|
|
//! compiler.
|
2015-02-25 22:44:44 +11:00
|
|
|
//!
|
|
|
|
//! To add a new lint to rustc, declare it here using `declare_lint!()`.
|
|
|
|
//! Then add code to emit the new lint in the appropriate circumstances.
|
2015-02-25 23:03:44 +11:00
|
|
|
//! You can do that in an existing `LintPass` if it makes sense, or in a
|
|
|
|
//! new `LintPass`, or using `Session::add_lint` elsewhere in the
|
|
|
|
//! compiler. Only do the latter if the check can't be written cleanly as a
|
|
|
|
//! `LintPass` (also, note that such lints will need to be defined in
|
|
|
|
//! `rustc::lint::builtin`, not here).
|
2015-02-25 22:44:44 +11:00
|
|
|
//!
|
|
|
|
//! If you define a new `LintPass`, you will also need to add it to the
|
2015-02-25 23:03:44 +11:00
|
|
|
//! `add_builtin!` or `add_builtin_with_new!` invocation in `lib.rs`.
|
2015-02-25 22:44:44 +11:00
|
|
|
//! Use the former for unit-like structs and the latter for structs with
|
|
|
|
//! a `pub fn new()`.
|
|
|
|
|
|
|
|
use metadata::{csearch, decoder};
|
|
|
|
use middle::def::*;
|
|
|
|
use middle::subst::Substs;
|
|
|
|
use middle::ty::{self, Ty};
|
|
|
|
use middle::{def, pat_util, stability};
|
|
|
|
use middle::const_eval::{eval_const_expr_partial, const_int, const_uint};
|
|
|
|
use middle::cfg;
|
2015-03-28 02:23:20 -07:00
|
|
|
use util::ppaux::ty_to_string;
|
2015-02-25 22:44:44 +11:00
|
|
|
use util::nodemap::{FnvHashMap, NodeSet};
|
|
|
|
use lint::{Level, Context, LintPass, LintArray, Lint};
|
|
|
|
|
2015-03-29 23:41:54 -04:00
|
|
|
use std::collections::{HashSet, BitSet};
|
2015-02-25 22:44:44 +11:00
|
|
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
|
|
|
use std::{cmp, slice};
|
|
|
|
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
|
|
|
|
|
|
|
|
use syntax::{abi, ast, ast_map};
|
2015-03-11 23:38:58 +02:00
|
|
|
use syntax::ast_util::{self, is_shift_binop, local_def};
|
2015-02-25 22:44:44 +11:00
|
|
|
use syntax::attr::{self, AttrMetaMethods};
|
|
|
|
use syntax::codemap::{self, Span};
|
|
|
|
use syntax::feature_gate::{KNOWN_ATTRIBUTES, AttributeType};
|
|
|
|
use syntax::parse::token;
|
|
|
|
use syntax::ast::{TyIs, TyUs, TyI8, TyU8, TyI16, TyU16, TyI32, TyU32, TyI64, TyU64};
|
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax::visit::{self, Visitor};
|
|
|
|
|
|
|
|
// hardwired lints from librustc
|
|
|
|
pub use lint::builtin::*;
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
WHILE_TRUE,
|
|
|
|
Warn,
|
|
|
|
"suggest using `loop { }` instead of `while true { }`"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct WhileTrue;
|
|
|
|
|
|
|
|
impl LintPass for WhileTrue {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(WHILE_TRUE)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
if let ast::ExprWhile(ref cond, _, _) = e.node {
|
|
|
|
if let ast::ExprLit(ref lit) = cond.node {
|
|
|
|
if let ast::LitBool(true) = lit.node {
|
|
|
|
cx.span_lint(WHILE_TRUE, e.span,
|
|
|
|
"denote infinite loops with loop { ... }");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNSIGNED_NEGATION,
|
|
|
|
Warn,
|
|
|
|
"using an unary minus operator on unsigned type"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNUSED_COMPARISONS,
|
|
|
|
Warn,
|
|
|
|
"comparisons made useless by limits of the types involved"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
OVERFLOWING_LITERALS,
|
|
|
|
Warn,
|
|
|
|
"literal out of range for its type"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
EXCEEDING_BITSHIFTS,
|
|
|
|
Deny,
|
|
|
|
"shift exceeds the type's number of bits"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct TypeLimits {
|
|
|
|
/// Id of the last visited negated expression
|
|
|
|
negated_expr_id: ast::NodeId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeLimits {
|
|
|
|
pub fn new() -> TypeLimits {
|
|
|
|
TypeLimits {
|
2015-04-01 19:53:32 +02:00
|
|
|
negated_expr_id: !0,
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for TypeLimits {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNSIGNED_NEGATION, UNUSED_COMPARISONS, OVERFLOWING_LITERALS,
|
|
|
|
EXCEEDING_BITSHIFTS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
match e.node {
|
|
|
|
ast::ExprUnary(ast::UnNeg, ref expr) => {
|
|
|
|
match expr.node {
|
|
|
|
ast::ExprLit(ref lit) => {
|
|
|
|
match lit.node {
|
|
|
|
ast::LitInt(_, ast::UnsignedIntLit(_)) => {
|
|
|
|
cx.span_lint(UNSIGNED_NEGATION, e.span,
|
|
|
|
"negation of unsigned int literal may \
|
|
|
|
be unintentional");
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
let t = ty::expr_ty(cx.tcx, &**expr);
|
|
|
|
match t.sty {
|
|
|
|
ty::ty_uint(_) => {
|
|
|
|
cx.span_lint(UNSIGNED_NEGATION, e.span,
|
|
|
|
"negation of unsigned int variable may \
|
|
|
|
be unintentional");
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// propagate negation, if the negation itself isn't negated
|
|
|
|
if self.negated_expr_id != e.id {
|
|
|
|
self.negated_expr_id = expr.id;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::ExprParen(ref expr) if self.negated_expr_id == e.id => {
|
|
|
|
self.negated_expr_id = expr.id;
|
|
|
|
},
|
|
|
|
ast::ExprBinary(binop, ref l, ref r) => {
|
|
|
|
if is_comparison(binop) && !check_limits(cx.tcx, binop, &**l, &**r) {
|
|
|
|
cx.span_lint(UNUSED_COMPARISONS, e.span,
|
|
|
|
"comparison is useless due to type limits");
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_shift_binop(binop.node) {
|
|
|
|
let opt_ty_bits = match ty::expr_ty(cx.tcx, &**l).sty {
|
|
|
|
ty::ty_int(t) => Some(int_ty_bits(t, cx.sess().target.int_type)),
|
|
|
|
ty::ty_uint(t) => Some(uint_ty_bits(t, cx.sess().target.uint_type)),
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(bits) = opt_ty_bits {
|
|
|
|
let exceeding = if let ast::ExprLit(ref lit) = r.node {
|
|
|
|
if let ast::LitInt(shift, _) = lit.node { shift >= bits }
|
|
|
|
else { false }
|
|
|
|
} else {
|
2015-03-25 17:06:52 -07:00
|
|
|
match eval_const_expr_partial(cx.tcx, &**r, Some(cx.tcx.types.usize)) {
|
2015-02-25 22:44:44 +11:00
|
|
|
Ok(const_int(shift)) => { shift as u64 >= bits },
|
|
|
|
Ok(const_uint(shift)) => { shift >= bits },
|
|
|
|
_ => { false }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if exceeding {
|
|
|
|
cx.span_lint(EXCEEDING_BITSHIFTS, e.span,
|
|
|
|
"bitshift exceeds the type's number of bits");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::ExprLit(ref lit) => {
|
|
|
|
match ty::expr_ty(cx.tcx, e).sty {
|
|
|
|
ty::ty_int(t) => {
|
|
|
|
match lit.node {
|
|
|
|
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
|
|
|
|
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => {
|
2015-03-24 13:26:16 -07:00
|
|
|
let int_type = if let ast::TyIs = t {
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.sess().target.int_type
|
2015-02-28 13:31:14 +01:00
|
|
|
} else {
|
|
|
|
t
|
|
|
|
};
|
2015-05-25 07:42:16 -06:00
|
|
|
let (_, max) = int_ty_range(int_type);
|
2015-02-25 22:44:44 +11:00
|
|
|
let negative = self.negated_expr_id == e.id;
|
|
|
|
|
2015-05-25 07:42:16 -06:00
|
|
|
// Detect literal value out of range [min, max] inclusive
|
|
|
|
// avoiding use of -min to prevent overflow/panic
|
|
|
|
if (negative && v > max as u64 + 1) ||
|
2015-05-19 19:43:33 +03:00
|
|
|
(!negative && v > max as u64) {
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.span_lint(OVERFLOWING_LITERALS, e.span,
|
|
|
|
&*format!("literal out of range for {:?}", t));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
ty::ty_uint(t) => {
|
2015-03-24 13:26:16 -07:00
|
|
|
let uint_type = if let ast::TyUs = t {
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.sess().target.uint_type
|
2015-02-28 13:31:14 +01:00
|
|
|
} else {
|
|
|
|
t
|
|
|
|
};
|
2015-02-25 22:44:44 +11:00
|
|
|
let (min, max) = uint_ty_range(uint_type);
|
|
|
|
let lit_val: u64 = match lit.node {
|
|
|
|
ast::LitByte(_v) => return, // _v is u8, within range by definition
|
|
|
|
ast::LitInt(v, _) => v,
|
|
|
|
_ => panic!()
|
|
|
|
};
|
2015-02-28 13:31:14 +01:00
|
|
|
if lit_val < min || lit_val > max {
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.span_lint(OVERFLOWING_LITERALS, e.span,
|
|
|
|
&*format!("literal out of range for {:?}", t));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ty::ty_float(t) => {
|
|
|
|
let (min, max) = float_ty_range(t);
|
|
|
|
let lit_val: f64 = match lit.node {
|
|
|
|
ast::LitFloat(ref v, _) |
|
|
|
|
ast::LitFloatUnsuffixed(ref v) => {
|
2015-02-28 13:31:14 +01:00
|
|
|
match v.parse() {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(_) => return
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!()
|
|
|
|
};
|
|
|
|
if lit_val < min || lit_val > max {
|
|
|
|
cx.span_lint(OVERFLOWING_LITERALS, e.span,
|
|
|
|
&*format!("literal out of range for {:?}", t));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
|
|
|
|
fn is_valid<T:cmp::PartialOrd>(binop: ast::BinOp, v: T,
|
|
|
|
min: T, max: T) -> bool {
|
|
|
|
match binop.node {
|
|
|
|
ast::BiLt => v > min && v <= max,
|
|
|
|
ast::BiLe => v >= min && v < max,
|
|
|
|
ast::BiGt => v >= min && v < max,
|
|
|
|
ast::BiGe => v > min && v <= max,
|
|
|
|
ast::BiEq | ast::BiNe => v >= min && v <= max,
|
|
|
|
_ => panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rev_binop(binop: ast::BinOp) -> ast::BinOp {
|
|
|
|
codemap::respan(binop.span, match binop.node {
|
|
|
|
ast::BiLt => ast::BiGt,
|
|
|
|
ast::BiLe => ast::BiGe,
|
|
|
|
ast::BiGt => ast::BiLt,
|
|
|
|
ast::BiGe => ast::BiLe,
|
|
|
|
_ => return binop
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:31:14 +01:00
|
|
|
// for isize & usize, be conservative with the warnings, so that the
|
2015-02-25 22:44:44 +11:00
|
|
|
// warnings are consistent between 32- and 64-bit platforms
|
|
|
|
fn int_ty_range(int_ty: ast::IntTy) -> (i64, i64) {
|
|
|
|
match int_ty {
|
2015-03-24 13:26:16 -07:00
|
|
|
ast::TyIs => (i64::MIN, i64::MAX),
|
2015-02-28 13:31:14 +01:00
|
|
|
ast::TyI8 => (i8::MIN as i64, i8::MAX as i64),
|
|
|
|
ast::TyI16 => (i16::MIN as i64, i16::MAX as i64),
|
|
|
|
ast::TyI32 => (i32::MIN as i64, i32::MAX as i64),
|
|
|
|
ast::TyI64 => (i64::MIN, i64::MAX)
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn uint_ty_range(uint_ty: ast::UintTy) -> (u64, u64) {
|
|
|
|
match uint_ty {
|
2015-03-24 13:26:16 -07:00
|
|
|
ast::TyUs => (u64::MIN, u64::MAX),
|
2015-02-28 13:31:14 +01:00
|
|
|
ast::TyU8 => (u8::MIN as u64, u8::MAX as u64),
|
|
|
|
ast::TyU16 => (u16::MIN as u64, u16::MAX as u64),
|
|
|
|
ast::TyU32 => (u32::MIN as u64, u32::MAX as u64),
|
|
|
|
ast::TyU64 => (u64::MIN, u64::MAX)
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn float_ty_range(float_ty: ast::FloatTy) -> (f64, f64) {
|
|
|
|
match float_ty {
|
2015-02-28 13:31:14 +01:00
|
|
|
ast::TyF32 => (f32::MIN as f64, f32::MAX as f64),
|
|
|
|
ast::TyF64 => (f64::MIN, f64::MAX)
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn int_ty_bits(int_ty: ast::IntTy, target_int_ty: ast::IntTy) -> u64 {
|
|
|
|
match int_ty {
|
2015-03-24 13:26:16 -07:00
|
|
|
ast::TyIs => int_ty_bits(target_int_ty, target_int_ty),
|
2015-02-28 13:31:14 +01:00
|
|
|
ast::TyI8 => i8::BITS as u64,
|
|
|
|
ast::TyI16 => i16::BITS as u64,
|
|
|
|
ast::TyI32 => i32::BITS as u64,
|
|
|
|
ast::TyI64 => i64::BITS as u64
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn uint_ty_bits(uint_ty: ast::UintTy, target_uint_ty: ast::UintTy) -> u64 {
|
|
|
|
match uint_ty {
|
2015-03-24 13:26:16 -07:00
|
|
|
ast::TyUs => uint_ty_bits(target_uint_ty, target_uint_ty),
|
2015-02-28 13:31:14 +01:00
|
|
|
ast::TyU8 => u8::BITS as u64,
|
|
|
|
ast::TyU16 => u16::BITS as u64,
|
|
|
|
ast::TyU32 => u32::BITS as u64,
|
|
|
|
ast::TyU64 => u64::BITS as u64
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_limits(tcx: &ty::ctxt, binop: ast::BinOp,
|
|
|
|
l: &ast::Expr, r: &ast::Expr) -> bool {
|
|
|
|
let (lit, expr, swap) = match (&l.node, &r.node) {
|
|
|
|
(&ast::ExprLit(_), _) => (l, r, true),
|
|
|
|
(_, &ast::ExprLit(_)) => (r, l, false),
|
|
|
|
_ => return true
|
|
|
|
};
|
|
|
|
// Normalize the binop so that the literal is always on the RHS in
|
|
|
|
// the comparison
|
2015-02-28 13:31:14 +01:00
|
|
|
let norm_binop = if swap {
|
|
|
|
rev_binop(binop)
|
|
|
|
} else {
|
|
|
|
binop
|
|
|
|
};
|
2015-02-25 22:44:44 +11:00
|
|
|
match ty::expr_ty(tcx, expr).sty {
|
|
|
|
ty::ty_int(int_ty) => {
|
|
|
|
let (min, max) = int_ty_range(int_ty);
|
|
|
|
let lit_val: i64 = match lit.node {
|
|
|
|
ast::ExprLit(ref li) => match li.node {
|
|
|
|
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
|
|
|
|
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Plus)) => v as i64,
|
|
|
|
ast::LitInt(v, ast::SignedIntLit(_, ast::Minus)) |
|
|
|
|
ast::LitInt(v, ast::UnsuffixedIntLit(ast::Minus)) => -(v as i64),
|
|
|
|
_ => return true
|
|
|
|
},
|
|
|
|
_ => panic!()
|
|
|
|
};
|
|
|
|
is_valid(norm_binop, lit_val, min, max)
|
|
|
|
}
|
|
|
|
ty::ty_uint(uint_ty) => {
|
|
|
|
let (min, max): (u64, u64) = uint_ty_range(uint_ty);
|
|
|
|
let lit_val: u64 = match lit.node {
|
|
|
|
ast::ExprLit(ref li) => match li.node {
|
|
|
|
ast::LitInt(v, _) => v,
|
|
|
|
_ => return true
|
|
|
|
},
|
|
|
|
_ => panic!()
|
|
|
|
};
|
|
|
|
is_valid(norm_binop, lit_val, min, max)
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_comparison(binop: ast::BinOp) -> bool {
|
|
|
|
match binop.node {
|
|
|
|
ast::BiEq | ast::BiLt | ast::BiLe |
|
|
|
|
ast::BiNe | ast::BiGe | ast::BiGt => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
IMPROPER_CTYPES,
|
|
|
|
Warn,
|
|
|
|
"proper use of libc types in foreign modules"
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ImproperCTypesVisitor<'a, 'tcx: 'a> {
|
|
|
|
cx: &'a Context<'a, 'tcx>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
|
|
|
|
fn check_def(&mut self, sp: Span, id: ast::NodeId) {
|
2015-03-21 21:15:47 -04:00
|
|
|
match self.cx.tcx.def_map.borrow().get(&id).unwrap().full_def() {
|
2015-03-24 13:26:16 -07:00
|
|
|
def::DefPrimTy(ast::TyInt(ast::TyIs)) => {
|
2015-02-25 22:44:44 +11:00
|
|
|
self.cx.span_lint(IMPROPER_CTYPES, sp,
|
|
|
|
"found rust type `isize` in foreign module, while \
|
|
|
|
libc::c_int or libc::c_long should be used");
|
|
|
|
}
|
2015-03-24 13:26:16 -07:00
|
|
|
def::DefPrimTy(ast::TyUint(ast::TyUs)) => {
|
2015-02-25 22:44:44 +11:00
|
|
|
self.cx.span_lint(IMPROPER_CTYPES, sp,
|
|
|
|
"found rust type `usize` in foreign module, while \
|
|
|
|
libc::c_uint or libc::c_ulong should be used");
|
|
|
|
}
|
|
|
|
def::DefTy(..) => {
|
|
|
|
let tty = match self.cx.tcx.ast_ty_to_ty_cache.borrow().get(&id) {
|
2015-02-24 09:24:42 -05:00
|
|
|
Some(&t) => t,
|
|
|
|
None => panic!("ast_ty_to_ty_cache was incomplete after typeck!")
|
2015-02-25 22:44:44 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
if !ty::is_ffi_safe(self.cx.tcx, tty) {
|
|
|
|
self.cx.span_lint(IMPROPER_CTYPES, sp,
|
2015-02-28 13:31:14 +01:00
|
|
|
"found type without foreign-function-safe \
|
|
|
|
representation annotation in foreign module, consider \
|
|
|
|
adding a #[repr(...)] attribute to the type");
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for ImproperCTypesVisitor<'a, 'tcx> {
|
|
|
|
fn visit_ty(&mut self, ty: &ast::Ty) {
|
|
|
|
if let ast::TyPath(..) = ty.node {
|
|
|
|
self.check_def(ty.span, ty.id);
|
|
|
|
}
|
|
|
|
visit::walk_ty(self, ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct ImproperCTypes;
|
|
|
|
|
|
|
|
impl LintPass for ImproperCTypes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(IMPROPER_CTYPES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
fn check_ty(cx: &Context, ty: &ast::Ty) {
|
|
|
|
let mut vis = ImproperCTypesVisitor { cx: cx };
|
|
|
|
vis.visit_ty(ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_foreign_fn(cx: &Context, decl: &ast::FnDecl) {
|
|
|
|
for input in &decl.inputs {
|
|
|
|
check_ty(cx, &*input.ty);
|
|
|
|
}
|
|
|
|
if let ast::Return(ref ret_ty) = decl.output {
|
|
|
|
check_ty(cx, &**ret_ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match it.node {
|
|
|
|
ast::ItemForeignMod(ref nmod) if nmod.abi != abi::RustIntrinsic => {
|
|
|
|
for ni in &nmod.items {
|
|
|
|
match ni.node {
|
|
|
|
ast::ForeignItemFn(ref decl, _) => check_foreign_fn(cx, &**decl),
|
|
|
|
ast::ForeignItemStatic(ref t, _) => check_ty(cx, &**t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
BOX_POINTERS,
|
|
|
|
Allow,
|
|
|
|
"use of owned (Box type) heap memory"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct BoxPointers;
|
|
|
|
|
|
|
|
impl BoxPointers {
|
|
|
|
fn check_heap_type<'a, 'tcx>(&self, cx: &Context<'a, 'tcx>,
|
|
|
|
span: Span, ty: Ty<'tcx>) {
|
|
|
|
let mut n_uniq: usize = 0;
|
|
|
|
ty::fold_ty(cx.tcx, ty, |t| {
|
|
|
|
match t.sty {
|
|
|
|
ty::ty_uniq(_) => {
|
|
|
|
n_uniq += 1;
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
};
|
|
|
|
t
|
|
|
|
});
|
|
|
|
|
|
|
|
if n_uniq > 0 {
|
|
|
|
let s = ty_to_string(cx.tcx, ty);
|
|
|
|
let m = format!("type uses owned (Box type) pointers: {}", s);
|
|
|
|
cx.span_lint(BOX_POINTERS, span, &m[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for BoxPointers {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(BOX_POINTERS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
match it.node {
|
|
|
|
ast::ItemFn(..) |
|
|
|
|
ast::ItemTy(..) |
|
|
|
|
ast::ItemEnum(..) |
|
|
|
|
ast::ItemStruct(..) =>
|
|
|
|
self.check_heap_type(cx, it.span,
|
|
|
|
ty::node_id_to_type(cx.tcx, it.id)),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a struct, we also have to check the fields' types
|
|
|
|
match it.node {
|
|
|
|
ast::ItemStruct(ref struct_def, _) => {
|
|
|
|
for struct_field in &struct_def.fields {
|
|
|
|
self.check_heap_type(cx, struct_field.span,
|
|
|
|
ty::node_id_to_type(cx.tcx, struct_field.node.id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
let ty = ty::expr_ty(cx.tcx, e);
|
|
|
|
self.check_heap_type(cx, e.span, ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
RAW_POINTER_DERIVE,
|
|
|
|
Warn,
|
|
|
|
"uses of #[derive] with raw pointers are rarely correct"
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RawPtrDeriveVisitor<'a, 'tcx: 'a> {
|
|
|
|
cx: &'a Context<'a, 'tcx>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for RawPtrDeriveVisitor<'a, 'tcx> {
|
|
|
|
fn visit_ty(&mut self, ty: &ast::Ty) {
|
2015-02-27 15:36:53 +01:00
|
|
|
const MSG: &'static str = "use of `#[derive]` with a raw pointer";
|
2015-02-25 22:44:44 +11:00
|
|
|
if let ast::TyPtr(..) = ty.node {
|
|
|
|
self.cx.span_lint(RAW_POINTER_DERIVE, ty.span, MSG);
|
|
|
|
}
|
|
|
|
visit::walk_ty(self, ty);
|
|
|
|
}
|
|
|
|
// explicit override to a no-op to reduce code bloat
|
|
|
|
fn visit_expr(&mut self, _: &ast::Expr) {}
|
|
|
|
fn visit_block(&mut self, _: &ast::Block) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RawPointerDerive {
|
|
|
|
checked_raw_pointers: NodeSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawPointerDerive {
|
|
|
|
pub fn new() -> RawPointerDerive {
|
|
|
|
RawPointerDerive {
|
|
|
|
checked_raw_pointers: NodeSet(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for RawPointerDerive {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(RAW_POINTER_DERIVE)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, item: &ast::Item) {
|
|
|
|
if !attr::contains_name(&item.attrs, "automatically_derived") {
|
2015-02-28 13:31:14 +01:00
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
let did = match item.node {
|
|
|
|
ast::ItemImpl(_, _, _, ref t_ref_opt, _, _) => {
|
|
|
|
// Deriving the Copy trait does not cause a warning
|
|
|
|
if let &Some(ref trait_ref) = t_ref_opt {
|
|
|
|
let def_id = ty::trait_ref_to_def_id(cx.tcx, trait_ref);
|
|
|
|
if Some(def_id) == cx.tcx.lang_items.copy_trait() {
|
2015-02-28 13:31:14 +01:00
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match ty::node_id_to_type(cx.tcx, item.id).sty {
|
|
|
|
ty::ty_enum(did, _) => did,
|
|
|
|
ty::ty_struct(did, _) => did,
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
2015-02-28 13:31:14 +01:00
|
|
|
if !ast_util::is_local(did) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
let item = match cx.tcx.map.find(did.node) {
|
|
|
|
Some(ast_map::NodeItem(item)) => item,
|
|
|
|
_ => return,
|
|
|
|
};
|
2015-02-28 13:31:14 +01:00
|
|
|
if !self.checked_raw_pointers.insert(item.id) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
match item.node {
|
|
|
|
ast::ItemStruct(..) | ast::ItemEnum(..) => {
|
|
|
|
let mut visitor = RawPtrDeriveVisitor { cx: cx };
|
|
|
|
visit::walk_item(&mut visitor, &*item);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
Warn,
|
|
|
|
"detects attributes that were not used by the compiler"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnusedAttributes;
|
|
|
|
|
|
|
|
impl LintPass for UnusedAttributes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_ATTRIBUTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_attribute(&mut self, cx: &Context, attr: &ast::Attribute) {
|
2015-02-28 13:31:27 +01:00
|
|
|
// Note that check_name() marks the attribute as used if it matches.
|
2015-02-25 22:44:44 +11:00
|
|
|
for &(ref name, ty) in KNOWN_ATTRIBUTES {
|
|
|
|
match ty {
|
|
|
|
AttributeType::Whitelisted
|
|
|
|
| AttributeType::Gated(_, _) if attr.check_name(name) => {
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 22:08:36 +05:30
|
|
|
let plugin_attributes = cx.sess().plugin_attributes.borrow_mut();
|
|
|
|
for &(ref name, ty) in plugin_attributes.iter() {
|
2015-05-13 12:23:43 +05:30
|
|
|
if ty == AttributeType::Whitelisted && attr.check_name(&*name) {
|
|
|
|
break;
|
2015-05-06 22:08:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:44:44 +11:00
|
|
|
if !attr::is_used(attr) {
|
|
|
|
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
|
2015-05-07 15:31:20 +05:30
|
|
|
// Is it a builtin attribute that must be used at the crate level?
|
|
|
|
let known_crate = KNOWN_ATTRIBUTES.contains(&(&attr.name(),
|
|
|
|
AttributeType::CrateLevel));
|
|
|
|
// Has a plugin registered this attribute as one which must be used at
|
|
|
|
// the crate level?
|
|
|
|
let plugin_crate = plugin_attributes.iter()
|
|
|
|
.find(|&&(ref x, t)| {
|
|
|
|
&*attr.name() == &*x &&
|
|
|
|
AttributeType::CrateLevel == t
|
|
|
|
}).is_some();
|
|
|
|
if known_crate || plugin_crate {
|
2015-02-25 22:44:44 +11:00
|
|
|
let msg = match attr.node.style {
|
|
|
|
ast::AttrOuter => "crate-level attribute should be an inner \
|
|
|
|
attribute: add an exclamation mark: #![foo]",
|
|
|
|
ast::AttrInner => "crate-level attribute should be in the \
|
|
|
|
root module",
|
|
|
|
};
|
|
|
|
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub PATH_STATEMENTS,
|
|
|
|
Warn,
|
|
|
|
"path statements with no effect"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct PathStatements;
|
|
|
|
|
|
|
|
impl LintPass for PathStatements {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(PATH_STATEMENTS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
|
|
|
|
match s.node {
|
|
|
|
ast::StmtSemi(ref expr, _) => {
|
|
|
|
match expr.node {
|
|
|
|
ast::ExprPath(..) => cx.span_lint(PATH_STATEMENTS, s.span,
|
|
|
|
"path statement with no effect"),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_MUST_USE,
|
|
|
|
Warn,
|
|
|
|
"unused result of a type flagged as #[must_use]"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_RESULTS,
|
|
|
|
Allow,
|
|
|
|
"unused result of an expression in a statement"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnusedResults;
|
|
|
|
|
|
|
|
impl LintPass for UnusedResults {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_MUST_USE, UNUSED_RESULTS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
|
|
|
|
let expr = match s.node {
|
|
|
|
ast::StmtSemi(ref expr, _) => &**expr,
|
|
|
|
_ => return
|
|
|
|
};
|
|
|
|
|
|
|
|
if let ast::ExprRet(..) = expr.node {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let t = ty::expr_ty(cx.tcx, expr);
|
2015-02-28 13:31:55 +01:00
|
|
|
let warned = match t.sty {
|
2015-02-25 22:44:44 +11:00
|
|
|
ty::ty_tup(ref tys) if tys.is_empty() => return,
|
|
|
|
ty::ty_bool => return,
|
|
|
|
ty::ty_struct(did, _) |
|
|
|
|
ty::ty_enum(did, _) => {
|
|
|
|
if ast_util::is_local(did) {
|
|
|
|
if let ast_map::NodeItem(it) = cx.tcx.map.get(did.node) {
|
2015-02-28 13:31:55 +01:00
|
|
|
check_must_use(cx, &it.attrs, s.span)
|
|
|
|
} else {
|
|
|
|
false
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let attrs = csearch::get_item_attrs(&cx.sess().cstore, did);
|
2015-02-28 13:31:55 +01:00
|
|
|
check_must_use(cx, &attrs[..], s.span)
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
2015-02-28 13:31:55 +01:00
|
|
|
_ => false,
|
|
|
|
};
|
2015-02-25 22:44:44 +11:00
|
|
|
if !warned {
|
|
|
|
cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_must_use(cx: &Context, attrs: &[ast::Attribute], sp: Span) -> bool {
|
|
|
|
for attr in attrs {
|
|
|
|
if attr.check_name("must_use") {
|
|
|
|
let mut msg = "unused result which must be used".to_string();
|
|
|
|
// check for #[must_use="..."]
|
|
|
|
match attr.value_str() {
|
|
|
|
None => {}
|
|
|
|
Some(s) => {
|
|
|
|
msg.push_str(": ");
|
|
|
|
msg.push_str(&s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cx.span_lint(UNUSED_MUST_USE, sp, &msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub NON_CAMEL_CASE_TYPES,
|
|
|
|
Warn,
|
|
|
|
"types, variants, traits and type parameters should have camel case names"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct NonCamelCaseTypes;
|
|
|
|
|
|
|
|
impl NonCamelCaseTypes {
|
|
|
|
fn check_case(&self, cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
|
|
|
|
fn is_camel_case(ident: ast::Ident) -> bool {
|
|
|
|
let ident = token::get_ident(ident);
|
2015-02-28 13:31:14 +01:00
|
|
|
if ident.is_empty() {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
let ident = ident.trim_matches('_');
|
|
|
|
|
|
|
|
// start with a non-lowercase letter rather than non-uppercase
|
|
|
|
// ones (some scripts don't have a concept of upper/lowercase)
|
2015-03-24 16:54:09 -07:00
|
|
|
!ident.is_empty() && !ident.char_at(0).is_lowercase() && !ident.contains('_')
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_camel_case(s: &str) -> String {
|
|
|
|
s.split('_').flat_map(|word| word.chars().enumerate().map(|(i, c)|
|
2015-02-28 13:31:14 +01:00
|
|
|
if i == 0 {
|
2015-03-05 18:23:57 -08:00
|
|
|
c.to_uppercase().collect::<String>()
|
2015-02-28 13:31:14 +01:00
|
|
|
} else {
|
2015-03-14 13:40:33 +08:00
|
|
|
c.to_lowercase().collect()
|
2015-02-28 13:31:14 +01:00
|
|
|
}
|
2015-03-05 18:23:57 -08:00
|
|
|
)).collect::<Vec<_>>().concat()
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
let s = token::get_ident(ident);
|
|
|
|
|
|
|
|
if !is_camel_case(ident) {
|
|
|
|
let c = to_camel_case(&s);
|
|
|
|
let m = if c.is_empty() {
|
|
|
|
format!("{} `{}` should have a camel case name such as `CamelCase`", sort, s)
|
|
|
|
} else {
|
|
|
|
format!("{} `{}` should have a camel case name such as `{}`", sort, s, c)
|
|
|
|
};
|
|
|
|
cx.span_lint(NON_CAMEL_CASE_TYPES, span, &m[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for NonCamelCaseTypes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_CAMEL_CASE_TYPES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
2015-02-28 13:31:14 +01:00
|
|
|
let has_extern_repr = it.attrs.iter().any(|attr| {
|
2015-02-25 22:44:44 +11:00
|
|
|
attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr).iter()
|
|
|
|
.any(|r| r == &attr::ReprExtern)
|
2015-02-28 13:31:14 +01:00
|
|
|
});
|
|
|
|
if has_extern_repr {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
|
|
|
|
match it.node {
|
|
|
|
ast::ItemTy(..) | ast::ItemStruct(..) => {
|
|
|
|
self.check_case(cx, "type", it.ident, it.span)
|
|
|
|
}
|
|
|
|
ast::ItemTrait(..) => {
|
|
|
|
self.check_case(cx, "trait", it.ident, it.span)
|
|
|
|
}
|
|
|
|
ast::ItemEnum(ref enum_definition, _) => {
|
2015-02-28 13:31:14 +01:00
|
|
|
if has_extern_repr {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
self.check_case(cx, "type", it.ident, it.span);
|
|
|
|
for variant in &enum_definition.variants {
|
|
|
|
self.check_case(cx, "variant", variant.node.name, variant.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_generics(&mut self, cx: &Context, it: &ast::Generics) {
|
|
|
|
for gen in &*it.ty_params {
|
|
|
|
self.check_case(cx, "type parameter", gen.ident, gen.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
enum MethodContext {
|
|
|
|
TraitDefaultImpl,
|
|
|
|
TraitImpl,
|
|
|
|
PlainImpl
|
|
|
|
}
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
fn method_context(cx: &Context, id: ast::NodeId, span: Span) -> MethodContext {
|
|
|
|
match cx.tcx.impl_or_trait_items.borrow().get(&local_def(id)) {
|
|
|
|
None => cx.sess().span_bug(span, "missing method descriptor?!"),
|
|
|
|
Some(item) => match item.container() {
|
|
|
|
ty::TraitContainer(..) => MethodContext::TraitDefaultImpl,
|
|
|
|
ty::ImplContainer(cid) => {
|
|
|
|
match ty::impl_trait_ref(cx.tcx, cid) {
|
|
|
|
Some(_) => MethodContext::TraitImpl,
|
|
|
|
None => MethodContext::PlainImpl
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
2015-02-28 13:32:18 +01:00
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub NON_SNAKE_CASE,
|
|
|
|
Warn,
|
|
|
|
"methods, functions, lifetime parameters and modules should have snake case names"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct NonSnakeCase;
|
|
|
|
|
|
|
|
impl NonSnakeCase {
|
|
|
|
fn to_snake_case(mut str: &str) -> String {
|
|
|
|
let mut words = vec![];
|
|
|
|
// Preserve leading underscores
|
|
|
|
str = str.trim_left_matches(|c: char| {
|
|
|
|
if c == '_' {
|
|
|
|
words.push(String::new());
|
|
|
|
true
|
2015-02-28 13:31:14 +01:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
});
|
|
|
|
for s in str.split('_') {
|
|
|
|
let mut last_upper = false;
|
|
|
|
let mut buf = String::new();
|
2015-02-28 13:31:14 +01:00
|
|
|
if s.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
for ch in s.chars() {
|
|
|
|
if !buf.is_empty() && buf != "'"
|
|
|
|
&& ch.is_uppercase()
|
|
|
|
&& !last_upper {
|
|
|
|
words.push(buf);
|
|
|
|
buf = String::new();
|
|
|
|
}
|
|
|
|
last_upper = ch.is_uppercase();
|
2015-03-05 18:23:57 -08:00
|
|
|
buf.extend(ch.to_lowercase());
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
words.push(buf);
|
|
|
|
}
|
|
|
|
words.connect("_")
|
|
|
|
}
|
|
|
|
|
2015-04-18 13:39:10 +12:00
|
|
|
fn check_snake_case(&self, cx: &Context, sort: &str, name: &str, span: Option<Span>) {
|
|
|
|
fn is_snake_case(ident: &str) -> bool {
|
2015-02-28 13:31:14 +01:00
|
|
|
if ident.is_empty() {
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
let ident = ident.trim_left_matches('\'');
|
|
|
|
let ident = ident.trim_matches('_');
|
|
|
|
|
|
|
|
let mut allow_underscore = true;
|
|
|
|
ident.chars().all(|c| {
|
|
|
|
allow_underscore = match c {
|
|
|
|
'_' if !allow_underscore => return false,
|
|
|
|
'_' => false,
|
2015-04-20 22:55:36 +02:00
|
|
|
// It would be more obvious to use `c.is_lowercase()`,
|
|
|
|
// but some characters do not have a lowercase form
|
2015-02-25 22:44:44 +11:00
|
|
|
c if !c.is_uppercase() => true,
|
|
|
|
_ => return false,
|
|
|
|
};
|
|
|
|
true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-18 13:39:10 +12:00
|
|
|
if !is_snake_case(name) {
|
|
|
|
let sc = NonSnakeCase::to_snake_case(name);
|
|
|
|
let msg = if sc != name {
|
|
|
|
format!("{} `{}` should have a snake case name such as `{}`",
|
|
|
|
sort, name, sc)
|
2015-02-25 22:44:44 +11:00
|
|
|
} else {
|
2015-04-18 13:39:10 +12:00
|
|
|
format!("{} `{}` should have a snake case name",
|
|
|
|
sort, name)
|
|
|
|
};
|
|
|
|
match span {
|
|
|
|
Some(span) => cx.span_lint(NON_SNAKE_CASE, span, &msg),
|
|
|
|
None => cx.lint(NON_SNAKE_CASE, &msg),
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for NonSnakeCase {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_SNAKE_CASE)
|
|
|
|
}
|
|
|
|
|
2015-04-18 13:39:10 +12:00
|
|
|
fn check_crate(&mut self, cx: &Context, cr: &ast::Crate) {
|
|
|
|
let attr_crate_name = cr.attrs.iter().find(|at| at.check_name("crate_name"))
|
|
|
|
.and_then(|at| at.value_str().map(|s| (at, s)));
|
|
|
|
if let Some(ref name) = cx.tcx.sess.opts.crate_name {
|
|
|
|
self.check_snake_case(cx, "crate", name, None);
|
|
|
|
} else if let Some((attr, ref name)) = attr_crate_name {
|
|
|
|
self.check_snake_case(cx, "crate", name, Some(attr.span));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:44:44 +11:00
|
|
|
fn check_fn(&mut self, cx: &Context,
|
|
|
|
fk: visit::FnKind, _: &ast::FnDecl,
|
2015-03-10 12:28:44 +02:00
|
|
|
_: &ast::Block, span: Span, id: ast::NodeId) {
|
2015-02-25 22:44:44 +11:00
|
|
|
match fk {
|
2015-04-14 09:54:58 +12:00
|
|
|
visit::FkMethod(ident, _, _) => match method_context(cx, id, span) {
|
2015-02-28 13:31:14 +01:00
|
|
|
MethodContext::PlainImpl => {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "method", &token::get_ident(ident), Some(span))
|
2015-02-28 13:31:14 +01:00
|
|
|
},
|
|
|
|
MethodContext::TraitDefaultImpl => {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "trait method", &token::get_ident(ident), Some(span))
|
2015-02-28 13:31:14 +01:00
|
|
|
},
|
2015-02-25 22:44:44 +11:00
|
|
|
_ => (),
|
|
|
|
},
|
2015-02-25 22:05:07 +02:00
|
|
|
visit::FkItemFn(ident, _, _, _, _, _) => {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "function", &token::get_ident(ident), Some(span))
|
2015-02-28 13:31:14 +01:00
|
|
|
},
|
2015-02-25 22:44:44 +11:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
if let ast::ItemMod(_) = it.node {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "module", &token::get_ident(it.ident), Some(it.span));
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
fn check_trait_item(&mut self, cx: &Context, trait_item: &ast::TraitItem) {
|
2015-03-11 23:38:58 +02:00
|
|
|
if let ast::MethodTraitItem(_, None) = trait_item.node {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "trait method", &token::get_ident(trait_item.ident),
|
|
|
|
Some(trait_item.span));
|
2015-03-10 12:28:44 +02:00
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_lifetime_def(&mut self, cx: &Context, t: &ast::LifetimeDef) {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "lifetime", &token::get_ident(t.lifetime.name.ident()),
|
|
|
|
Some(t.lifetime.span));
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
|
|
|
|
if let &ast::PatIdent(_, ref path1, _) = &p.node {
|
|
|
|
let def = cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def());
|
|
|
|
if let Some(def::DefLocal(_)) = def {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "variable", &token::get_ident(path1.node), Some(p.span));
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_struct_def(&mut self, cx: &Context, s: &ast::StructDef,
|
2015-02-28 13:31:14 +01:00
|
|
|
_: ast::Ident, _: &ast::Generics, _: ast::NodeId) {
|
2015-02-25 22:44:44 +11:00
|
|
|
for sf in &s.fields {
|
|
|
|
if let ast::StructField_ { kind: ast::NamedField(ident, _), .. } = sf.node {
|
2015-04-18 13:39:10 +12:00
|
|
|
self.check_snake_case(cx, "structure field", &token::get_ident(ident),
|
|
|
|
Some(sf.span));
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub NON_UPPER_CASE_GLOBALS,
|
|
|
|
Warn,
|
|
|
|
"static constants should have uppercase identifiers"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct NonUpperCaseGlobals;
|
|
|
|
|
|
|
|
impl NonUpperCaseGlobals {
|
|
|
|
fn check_upper_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) {
|
|
|
|
let s = token::get_ident(ident);
|
|
|
|
|
|
|
|
if s.chars().any(|c| c.is_lowercase()) {
|
2015-03-05 18:23:57 -08:00
|
|
|
let uc = NonSnakeCase::to_snake_case(&s).to_uppercase();
|
2015-02-25 22:44:44 +11:00
|
|
|
if uc != &s[..] {
|
|
|
|
cx.span_lint(NON_UPPER_CASE_GLOBALS, span,
|
|
|
|
&format!("{} `{}` should have an upper case name such as `{}`",
|
|
|
|
sort, s, uc));
|
|
|
|
} else {
|
|
|
|
cx.span_lint(NON_UPPER_CASE_GLOBALS, span,
|
|
|
|
&format!("{} `{}` should have an upper case name",
|
|
|
|
sort, s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for NonUpperCaseGlobals {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_UPPER_CASE_GLOBALS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
match it.node {
|
|
|
|
// only check static constants
|
|
|
|
ast::ItemStatic(_, ast::MutImmutable, _) => {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "static constant", it.ident, it.span);
|
|
|
|
}
|
|
|
|
ast::ItemConst(..) => {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "constant", it.ident, it.span);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:35:25 -06:00
|
|
|
fn check_trait_item(&mut self, cx: &Context, ti: &ast::TraitItem) {
|
|
|
|
match ti.node {
|
|
|
|
ast::ConstTraitItem(..) => {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "associated constant",
|
|
|
|
ti.ident, ti.span);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_impl_item(&mut self, cx: &Context, ii: &ast::ImplItem) {
|
|
|
|
match ii.node {
|
|
|
|
ast::ConstImplItem(..) => {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "associated constant",
|
|
|
|
ii.ident, ii.span);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:44:44 +11:00
|
|
|
fn check_pat(&mut self, cx: &Context, p: &ast::Pat) {
|
|
|
|
// Lint for constants that look like binding identifiers (#7526)
|
|
|
|
match (&p.node, cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def())) {
|
|
|
|
(&ast::PatIdent(_, ref path1, _), Some(def::DefConst(..))) => {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "constant in pattern",
|
|
|
|
path1.node, p.span);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNUSED_PARENS,
|
|
|
|
Warn,
|
|
|
|
"`if`, `match`, `while` and `return` do not need parentheses"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnusedParens;
|
|
|
|
|
|
|
|
impl UnusedParens {
|
|
|
|
fn check_unused_parens_core(&self, cx: &Context, value: &ast::Expr, msg: &str,
|
2015-02-28 13:31:14 +01:00
|
|
|
struct_lit_needs_parens: bool) {
|
2015-02-25 22:44:44 +11:00
|
|
|
if let ast::ExprParen(ref inner) = value.node {
|
|
|
|
let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&**inner);
|
|
|
|
if !necessary {
|
|
|
|
cx.span_lint(UNUSED_PARENS, value.span,
|
2015-02-28 13:31:14 +01:00
|
|
|
&format!("unnecessary parentheses around {}", msg))
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Expressions that syntactically contain an "exterior" struct
|
|
|
|
/// literal i.e. not surrounded by any parens or other
|
|
|
|
/// delimiters, e.g. `X { y: 1 }`, `X { y: 1 }.method()`, `foo
|
|
|
|
/// == X { y: 1 }` and `X { y: 1 } == foo` all do, but `(X {
|
|
|
|
/// y: 1 }) == foo` does not.
|
|
|
|
fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
|
|
|
|
match value.node {
|
|
|
|
ast::ExprStruct(..) => true,
|
|
|
|
|
|
|
|
ast::ExprAssign(ref lhs, ref rhs) |
|
|
|
|
ast::ExprAssignOp(_, ref lhs, ref rhs) |
|
|
|
|
ast::ExprBinary(_, ref lhs, ref rhs) => {
|
|
|
|
// X { y: 1 } + X { y: 2 }
|
|
|
|
contains_exterior_struct_lit(&**lhs) ||
|
|
|
|
contains_exterior_struct_lit(&**rhs)
|
|
|
|
}
|
|
|
|
ast::ExprUnary(_, ref x) |
|
|
|
|
ast::ExprCast(ref x, _) |
|
|
|
|
ast::ExprField(ref x, _) |
|
|
|
|
ast::ExprTupField(ref x, _) |
|
|
|
|
ast::ExprIndex(ref x, _) => {
|
|
|
|
// &X { y: 1 }, X { y: 1 }.y
|
|
|
|
contains_exterior_struct_lit(&**x)
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::ExprMethodCall(_, _, ref exprs) => {
|
|
|
|
// X { y: 1 }.bar(...)
|
|
|
|
contains_exterior_struct_lit(&*exprs[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for UnusedParens {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_PARENS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
let (value, msg, struct_lit_needs_parens) = match e.node {
|
|
|
|
ast::ExprIf(ref cond, _, _) => (cond, "`if` condition", true),
|
|
|
|
ast::ExprWhile(ref cond, _, _) => (cond, "`while` condition", true),
|
|
|
|
ast::ExprMatch(ref head, _, source) => match source {
|
|
|
|
ast::MatchSource::Normal => (head, "`match` head expression", true),
|
|
|
|
ast::MatchSource::IfLetDesugar { .. } => (head, "`if let` head expression", true),
|
|
|
|
ast::MatchSource::WhileLetDesugar => (head, "`while let` head expression", true),
|
|
|
|
ast::MatchSource::ForLoopDesugar => (head, "`for` head expression", true),
|
|
|
|
},
|
|
|
|
ast::ExprRet(Some(ref value)) => (value, "`return` value", false),
|
|
|
|
ast::ExprAssign(_, ref value) => (value, "assigned value", false),
|
|
|
|
ast::ExprAssignOp(_, _, ref value) => (value, "assigned value", false),
|
|
|
|
_ => return
|
|
|
|
};
|
|
|
|
self.check_unused_parens_core(cx, &**value, msg, struct_lit_needs_parens);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
|
|
|
|
let (value, msg) = match s.node {
|
|
|
|
ast::StmtDecl(ref decl, _) => match decl.node {
|
|
|
|
ast::DeclLocal(ref local) => match local.init {
|
|
|
|
Some(ref value) => (value, "assigned value"),
|
|
|
|
None => return
|
|
|
|
},
|
|
|
|
_ => return
|
|
|
|
},
|
|
|
|
_ => return
|
|
|
|
};
|
|
|
|
self.check_unused_parens_core(cx, &**value, msg, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNUSED_IMPORT_BRACES,
|
|
|
|
Allow,
|
|
|
|
"unnecessary braces around an imported item"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnusedImportBraces;
|
|
|
|
|
|
|
|
impl LintPass for UnusedImportBraces {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_IMPORT_BRACES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, item: &ast::Item) {
|
2015-02-28 13:32:34 +01:00
|
|
|
if let ast::ItemUse(ref view_path) = item.node {
|
|
|
|
if let ast::ViewPathList(_, ref items) = view_path.node {
|
|
|
|
if items.len() == 1 {
|
|
|
|
if let ast::PathListIdent {ref name, ..} = items[0].node {
|
|
|
|
let m = format!("braces around {} is unnecessary",
|
|
|
|
&token::get_ident(*name));
|
|
|
|
cx.span_lint(UNUSED_IMPORT_BRACES, item.span,
|
|
|
|
&m[..]);
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
2015-02-28 13:32:34 +01:00
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
NON_SHORTHAND_FIELD_PATTERNS,
|
|
|
|
Warn,
|
|
|
|
"using `Struct { x: x }` instead of `Struct { x }`"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct NonShorthandFieldPatterns;
|
|
|
|
|
|
|
|
impl LintPass for NonShorthandFieldPatterns {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_SHORTHAND_FIELD_PATTERNS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_pat(&mut self, cx: &Context, pat: &ast::Pat) {
|
|
|
|
let def_map = cx.tcx.def_map.borrow();
|
|
|
|
if let ast::PatStruct(_, ref v, _) = pat.node {
|
2015-02-28 13:31:14 +01:00
|
|
|
let field_pats = v.iter().filter(|fieldpat| {
|
|
|
|
if fieldpat.node.is_shorthand {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
let def = def_map.get(&fieldpat.node.pat.id).map(|d| d.full_def());
|
|
|
|
def == Some(def::DefLocal(fieldpat.node.pat.id))
|
|
|
|
});
|
|
|
|
for fieldpat in field_pats {
|
|
|
|
if let ast::PatIdent(_, ident, None) = fieldpat.node.pat.node {
|
|
|
|
if ident.node.as_str() == fieldpat.node.ident.as_str() {
|
|
|
|
cx.span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span,
|
|
|
|
&format!("the `{}:` in this pattern is redundant and can \
|
|
|
|
be removed", ident.node.as_str()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_UNSAFE,
|
|
|
|
Warn,
|
|
|
|
"unnecessary use of an `unsafe` block"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnusedUnsafe;
|
|
|
|
|
|
|
|
impl LintPass for UnusedUnsafe {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_UNSAFE)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
if let ast::ExprBlock(ref blk) = e.node {
|
|
|
|
// Don't warn about generated blocks, that'll just pollute the output.
|
|
|
|
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
|
|
|
|
!cx.tcx.used_unsafe.borrow().contains(&blk.id) {
|
|
|
|
cx.span_lint(UNUSED_UNSAFE, blk.span, "unnecessary `unsafe` block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNSAFE_CODE,
|
|
|
|
Allow,
|
|
|
|
"usage of `unsafe` code"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnsafeCode;
|
|
|
|
|
|
|
|
impl LintPass for UnsafeCode {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNSAFE_CODE)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
if let ast::ExprBlock(ref blk) = e.node {
|
|
|
|
// Don't warn about generated blocks, that'll just pollute the output.
|
|
|
|
if blk.rules == ast::UnsafeBlock(ast::UserProvided) {
|
|
|
|
cx.span_lint(UNSAFE_CODE, blk.span, "usage of an `unsafe` block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
match it.node {
|
|
|
|
ast::ItemTrait(ast::Unsafety::Unsafe, _, _, _) =>
|
|
|
|
cx.span_lint(UNSAFE_CODE, it.span, "declaration of an `unsafe` trait"),
|
|
|
|
|
|
|
|
ast::ItemImpl(ast::Unsafety::Unsafe, _, _, _, _, _) =>
|
|
|
|
cx.span_lint(UNSAFE_CODE, it.span, "implementation of an `unsafe` trait"),
|
|
|
|
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_fn(&mut self, cx: &Context, fk: visit::FnKind, _: &ast::FnDecl,
|
|
|
|
_: &ast::Block, span: Span, _: ast::NodeId) {
|
|
|
|
match fk {
|
2015-05-05 08:47:04 -04:00
|
|
|
visit::FkItemFn(_, _, ast::Unsafety::Unsafe, _, _, _) =>
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.span_lint(UNSAFE_CODE, span, "declaration of an `unsafe` function"),
|
|
|
|
|
2015-04-14 09:54:58 +12:00
|
|
|
visit::FkMethod(_, sig, _) => {
|
2015-03-11 23:38:58 +02:00
|
|
|
if sig.unsafety == ast::Unsafety::Unsafe {
|
2015-03-10 12:28:44 +02:00
|
|
|
cx.span_lint(UNSAFE_CODE, span, "implementation of an `unsafe` method")
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
fn check_trait_item(&mut self, cx: &Context, trait_item: &ast::TraitItem) {
|
2015-03-11 23:38:58 +02:00
|
|
|
if let ast::MethodTraitItem(ref sig, None) = trait_item.node {
|
|
|
|
if sig.unsafety == ast::Unsafety::Unsafe {
|
2015-03-10 12:28:44 +02:00
|
|
|
cx.span_lint(UNSAFE_CODE, trait_item.span,
|
|
|
|
"declaration of an `unsafe` method")
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_MUT,
|
|
|
|
Warn,
|
|
|
|
"detect mut variables which don't need to be mutable"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnusedMut;
|
|
|
|
|
|
|
|
impl UnusedMut {
|
|
|
|
fn check_unused_mut_pat(&self, cx: &Context, pats: &[P<ast::Pat>]) {
|
|
|
|
// collect all mutable pattern and group their NodeIDs by their Identifier to
|
|
|
|
// avoid false warnings in match arms with multiple patterns
|
|
|
|
|
|
|
|
let mut mutables = FnvHashMap();
|
|
|
|
for p in pats {
|
|
|
|
pat_util::pat_bindings(&cx.tcx.def_map, &**p, |mode, id, _, path1| {
|
|
|
|
let ident = path1.node;
|
|
|
|
if let ast::BindByValue(ast::MutMutable) = mode {
|
|
|
|
if !token::get_ident(ident).starts_with("_") {
|
|
|
|
match mutables.entry(ident.name.usize()) {
|
|
|
|
Vacant(entry) => { entry.insert(vec![id]); },
|
|
|
|
Occupied(mut entry) => { entry.get_mut().push(id); },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let used_mutables = cx.tcx.used_mut_nodes.borrow();
|
|
|
|
for (_, v) in &mutables {
|
|
|
|
if !v.iter().any(|e| used_mutables.contains(e)) {
|
|
|
|
cx.span_lint(UNUSED_MUT, cx.tcx.map.span(v[0]),
|
|
|
|
"variable does not need to be mutable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for UnusedMut {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_MUT)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
if let ast::ExprMatch(_, ref arms, _) = e.node {
|
|
|
|
for a in arms {
|
|
|
|
self.check_unused_mut_pat(cx, &a.pats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) {
|
|
|
|
if let ast::StmtDecl(ref d, _) = s.node {
|
|
|
|
if let ast::DeclLocal(ref l) = d.node {
|
|
|
|
self.check_unused_mut_pat(cx, slice::ref_slice(&l.pat));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_fn(&mut self, cx: &Context,
|
|
|
|
_: visit::FnKind, decl: &ast::FnDecl,
|
|
|
|
_: &ast::Block, _: Span, _: ast::NodeId) {
|
|
|
|
for a in &decl.inputs {
|
|
|
|
self.check_unused_mut_pat(cx, slice::ref_slice(&a.pat));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNUSED_ALLOCATION,
|
|
|
|
Warn,
|
|
|
|
"detects unnecessary allocations that can be eliminated"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnusedAllocation;
|
|
|
|
|
|
|
|
impl LintPass for UnusedAllocation {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_ALLOCATION)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
match e.node {
|
|
|
|
ast::ExprUnary(ast::UnUniq, _) => (),
|
|
|
|
_ => return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(adjustment) = cx.tcx.adjustments.borrow().get(&e.id) {
|
|
|
|
if let ty::AdjustDerefRef(ty::AutoDerefRef { ref autoref, .. }) = *adjustment {
|
|
|
|
match autoref {
|
2015-03-16 18:45:01 +02:00
|
|
|
&Some(ty::AutoPtr(_, ast::MutImmutable)) => {
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.span_lint(UNUSED_ALLOCATION, e.span,
|
|
|
|
"unnecessary allocation, use & instead");
|
|
|
|
}
|
2015-03-16 18:45:01 +02:00
|
|
|
&Some(ty::AutoPtr(_, ast::MutMutable)) => {
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.span_lint(UNUSED_ALLOCATION, e.span,
|
|
|
|
"unnecessary allocation, use &mut instead");
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
MISSING_DOCS,
|
|
|
|
Allow,
|
|
|
|
"detects missing documentation for public members"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MissingDoc {
|
|
|
|
/// Stack of IDs of struct definitions.
|
|
|
|
struct_def_stack: Vec<ast::NodeId>,
|
|
|
|
|
|
|
|
/// True if inside variant definition
|
|
|
|
in_variant: bool,
|
|
|
|
|
|
|
|
/// Stack of whether #[doc(hidden)] is set
|
|
|
|
/// at each level which has lint attributes.
|
|
|
|
doc_hidden_stack: Vec<bool>,
|
2015-03-29 23:41:54 -04:00
|
|
|
|
|
|
|
/// Private traits or trait items that leaked through. Don't check their methods.
|
|
|
|
private_traits: HashSet<ast::NodeId>,
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MissingDoc {
|
|
|
|
pub fn new() -> MissingDoc {
|
|
|
|
MissingDoc {
|
|
|
|
struct_def_stack: vec!(),
|
|
|
|
in_variant: false,
|
|
|
|
doc_hidden_stack: vec!(false),
|
2015-03-29 23:41:54 -04:00
|
|
|
private_traits: HashSet::new(),
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn doc_hidden(&self) -> bool {
|
|
|
|
*self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_missing_docs_attrs(&self,
|
|
|
|
cx: &Context,
|
|
|
|
id: Option<ast::NodeId>,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sp: Span,
|
|
|
|
desc: &'static str) {
|
|
|
|
// If we're building a test harness, then warning about
|
|
|
|
// documentation is probably not really relevant right now.
|
2015-02-28 13:31:14 +01:00
|
|
|
if cx.sess().opts.test {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
|
|
|
|
// `#[doc(hidden)]` disables missing_docs check.
|
2015-02-28 13:31:14 +01:00
|
|
|
if self.doc_hidden() {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
|
|
|
|
// Only check publicly-visible items, using the result from the privacy pass.
|
|
|
|
// It's an option so the crate root can also use this function (it doesn't
|
|
|
|
// have a NodeId).
|
|
|
|
if let Some(ref id) = id {
|
|
|
|
if !cx.exported_items.contains(id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let has_doc = attrs.iter().any(|a| {
|
|
|
|
match a.node.value.node {
|
|
|
|
ast::MetaNameValue(ref name, _) if *name == "doc" => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if !has_doc {
|
|
|
|
cx.span_lint(MISSING_DOCS, sp,
|
2015-02-28 13:31:14 +01:00
|
|
|
&format!("missing documentation for {}", desc));
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for MissingDoc {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MISSING_DOCS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enter_lint_attrs(&mut self, _: &Context, attrs: &[ast::Attribute]) {
|
|
|
|
let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
|
|
|
|
attr.check_name("doc") && match attr.meta_item_list() {
|
|
|
|
None => false,
|
|
|
|
Some(l) => attr::contains_name(&l[..], "hidden"),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.doc_hidden_stack.push(doc_hidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_lint_attrs(&mut self, _: &Context, _: &[ast::Attribute]) {
|
|
|
|
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:31:14 +01:00
|
|
|
fn check_struct_def(&mut self, _: &Context, _: &ast::StructDef,
|
|
|
|
_: ast::Ident, _: &ast::Generics, id: ast::NodeId) {
|
2015-02-25 22:44:44 +11:00
|
|
|
self.struct_def_stack.push(id);
|
|
|
|
}
|
|
|
|
|
2015-02-28 13:31:14 +01:00
|
|
|
fn check_struct_def_post(&mut self, _: &Context, _: &ast::StructDef,
|
|
|
|
_: ast::Ident, _: &ast::Generics, id: ast::NodeId) {
|
2015-02-25 22:44:44 +11:00
|
|
|
let popped = self.struct_def_stack.pop().expect("empty struct_def_stack");
|
|
|
|
assert!(popped == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_crate(&mut self, cx: &Context, krate: &ast::Crate) {
|
2015-02-28 13:31:14 +01:00
|
|
|
self.check_missing_docs_attrs(cx, None, &krate.attrs, krate.span, "crate");
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
let desc = match it.node {
|
|
|
|
ast::ItemFn(..) => "a function",
|
|
|
|
ast::ItemMod(..) => "a module",
|
|
|
|
ast::ItemEnum(..) => "an enum",
|
|
|
|
ast::ItemStruct(..) => "a struct",
|
2015-03-29 23:41:54 -04:00
|
|
|
ast::ItemTrait(_, _, _, ref items) => {
|
|
|
|
// Issue #11592, traits are always considered exported, even when private.
|
|
|
|
if it.vis == ast::Visibility::Inherited {
|
|
|
|
self.private_traits.insert(it.id);
|
|
|
|
for itm in items {
|
|
|
|
self.private_traits.insert(itm.id);
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
"a trait"
|
|
|
|
},
|
2015-02-25 22:44:44 +11:00
|
|
|
ast::ItemTy(..) => "a type alias",
|
2015-03-29 23:41:54 -04:00
|
|
|
ast::ItemImpl(_, _, _, Some(ref trait_ref), _, ref impl_items) => {
|
|
|
|
// If the trait is private, add the impl items to private_traits so they don't get
|
|
|
|
// reported for missing docs.
|
|
|
|
let real_trait = ty::trait_ref_to_def_id(cx.tcx, trait_ref);
|
|
|
|
match cx.tcx.map.find(real_trait.node) {
|
|
|
|
Some(ast_map::NodeItem(item)) => if item.vis == ast::Visibility::Inherited {
|
|
|
|
for itm in impl_items {
|
|
|
|
self.private_traits.insert(itm.id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => { }
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
2015-02-25 22:44:44 +11:00
|
|
|
_ => return
|
|
|
|
};
|
2015-03-29 23:41:54 -04:00
|
|
|
|
2015-02-28 13:31:14 +01:00
|
|
|
self.check_missing_docs_attrs(cx, Some(it.id), &it.attrs, it.span, desc);
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
fn check_trait_item(&mut self, cx: &Context, trait_item: &ast::TraitItem) {
|
2015-03-29 23:41:54 -04:00
|
|
|
if self.private_traits.contains(&trait_item.id) { return }
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
let desc = match trait_item.node {
|
2015-03-14 12:05:00 -06:00
|
|
|
ast::ConstTraitItem(..) => "an associated constant",
|
2015-03-11 23:38:58 +02:00
|
|
|
ast::MethodTraitItem(..) => "a trait method",
|
2015-03-14 12:05:00 -06:00
|
|
|
ast::TypeTraitItem(..) => "an associated type",
|
2015-03-10 12:28:44 +02:00
|
|
|
};
|
2015-03-29 23:41:54 -04:00
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
self.check_missing_docs_attrs(cx, Some(trait_item.id),
|
|
|
|
&trait_item.attrs,
|
|
|
|
trait_item.span, desc);
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
fn check_impl_item(&mut self, cx: &Context, impl_item: &ast::ImplItem) {
|
|
|
|
// If the method is an impl for a trait, don't doc.
|
|
|
|
if method_context(cx, impl_item.id, impl_item.span) == MethodContext::TraitImpl {
|
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
|
|
|
|
let desc = match impl_item.node {
|
2015-03-14 12:05:00 -06:00
|
|
|
ast::ConstImplItem(..) => "an associated constant",
|
2015-03-11 23:38:58 +02:00
|
|
|
ast::MethodImplItem(..) => "a method",
|
|
|
|
ast::TypeImplItem(_) => "an associated type",
|
2015-03-14 12:05:00 -06:00
|
|
|
ast::MacImplItem(_) => "an impl item macro",
|
2015-03-10 12:28:44 +02:00
|
|
|
};
|
|
|
|
self.check_missing_docs_attrs(cx, Some(impl_item.id),
|
|
|
|
&impl_item.attrs,
|
|
|
|
impl_item.span, desc);
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_struct_field(&mut self, cx: &Context, sf: &ast::StructField) {
|
|
|
|
if let ast::NamedField(_, vis) = sf.node.kind {
|
|
|
|
if vis == ast::Public || self.in_variant {
|
|
|
|
let cur_struct_def = *self.struct_def_stack.last()
|
|
|
|
.expect("empty struct_def_stack");
|
|
|
|
self.check_missing_docs_attrs(cx, Some(cur_struct_def),
|
|
|
|
&sf.node.attrs, sf.span,
|
|
|
|
"a struct field")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_variant(&mut self, cx: &Context, v: &ast::Variant, _: &ast::Generics) {
|
2015-02-28 13:31:14 +01:00
|
|
|
self.check_missing_docs_attrs(cx, Some(v.node.id), &v.node.attrs, v.span, "a variant");
|
2015-02-25 22:44:44 +11:00
|
|
|
assert!(!self.in_variant);
|
|
|
|
self.in_variant = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_variant_post(&mut self, _: &Context, _: &ast::Variant, _: &ast::Generics) {
|
|
|
|
assert!(self.in_variant);
|
|
|
|
self.in_variant = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub MISSING_COPY_IMPLEMENTATIONS,
|
|
|
|
Allow,
|
|
|
|
"detects potentially-forgotten implementations of `Copy`"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct MissingCopyImplementations;
|
|
|
|
|
|
|
|
impl LintPass for MissingCopyImplementations {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MISSING_COPY_IMPLEMENTATIONS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, item: &ast::Item) {
|
|
|
|
if !cx.exported_items.contains(&item.id) {
|
2015-02-28 13:31:14 +01:00
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
if cx.tcx.destructor_for_type.borrow().contains_key(&local_def(item.id)) {
|
2015-02-28 13:31:14 +01:00
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
let ty = match item.node {
|
|
|
|
ast::ItemStruct(_, ref ast_generics) => {
|
|
|
|
if ast_generics.is_parameterized() {
|
2015-02-28 13:31:14 +01:00
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
ty::mk_struct(cx.tcx, local_def(item.id),
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.tcx.mk_substs(Substs::empty()))
|
|
|
|
}
|
|
|
|
ast::ItemEnum(_, ref ast_generics) => {
|
|
|
|
if ast_generics.is_parameterized() {
|
2015-02-28 13:31:14 +01:00
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
ty::mk_enum(cx.tcx, local_def(item.id),
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.tcx.mk_substs(Substs::empty()))
|
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
let parameter_environment = ty::empty_parameter_environment(cx.tcx);
|
|
|
|
if !ty::type_moves_by_default(¶meter_environment, item.span, ty) {
|
2015-02-28 13:31:14 +01:00
|
|
|
return;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
if ty::can_type_implement_copy(¶meter_environment, item.span, ty).is_ok() {
|
|
|
|
cx.span_lint(MISSING_COPY_IMPLEMENTATIONS,
|
|
|
|
item.span,
|
|
|
|
"type could implement `Copy`; consider adding `impl \
|
|
|
|
Copy`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
MISSING_DEBUG_IMPLEMENTATIONS,
|
|
|
|
Allow,
|
|
|
|
"detects missing implementations of fmt::Debug"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MissingDebugImplementations {
|
|
|
|
impling_types: Option<NodeSet>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MissingDebugImplementations {
|
|
|
|
pub fn new() -> MissingDebugImplementations {
|
|
|
|
MissingDebugImplementations {
|
|
|
|
impling_types: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for MissingDebugImplementations {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MISSING_DEBUG_IMPLEMENTATIONS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, item: &ast::Item) {
|
|
|
|
if !cx.exported_items.contains(&item.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match item.node {
|
|
|
|
ast::ItemStruct(..) | ast::ItemEnum(..) => {},
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
|
|
|
|
let debug = match cx.tcx.lang_items.debug_trait() {
|
|
|
|
Some(debug) => debug,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.impling_types.is_none() {
|
2015-04-21 19:00:12 +03:00
|
|
|
let debug_def = ty::lookup_trait_def(cx.tcx, debug);
|
|
|
|
let mut impls = NodeSet();
|
|
|
|
debug_def.for_each_impl(cx.tcx, |d| {
|
|
|
|
if d.krate == ast::LOCAL_CRATE {
|
|
|
|
if let Some(ty_def) = ty::ty_to_def_id(ty::node_id_to_type(cx.tcx, d.node)) {
|
|
|
|
impls.insert(ty_def.node);
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
2015-04-21 19:00:12 +03:00
|
|
|
});
|
|
|
|
|
2015-02-25 22:44:44 +11:00
|
|
|
self.impling_types = Some(impls);
|
|
|
|
debug!("{:?}", self.impling_types);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.impling_types.as_ref().unwrap().contains(&item.id) {
|
|
|
|
cx.span_lint(MISSING_DEBUG_IMPLEMENTATIONS,
|
|
|
|
item.span,
|
|
|
|
"type does not implement `fmt::Debug`; consider adding #[derive(Debug)] \
|
|
|
|
or a manual implementation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
DEPRECATED,
|
|
|
|
Warn,
|
|
|
|
"detects use of #[deprecated] items"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks for use of items with `#[deprecated]` attributes
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct Stability;
|
|
|
|
|
|
|
|
impl Stability {
|
2015-05-26 02:28:04 +03:00
|
|
|
fn lint(&self, cx: &Context, _id: ast::DefId,
|
|
|
|
span: Span, stability: &Option<&attr::Stability>) {
|
2015-02-28 13:31:14 +01:00
|
|
|
// Deprecated attributes apply in-crate and cross-crate.
|
2015-02-25 22:44:44 +11:00
|
|
|
let (lint, label) = match *stability {
|
2015-05-26 00:41:27 +03:00
|
|
|
Some(&attr::Stability { deprecated_since: Some(_), .. }) =>
|
2015-02-25 22:44:44 +11:00
|
|
|
(DEPRECATED, "deprecated"),
|
|
|
|
_ => return
|
|
|
|
};
|
|
|
|
|
|
|
|
output(cx, span, stability, lint, label);
|
|
|
|
|
2015-05-26 00:41:27 +03:00
|
|
|
fn output(cx: &Context, span: Span, stability: &Option<&attr::Stability>,
|
2015-02-25 22:44:44 +11:00
|
|
|
lint: &'static Lint, label: &'static str) {
|
|
|
|
let msg = match *stability {
|
2015-05-26 00:41:27 +03:00
|
|
|
Some(&attr::Stability { reason: Some(ref s), .. }) => {
|
2015-02-25 22:44:44 +11:00
|
|
|
format!("use of {} item: {}", label, *s)
|
|
|
|
}
|
|
|
|
_ => format!("use of {} item", label)
|
|
|
|
};
|
|
|
|
|
|
|
|
cx.span_lint(lint, span, &msg[..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for Stability {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(DEPRECATED)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, item: &ast::Item) {
|
|
|
|
stability::check_item(cx.tcx, item, false,
|
|
|
|
&mut |id, sp, stab| self.lint(cx, id, sp, stab));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, e: &ast::Expr) {
|
|
|
|
stability::check_expr(cx.tcx, e,
|
|
|
|
&mut |id, sp, stab| self.lint(cx, id, sp, stab));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_path(&mut self, cx: &Context, path: &ast::Path, id: ast::NodeId) {
|
|
|
|
stability::check_path(cx.tcx, path, id,
|
|
|
|
&mut |id, sp, stab| self.lint(cx, id, sp, stab));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_pat(&mut self, cx: &Context, pat: &ast::Pat) {
|
|
|
|
stability::check_pat(cx.tcx, pat,
|
|
|
|
&mut |id, sp, stab| self.lint(cx, id, sp, stab))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNCONDITIONAL_RECURSION,
|
|
|
|
Warn,
|
|
|
|
"functions that cannot return without calling themselves"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnconditionalRecursion;
|
|
|
|
|
|
|
|
|
|
|
|
impl LintPass for UnconditionalRecursion {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![UNCONDITIONAL_RECURSION]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_fn(&mut self, cx: &Context, fn_kind: visit::FnKind, _: &ast::FnDecl,
|
|
|
|
blk: &ast::Block, sp: Span, id: ast::NodeId) {
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-20 17:15:27 +13:00
|
|
|
// FIXME(#23542) Replace with type ascription.
|
2015-03-24 11:23:34 +13:00
|
|
|
#![allow(trivial_casts)]
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-20 17:15:27 +13:00
|
|
|
|
2015-02-25 22:44:44 +11:00
|
|
|
type F = for<'tcx> fn(&ty::ctxt<'tcx>,
|
|
|
|
ast::NodeId, ast::NodeId, ast::Ident, ast::NodeId) -> bool;
|
|
|
|
|
|
|
|
let (name, checker) = match fn_kind {
|
2015-02-25 22:05:07 +02:00
|
|
|
visit::FkItemFn(name, _, _, _, _, _) => (name, id_refers_to_this_fn as F),
|
2015-04-14 09:54:58 +12:00
|
|
|
visit::FkMethod(name, _, _) => (name, id_refers_to_this_method as F),
|
2015-02-25 22:44:44 +11:00
|
|
|
// closures can't recur, so they don't matter.
|
|
|
|
visit::FkFnBlock => return
|
|
|
|
};
|
|
|
|
|
2015-03-10 12:28:44 +02:00
|
|
|
let impl_def_id = ty::impl_of_method(cx.tcx, local_def(id))
|
|
|
|
.unwrap_or(local_def(ast::DUMMY_NODE_ID));
|
2015-02-25 22:44:44 +11:00
|
|
|
assert!(ast_util::is_local(impl_def_id));
|
|
|
|
let impl_node_id = impl_def_id.node;
|
|
|
|
|
|
|
|
// Walk through this function (say `f`) looking to see if
|
|
|
|
// every possible path references itself, i.e. the function is
|
|
|
|
// called recursively unconditionally. This is done by trying
|
|
|
|
// to find a path from the entry node to the exit node that
|
|
|
|
// *doesn't* call `f` by traversing from the entry while
|
|
|
|
// pretending that calls of `f` are sinks (i.e. ignoring any
|
|
|
|
// exit edges from them).
|
|
|
|
//
|
|
|
|
// NB. this has an edge case with non-returning statements,
|
|
|
|
// like `loop {}` or `panic!()`: control flow never reaches
|
|
|
|
// the exit node through these, so one can have a function
|
|
|
|
// that never actually calls itselfs but is still picked up by
|
|
|
|
// this lint:
|
|
|
|
//
|
|
|
|
// fn f(cond: bool) {
|
|
|
|
// if !cond { panic!() } // could come from `assert!(cond)`
|
|
|
|
// f(false)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// In general, functions of that form may be able to call
|
|
|
|
// itself a finite number of times and then diverge. The lint
|
|
|
|
// considers this to be an error for two reasons, (a) it is
|
|
|
|
// easier to implement, and (b) it seems rare to actually want
|
|
|
|
// to have behaviour like the above, rather than
|
|
|
|
// e.g. accidentally recurring after an assert.
|
|
|
|
|
|
|
|
let cfg = cfg::CFG::new(cx.tcx, blk);
|
|
|
|
|
|
|
|
let mut work_queue = vec![cfg.entry];
|
|
|
|
let mut reached_exit_without_self_call = false;
|
|
|
|
let mut self_call_spans = vec![];
|
|
|
|
let mut visited = BitSet::new();
|
|
|
|
|
|
|
|
while let Some(idx) = work_queue.pop() {
|
|
|
|
if idx == cfg.exit {
|
|
|
|
// found a path!
|
|
|
|
reached_exit_without_self_call = true;
|
2015-02-28 13:31:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let cfg_id = idx.node_id();
|
|
|
|
if visited.contains(&cfg_id) {
|
2015-02-25 22:44:44 +11:00
|
|
|
// already done
|
2015-02-28 13:31:14 +01:00
|
|
|
continue;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
visited.insert(cfg_id);
|
2015-02-28 13:31:14 +01:00
|
|
|
|
2015-02-25 22:44:44 +11:00
|
|
|
let node_id = cfg.graph.node_data(idx).id();
|
|
|
|
|
|
|
|
// is this a recursive call?
|
|
|
|
if node_id != ast::DUMMY_NODE_ID && checker(cx.tcx, impl_node_id, id, name, node_id) {
|
|
|
|
self_call_spans.push(cx.tcx.map.span(node_id));
|
|
|
|
// this is a self call, so we shouldn't explore past
|
|
|
|
// this node in the CFG.
|
2015-02-28 13:31:14 +01:00
|
|
|
continue;
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
// add the successors of this node to explore the graph further.
|
2015-04-07 06:12:13 -04:00
|
|
|
for (_, edge) in cfg.graph.outgoing_edges(idx) {
|
2015-02-25 22:44:44 +11:00
|
|
|
let target_idx = edge.target();
|
|
|
|
let target_cfg_id = target_idx.node_id();
|
|
|
|
if !visited.contains(&target_cfg_id) {
|
|
|
|
work_queue.push(target_idx)
|
|
|
|
}
|
2015-04-07 06:12:13 -04:00
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
2015-02-28 13:31:14 +01:00
|
|
|
// Check the number of self calls because a function that
|
2015-02-25 22:44:44 +11:00
|
|
|
// doesn't return (e.g. calls a `-> !` function or `loop { /*
|
|
|
|
// no break */ }`) shouldn't be linted unless it actually
|
|
|
|
// recurs.
|
2015-03-24 16:54:09 -07:00
|
|
|
if !reached_exit_without_self_call && !self_call_spans.is_empty() {
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.span_lint(UNCONDITIONAL_RECURSION, sp,
|
|
|
|
"function cannot return without recurring");
|
|
|
|
|
|
|
|
// FIXME #19668: these could be span_lint_note's instead of this manual guard.
|
|
|
|
if cx.current_level(UNCONDITIONAL_RECURSION) != Level::Allow {
|
|
|
|
let sess = cx.sess();
|
|
|
|
// offer some help to the programmer.
|
|
|
|
for call in &self_call_spans {
|
|
|
|
sess.span_note(*call, "recursive call site")
|
|
|
|
}
|
2015-02-24 16:07:54 +02:00
|
|
|
sess.fileline_help(sp, "a `loop` may express intention \
|
|
|
|
better if this is on purpose")
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// all done
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Functions for identifying if the given NodeId `id`
|
|
|
|
// represents a call to the function `fn_id`/method
|
|
|
|
// `method_id`.
|
|
|
|
|
|
|
|
fn id_refers_to_this_fn<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
_: ast::NodeId,
|
|
|
|
fn_id: ast::NodeId,
|
|
|
|
_: ast::Ident,
|
|
|
|
id: ast::NodeId) -> bool {
|
|
|
|
tcx.def_map.borrow().get(&id)
|
2015-03-10 12:28:44 +02:00
|
|
|
.map_or(false, |def| def.def_id() == local_def(fn_id))
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if the method call `id` refers to method `method_id`
|
|
|
|
// (with name `method_name` contained in impl `impl_id`).
|
|
|
|
fn id_refers_to_this_method<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
impl_id: ast::NodeId,
|
|
|
|
method_id: ast::NodeId,
|
|
|
|
method_name: ast::Ident,
|
|
|
|
id: ast::NodeId) -> bool {
|
|
|
|
let did = match tcx.method_map.borrow().get(&ty::MethodCall::expr(id)) {
|
|
|
|
None => return false,
|
|
|
|
Some(m) => match m.origin {
|
|
|
|
// There's no way to know if a method call via a
|
|
|
|
// vtable is recursion, so we assume it's not.
|
|
|
|
ty::MethodTraitObject(_) => return false,
|
|
|
|
|
|
|
|
// This `did` refers directly to the method definition.
|
|
|
|
ty::MethodStatic(did) | ty::MethodStaticClosure(did) => did,
|
|
|
|
|
|
|
|
// MethodTypeParam are methods from traits:
|
|
|
|
|
|
|
|
// The `impl ... for ...` of this method call
|
|
|
|
// isn't known, e.g. it might be a default method
|
|
|
|
// in a trait, so we get the def-id of the trait
|
|
|
|
// method instead.
|
|
|
|
ty::MethodTypeParam(
|
|
|
|
ty::MethodParam { ref trait_ref, method_num, impl_def_id: None, }) => {
|
|
|
|
ty::trait_item(tcx, trait_ref.def_id, method_num).def_id()
|
|
|
|
}
|
|
|
|
|
|
|
|
// The `impl` is known, so we check that with a
|
|
|
|
// special case:
|
|
|
|
ty::MethodTypeParam(
|
|
|
|
ty::MethodParam { impl_def_id: Some(impl_def_id), .. }) => {
|
|
|
|
|
|
|
|
let name = match tcx.map.expect_expr(id).node {
|
|
|
|
ast::ExprMethodCall(ref sp_ident, _, _) => sp_ident.node,
|
|
|
|
_ => tcx.sess.span_bug(
|
|
|
|
tcx.map.span(id),
|
|
|
|
"non-method call expr behaving like a method call?")
|
|
|
|
};
|
2015-02-28 13:31:14 +01:00
|
|
|
// It matches if it comes from the same impl,
|
2015-02-25 22:44:44 +11:00
|
|
|
// and has the same method name.
|
|
|
|
return ast_util::is_local(impl_def_id)
|
|
|
|
&& impl_def_id.node == impl_id
|
|
|
|
&& method_name.name == name.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ast_util::is_local(did) && did.node == method_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
PLUGIN_AS_LIBRARY,
|
|
|
|
Warn,
|
|
|
|
"compiler plugin used as ordinary library in non-plugin crate"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct PluginAsLibrary;
|
|
|
|
|
|
|
|
impl LintPass for PluginAsLibrary {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![PLUGIN_AS_LIBRARY]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
if cx.sess().plugin_registrar_fn.get().is_some() {
|
|
|
|
// We're compiling a plugin; it's fine to link other plugins.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match it.node {
|
|
|
|
ast::ItemExternCrate(..) => (),
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
let md = match cx.sess().cstore.find_extern_mod_stmt_cnum(it.id) {
|
|
|
|
Some(cnum) => cx.sess().cstore.get_crate_data(cnum),
|
|
|
|
None => {
|
|
|
|
// Probably means we aren't linking the crate for some reason.
|
|
|
|
//
|
|
|
|
// Not sure if / when this could happen.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if decoder::get_plugin_registrar_fn(md.data()).is_some() {
|
|
|
|
cx.span_lint(PLUGIN_AS_LIBRARY, it.span,
|
2015-02-28 13:31:14 +01:00
|
|
|
"compiler plugin used as an ordinary library");
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
PRIVATE_NO_MANGLE_FNS,
|
|
|
|
Warn,
|
|
|
|
"functions marked #[no_mangle] should be exported"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
PRIVATE_NO_MANGLE_STATICS,
|
|
|
|
Warn,
|
|
|
|
"statics marked #[no_mangle] should be exported"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
NO_MANGLE_CONST_ITEMS,
|
|
|
|
Deny,
|
|
|
|
"const items will not have their symbols exported"
|
|
|
|
}
|
|
|
|
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct InvalidNoMangleItems;
|
|
|
|
|
|
|
|
impl LintPass for InvalidNoMangleItems {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(PRIVATE_NO_MANGLE_FNS,
|
|
|
|
PRIVATE_NO_MANGLE_STATICS,
|
|
|
|
NO_MANGLE_CONST_ITEMS)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
|
|
|
|
match it.node {
|
|
|
|
ast::ItemFn(..) => {
|
|
|
|
if attr::contains_name(&it.attrs, "no_mangle") &&
|
|
|
|
!cx.exported_items.contains(&it.id) {
|
|
|
|
let msg = format!("function {} is marked #[no_mangle], but not exported",
|
|
|
|
it.ident);
|
|
|
|
cx.span_lint(PRIVATE_NO_MANGLE_FNS, it.span, &msg);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::ItemStatic(..) => {
|
2015-03-18 09:14:54 -07:00
|
|
|
if attr::contains_name(&it.attrs, "no_mangle") &&
|
2015-02-25 22:44:44 +11:00
|
|
|
!cx.exported_items.contains(&it.id) {
|
|
|
|
let msg = format!("static {} is marked #[no_mangle], but not exported",
|
|
|
|
it.ident);
|
2015-03-07 18:08:48 -08:00
|
|
|
cx.span_lint(PRIVATE_NO_MANGLE_STATICS, it.span, &msg);
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
ast::ItemConst(..) => {
|
2015-03-18 09:14:54 -07:00
|
|
|
if attr::contains_name(&it.attrs, "no_mangle") {
|
2015-02-25 22:44:44 +11:00
|
|
|
// Const items do not refer to a particular location in memory, and therefore
|
|
|
|
// don't have anything to attach a symbol to
|
|
|
|
let msg = "const items should never be #[no_mangle], consider instead using \
|
2015-02-28 13:31:14 +01:00
|
|
|
`pub static`";
|
2015-02-25 22:44:44 +11:00
|
|
|
cx.span_lint(NO_MANGLE_CONST_ITEMS, it.span, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:49:10 -07:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct MutableTransmutes;
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
MUTABLE_TRANSMUTES,
|
|
|
|
Deny,
|
|
|
|
"mutating transmuted &mut T from &T may cause undefined behavior"
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for MutableTransmutes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MUTABLE_TRANSMUTES)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &Context, expr: &ast::Expr) {
|
|
|
|
use syntax::ast::DefId;
|
|
|
|
use syntax::abi::RustIntrinsic;
|
|
|
|
let msg = "mutating transmuted &mut T from &T may cause undefined behavior,\
|
|
|
|
consider instead using an UnsafeCell";
|
|
|
|
match get_transmute_from_to(cx, expr) {
|
|
|
|
Some((&ty::ty_rptr(_, from_mt), &ty::ty_rptr(_, to_mt))) => {
|
|
|
|
if to_mt.mutbl == ast::Mutability::MutMutable
|
|
|
|
&& from_mt.mutbl == ast::Mutability::MutImmutable {
|
|
|
|
cx.span_lint(MUTABLE_TRANSMUTES, expr.span, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_transmute_from_to<'a, 'tcx>(cx: &Context<'a, 'tcx>, expr: &ast::Expr)
|
|
|
|
-> Option<(&'tcx ty::sty<'tcx>, &'tcx ty::sty<'tcx>)> {
|
|
|
|
match expr.node {
|
|
|
|
ast::ExprPath(..) => (),
|
|
|
|
_ => return None
|
|
|
|
}
|
|
|
|
if let DefFn(did, _) = ty::resolve_expr(cx.tcx, expr) {
|
|
|
|
if !def_id_is_transmute(cx, did) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let typ = ty::node_id_to_type(cx.tcx, expr.id);
|
|
|
|
match typ.sty {
|
|
|
|
ty::ty_bare_fn(_, ref bare_fn) if bare_fn.abi == RustIntrinsic => {
|
|
|
|
if let ty::FnConverging(to) = bare_fn.sig.0.output {
|
|
|
|
let from = bare_fn.sig.0.inputs[0];
|
|
|
|
return Some((&from.sty, &to.sty));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn def_id_is_transmute(cx: &Context, def_id: DefId) -> bool {
|
|
|
|
match ty::lookup_item_type(cx.tcx, def_id).ty.sty {
|
|
|
|
ty::ty_bare_fn(_, ref bfty) if bfty.abi == RustIntrinsic => (),
|
|
|
|
_ => return false
|
|
|
|
}
|
|
|
|
ty::with_path(cx.tcx, def_id, |path| match path.last() {
|
|
|
|
Some(ref last) => last.name().as_str() == "transmute",
|
|
|
|
_ => false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 22:44:44 +11:00
|
|
|
/// Forbids using the `#[feature(...)]` attribute
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2015-02-25 22:44:44 +11:00
|
|
|
pub struct UnstableFeatures;
|
|
|
|
|
2015-02-28 13:31:14 +01:00
|
|
|
declare_lint! {
|
|
|
|
UNSTABLE_FEATURES,
|
|
|
|
Allow,
|
|
|
|
"enabling unstable features"
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
|
|
|
|
impl LintPass for UnstableFeatures {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNSTABLE_FEATURES)
|
|
|
|
}
|
|
|
|
fn check_attribute(&mut self, ctx: &Context, attr: &ast::Attribute) {
|
|
|
|
if attr::contains_name(&[attr.node.value.clone()], "feature") {
|
2015-05-18 16:37:05 +02:00
|
|
|
if let Some(items) = attr.node.value.meta_item_list() {
|
|
|
|
for item in items {
|
|
|
|
ctx.span_lint(UNSTABLE_FEATURES, item.span, "unstable feature");
|
|
|
|
}
|
|
|
|
}
|
2015-02-25 22:44:44 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 11:37:19 +02:00
|
|
|
|
|
|
|
/// Lints for attempts to impl Drop on types that have `#[repr(C)]`
|
|
|
|
/// attribute (see issue #24585).
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct DropWithReprExtern;
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
DROP_WITH_REPR_EXTERN,
|
|
|
|
Warn,
|
|
|
|
"use of #[repr(C)] on a type that implements Drop"
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for DropWithReprExtern {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(DROP_WITH_REPR_EXTERN)
|
|
|
|
}
|
|
|
|
fn check_crate(&mut self, ctx: &Context, _: &ast::Crate) {
|
|
|
|
for dtor_did in ctx.tcx.destructors.borrow().iter() {
|
|
|
|
let (drop_impl_did, dtor_self_type) =
|
|
|
|
if dtor_did.krate == ast::LOCAL_CRATE {
|
|
|
|
let impl_did = ctx.tcx.map.get_parent_did(dtor_did.node);
|
|
|
|
let ty = ty::lookup_item_type(ctx.tcx, impl_did).ty;
|
|
|
|
(impl_did, ty)
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
match dtor_self_type.sty {
|
|
|
|
ty::ty_enum(self_type_did, _) |
|
|
|
|
ty::ty_struct(self_type_did, _) |
|
|
|
|
ty::ty_closure(self_type_did, _) => {
|
|
|
|
let hints = ty::lookup_repr_hints(ctx.tcx, self_type_did);
|
|
|
|
if hints.iter().any(|attr| *attr == attr::ReprExtern) &&
|
|
|
|
ty::ty_dtor(ctx.tcx, self_type_did).has_drop_flag() {
|
|
|
|
let drop_impl_span = ctx.tcx.map.def_id_span(drop_impl_did,
|
|
|
|
codemap::DUMMY_SP);
|
|
|
|
let self_defn_span = ctx.tcx.map.def_id_span(self_type_did,
|
|
|
|
codemap::DUMMY_SP);
|
|
|
|
ctx.span_lint(DROP_WITH_REPR_EXTERN,
|
|
|
|
drop_impl_span,
|
|
|
|
"implementing Drop adds hidden state to types, \
|
|
|
|
possibly conflicting with `#[repr(C)]`");
|
|
|
|
// FIXME #19668: could be span_lint_note instead of manual guard.
|
|
|
|
if ctx.current_level(DROP_WITH_REPR_EXTERN) != Level::Allow {
|
|
|
|
ctx.sess().span_note(self_defn_span,
|
|
|
|
"the `#[repr(C)]` attribute is attached here");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|