1 use rustc::hir::intravisit;
5 use std::collections::HashSet;
8 use syntax::codemap::Span;
9 use utils::{span_lint, type_is_unsafe_function};
11 /// **What it does:** Checks for functions with too many parameters.
13 /// **Why is this bad?** Functions with lots of parameters are considered bad
14 /// style and reduce readability (“what does the 5th parameter mean?”). Consider
15 /// grouping some parameters into a new type.
17 /// **Known problems:** None.
21 /// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) { .. }
24 pub TOO_MANY_ARGUMENTS,
26 "functions with too many arguments"
29 /// **What it does:** Checks for public functions that dereferences raw pointer
30 /// arguments but are not marked unsafe.
32 /// **Why is this bad?** The function should probably be marked `unsafe`, since
33 /// for an arbitrary raw pointer, there is no way of telling for sure if it is
36 /// **Known problems:**
38 /// * It does not check functions recursively so if the pointer is passed to a
39 /// private non-`unsafe` function which does the dereferencing, the lint won't trigger.
40 /// * It only checks for arguments whose type are raw pointers, not raw pointers
41 /// got from an argument in some other way (`fn foo(bar: &[*const u8])` or
42 /// `some_argument.get_raw_ptr()`).
46 /// pub fn foo(x: *const u8) { println!("{}", unsafe { *x }); }
49 pub NOT_UNSAFE_PTR_ARG_DEREF,
51 "public functions dereferencing raw pointer arguments but not marked `unsafe`"
55 pub struct Functions {
60 pub fn new(threshold: u64) -> Functions {
61 Functions { threshold: threshold }
65 impl LintPass for Functions {
66 fn get_lints(&self) -> LintArray {
67 lint_array!(TOO_MANY_ARGUMENTS, NOT_UNSAFE_PTR_ARG_DEREF)
71 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
72 fn check_fn(&mut self, cx: &LateContext<'a, 'tcx>, kind: intravisit::FnKind<'tcx>, decl: &'tcx hir::FnDecl,
73 expr: &'tcx hir::Expr, span: Span, nodeid: ast::NodeId) {
74 use rustc::hir::map::Node::*;
76 let is_impl = if let Some(NodeItem(item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
77 matches!(item.node, hir::ItemImpl(_, _, _, Some(_), _, _) | hir::ItemDefaultImpl(..))
82 let unsafety = match kind {
83 hir::intravisit::FnKind::ItemFn(_, _, unsafety, _, _, _, _) => unsafety,
84 hir::intravisit::FnKind::Method(_, sig, _, _) => sig.unsafety,
85 hir::intravisit::FnKind::Closure(_) => return,
88 // don't warn for implementations, it's not their fault
90 // don't lint extern functions decls, it's not their fault either
92 hir::intravisit::FnKind::Method(_, &hir::MethodSig { abi: Abi::Rust, .. }, _, _) |
93 hir::intravisit::FnKind::ItemFn(_, _, _, _, Abi::Rust, _, _) => self.check_arg_number(cx, decl, span),
98 self.check_raw_ptr(cx, unsafety, decl, expr, nodeid);
101 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::TraitItem) {
102 if let hir::MethodTraitItem(ref sig, eid) = item.node {
103 // don't lint extern functions decls, it's not their fault
104 if sig.abi == Abi::Rust {
105 self.check_arg_number(cx, &sig.decl, item.span);
108 if let Some(eid) = eid {
109 let expr = cx.tcx.map.expr(eid);
110 self.check_raw_ptr(cx, sig.unsafety, &sig.decl, expr, item.id);
116 impl<'a, 'tcx> Functions {
117 fn check_arg_number(&self, cx: &LateContext, decl: &hir::FnDecl, span: Span) {
118 let args = decl.inputs.len() as u64;
119 if args > self.threshold {
123 &format!("this function has too many arguments ({}/{})", args, self.threshold));
127 fn check_raw_ptr(&self, cx: &LateContext<'a, 'tcx>, unsafety: hir::Unsafety, decl: &'tcx hir::FnDecl,
128 expr: &'tcx hir::Expr, nodeid: ast::NodeId) {
129 if unsafety == hir::Unsafety::Normal && cx.access_levels.is_exported(nodeid) {
130 let raw_ptrs = decl.inputs.iter().filter_map(|arg| raw_ptr_arg(cx, arg)).collect::<HashSet<_>>();
132 if !raw_ptrs.is_empty() {
133 let mut v = DerefVisitor {
138 hir::intravisit::walk_expr(&mut v, expr);
144 fn raw_ptr_arg(_cx: &LateContext, arg: &hir::Arg) -> Option<hir::def_id::DefId> {
145 if let (&hir::PatKind::Binding(_, def_id, _, _), &hir::TyPtr(_)) = (&arg.pat.node, &arg.ty.node) {
152 struct DerefVisitor<'a, 'tcx: 'a> {
153 cx: &'a LateContext<'a, 'tcx>,
154 ptrs: HashSet<hir::def_id::DefId>,
157 impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
158 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
160 hir::ExprCall(ref f, ref args) => {
161 let ty = self.cx.tcx.tables().expr_ty(f);
163 if type_is_unsafe_function(ty) {
169 hir::ExprMethodCall(_, _, ref args) => {
170 let method_call = ty::MethodCall::expr(expr.id);
171 let base_type = self.cx.tcx.tables.borrow().method_map[&method_call].ty;
173 if type_is_unsafe_function(base_type) {
179 hir::ExprUnary(hir::UnDeref, ref ptr) => self.check_arg(ptr),
183 hir::intravisit::walk_expr(self, expr);
185 fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
186 intravisit::NestedVisitorMap::All(&self.cx.tcx.map)
190 impl<'a, 'tcx: 'a> DerefVisitor<'a, 'tcx> {
191 fn check_arg(&self, ptr: &hir::Expr) {
192 if let hir::ExprPath(ref qpath) = ptr.node {
193 let def = self.cx.tcx.tables().qpath_def(qpath, ptr.id);
194 if self.ptrs.contains(&def.def_id()) {
196 NOT_UNSAFE_PTR_ARG_DEREF,
198 "this public function dereferences a raw pointer but is not marked `unsafe`");