2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::intravisit;
|
2016-06-07 14:55:55 +00:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::ty;
|
|
|
|
use rustc::lint::*;
|
|
|
|
use std::collections::HashSet;
|
2016-03-08 23:48:10 +00:00
|
|
|
use syntax::ast;
|
2016-08-08 15:21:47 +00:00
|
|
|
use syntax::abi::Abi;
|
2016-03-08 23:48:10 +00:00
|
|
|
use syntax::codemap::Span;
|
2016-06-07 14:55:55 +00:00
|
|
|
use utils::{span_lint, type_is_unsafe_function};
|
2016-03-08 23:48:10 +00:00
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for functions with too many parameters.
|
2016-03-08 23:48:10 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** Functions with lots of parameters are considered bad
|
|
|
|
/// style and reduce readability (“what does the 5th parameter mean?”). Consider
|
|
|
|
/// grouping some parameters into a new type.
|
2016-03-08 23:48:10 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2016-06-07 14:55:55 +00:00
|
|
|
/// ```rust
|
2016-03-08 23:48:10 +00:00
|
|
|
/// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) { .. }
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub TOO_MANY_ARGUMENTS,
|
|
|
|
Warn,
|
|
|
|
"functions with too many arguments"
|
|
|
|
}
|
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for public functions that dereferences raw pointer
|
|
|
|
/// arguments but are not marked unsafe.
|
2016-06-07 14:55:55 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** The function should probably be marked `unsafe`, since
|
|
|
|
/// for an arbitrary raw pointer, there is no way of telling for sure if it is
|
|
|
|
/// valid.
|
2016-06-07 14:55:55 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:**
|
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// * It does not check functions recursively so if the pointer is passed to a
|
|
|
|
/// private non-`unsafe` function which does the dereferencing, the lint won't trigger.
|
|
|
|
/// * It only checks for arguments whose type are raw pointers, not raw pointers
|
|
|
|
/// got from an argument in some other way (`fn foo(bar: &[*const u8])` or
|
|
|
|
/// `some_argument.get_raw_ptr()`).
|
2016-06-07 14:55:55 +00:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// pub fn foo(x: *const u8) { println!("{}", unsafe { *x }); }
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub NOT_UNSAFE_PTR_ARG_DEREF,
|
|
|
|
Warn,
|
|
|
|
"public functions dereferencing raw pointer arguments but not marked `unsafe`"
|
|
|
|
}
|
|
|
|
|
2016-03-08 23:48:10 +00:00
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct Functions {
|
|
|
|
threshold: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Functions {
|
|
|
|
pub fn new(threshold: u64) -> Functions {
|
2016-04-14 18:14:03 +00:00
|
|
|
Functions { threshold: threshold }
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for Functions {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
2016-06-07 14:55:55 +00:00
|
|
|
lint_array!(TOO_MANY_ARGUMENTS, NOT_UNSAFE_PTR_ARG_DEREF)
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_fn(
|
2016-12-21 11:14:54 +00:00
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
kind: intravisit::FnKind<'tcx>,
|
|
|
|
decl: &'tcx hir::FnDecl,
|
2017-01-04 04:40:42 +00:00
|
|
|
body: &'tcx hir::Body,
|
2016-12-21 11:14:54 +00:00
|
|
|
span: Span,
|
|
|
|
nodeid: ast::NodeId
|
2016-12-21 09:25:14 +00:00
|
|
|
) {
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::map::Node::*;
|
2016-03-08 23:48:10 +00:00
|
|
|
|
2016-08-01 14:59:14 +00:00
|
|
|
let is_impl = if let Some(NodeItem(item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
|
2016-06-07 14:55:55 +00:00
|
|
|
matches!(item.node, hir::ItemImpl(_, _, _, Some(_), _, _) | hir::ItemDefaultImpl(..))
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let unsafety = match kind {
|
|
|
|
hir::intravisit::FnKind::ItemFn(_, _, unsafety, _, _, _, _) => unsafety,
|
|
|
|
hir::intravisit::FnKind::Method(_, sig, _, _) => sig.unsafety,
|
|
|
|
hir::intravisit::FnKind::Closure(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// don't warn for implementations, it's not their fault
|
|
|
|
if !is_impl {
|
2016-08-08 15:21:47 +00:00
|
|
|
// don't lint extern functions decls, it's not their fault either
|
|
|
|
match kind {
|
|
|
|
hir::intravisit::FnKind::Method(_, &hir::MethodSig { abi: Abi::Rust, .. }, _, _) |
|
|
|
|
hir::intravisit::FnKind::ItemFn(_, _, _, _, Abi::Rust, _, _) => self.check_arg_number(cx, decl, span),
|
|
|
|
_ => {},
|
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:40:42 +00:00
|
|
|
self.check_raw_ptr(cx, unsafety, decl, &body.value, nodeid);
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
|
2017-01-03 17:19:17 +00:00
|
|
|
if let hir::TraitItemKind::Method(ref sig, eid) = item.node {
|
2016-08-08 15:21:47 +00:00
|
|
|
// don't lint extern functions decls, it's not their fault
|
|
|
|
if sig.abi == Abi::Rust {
|
|
|
|
self.check_arg_number(cx, &sig.decl, item.span);
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2017-01-04 04:40:42 +00:00
|
|
|
if let hir::TraitMethod::Provided(eid) = eid {
|
|
|
|
let expr = cx.tcx.map.body(eid).value;
|
|
|
|
self.check_raw_ptr(cx, sig.unsafety, &sig.decl, &expr, item.id);
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> Functions {
|
2016-03-08 23:48:10 +00:00
|
|
|
fn check_arg_number(&self, cx: &LateContext, decl: &hir::FnDecl, span: Span) {
|
|
|
|
let args = decl.inputs.len() as u64;
|
|
|
|
if args > self.threshold {
|
2016-04-14 18:14:03 +00:00
|
|
|
span_lint(cx,
|
|
|
|
TOO_MANY_ARGUMENTS,
|
|
|
|
span,
|
2016-05-12 15:52:51 +00:00
|
|
|
&format!("this function has too many arguments ({}/{})", args, self.threshold));
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
|
2016-12-21 09:25:14 +00:00
|
|
|
fn check_raw_ptr(
|
2016-12-21 11:14:54 +00:00
|
|
|
&self,
|
|
|
|
cx: &LateContext<'a, 'tcx>,
|
|
|
|
unsafety: hir::Unsafety,
|
|
|
|
decl: &'tcx hir::FnDecl,
|
|
|
|
expr: &'tcx hir::Expr,
|
2016-12-21 09:25:14 +00:00
|
|
|
nodeid: ast::NodeId
|
|
|
|
) {
|
2016-06-07 14:55:55 +00:00
|
|
|
if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(nodeid) {
|
|
|
|
let raw_ptrs = decl.inputs.iter().filter_map(|arg| raw_ptr_arg(cx, arg)).collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
if !raw_ptrs.is_empty() {
|
|
|
|
let mut v = DerefVisitor {
|
|
|
|
cx: cx,
|
|
|
|
ptrs: raw_ptrs,
|
|
|
|
};
|
|
|
|
|
2016-11-16 20:57:56 +00:00
|
|
|
hir::intravisit::walk_expr(&mut v, expr);
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:40:42 +00:00
|
|
|
fn raw_ptr_arg(cx: &LateContext, arg: &hir::Arg) -> Option<hir::def_id::DefId> {
|
|
|
|
if let (&hir::PatKind::Binding(_, def_id, _, _), &hir::TyPtr(_)) = (&arg.pat.node, &cx.tcx.map.get(arg.id)) {
|
2016-12-01 21:31:56 +00:00
|
|
|
Some(def_id)
|
2016-06-07 14:55:55 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DerefVisitor<'a, 'tcx: 'a> {
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
ptrs: HashSet<hir::def_id::DefId>,
|
|
|
|
}
|
|
|
|
|
2016-12-06 10:32:21 +00:00
|
|
|
impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
2016-06-07 15:29:22 +00:00
|
|
|
match expr.node {
|
|
|
|
hir::ExprCall(ref f, ref args) => {
|
2016-11-16 20:57:56 +00:00
|
|
|
let ty = self.cx.tcx.tables().expr_ty(f);
|
2016-06-07 15:29:22 +00:00
|
|
|
|
|
|
|
if type_is_unsafe_function(ty) {
|
|
|
|
for arg in args {
|
|
|
|
self.check_arg(arg);
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-06-07 14:55:55 +00:00
|
|
|
hir::ExprMethodCall(_, _, ref args) => {
|
|
|
|
let method_call = ty::MethodCall::expr(expr.id);
|
|
|
|
let base_type = self.cx.tcx.tables.borrow().method_map[&method_call].ty;
|
|
|
|
|
|
|
|
if type_is_unsafe_function(base_type) {
|
2016-06-07 15:29:22 +00:00
|
|
|
for arg in args {
|
|
|
|
self.check_arg(arg);
|
|
|
|
}
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
2016-12-20 17:21:30 +00:00
|
|
|
},
|
2016-06-07 15:29:22 +00:00
|
|
|
hir::ExprUnary(hir::UnDeref, ref ptr) => self.check_arg(ptr),
|
|
|
|
_ => (),
|
2016-06-07 14:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hir::intravisit::walk_expr(self, expr);
|
|
|
|
}
|
2016-12-06 10:32:21 +00:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
|
|
|
|
intravisit::NestedVisitorMap::All(&self.cx.tcx.map)
|
|
|
|
}
|
2016-03-08 23:48:10 +00:00
|
|
|
}
|
2016-06-07 15:29:22 +00:00
|
|
|
|
|
|
|
impl<'a, 'tcx: 'a> DerefVisitor<'a, 'tcx> {
|
|
|
|
fn check_arg(&self, ptr: &hir::Expr) {
|
2016-12-01 21:31:56 +00:00
|
|
|
if let hir::ExprPath(ref qpath) = ptr.node {
|
|
|
|
let def = self.cx.tcx.tables().qpath_def(qpath, ptr.id);
|
|
|
|
if self.ptrs.contains(&def.def_id()) {
|
2016-06-07 15:29:22 +00:00
|
|
|
span_lint(self.cx,
|
|
|
|
NOT_UNSAFE_PTR_ARG_DEREF,
|
|
|
|
ptr.span,
|
|
|
|
"this public function dereferences a raw pointer but is not marked `unsafe`");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|