3 use rustc::hir::intravisit;
5 use syntax::codemap::Span;
8 /// **What it does:** Check for functions with too many parameters.
10 /// **Why is this bad?** Functions with lots of parameters are considered bad style and reduce
11 /// readability (“what does the 5th parameter mean?”). Consider grouping some parameters into a
14 /// **Known problems:** None.
19 /// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) { .. }
22 pub TOO_MANY_ARGUMENTS,
24 "functions with too many arguments"
28 pub struct Functions {
33 pub fn new(threshold: u64) -> Functions {
34 Functions { threshold: threshold }
38 impl LintPass for Functions {
39 fn get_lints(&self) -> LintArray {
40 lint_array!(TOO_MANY_ARGUMENTS)
44 impl LateLintPass for Functions {
45 fn check_fn(&mut self, cx: &LateContext, _: intravisit::FnKind, decl: &hir::FnDecl, _: &hir::Block, span: Span, nodeid: ast::NodeId) {
46 use rustc::hir::map::Node::*;
48 if let Some(NodeItem(ref item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
50 hir::ItemImpl(_, _, _, Some(_), _, _) |
51 hir::ItemDefaultImpl(..) => return,
56 self.check_arg_number(cx, decl, span);
59 fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) {
60 if let hir::MethodTraitItem(ref sig, _) = item.node {
61 self.check_arg_number(cx, &sig.decl, item.span);
67 fn check_arg_number(&self, cx: &LateContext, decl: &hir::FnDecl, span: Span) {
68 let args = decl.inputs.len() as u64;
69 if args > self.threshold {
73 &format!("this function has too many arguments ({}/{})", args, self.threshold));