2016-03-09 00:48:10 +01:00
|
|
|
use rustc::lint::*;
|
2016-04-07 17:46:48 +02:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::intravisit;
|
2016-03-09 00:48:10 +01:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::Span;
|
|
|
|
use utils::span_lint;
|
|
|
|
|
|
|
|
/// **What it does:** Check for functions with too many parameters.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Functions with lots of parameters are considered bad style and reduce
|
|
|
|
/// readability (“what does the 5th parameter means?”). Consider grouping some parameters into a
|
|
|
|
/// new type.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// 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"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy,Clone)]
|
|
|
|
pub struct Functions {
|
|
|
|
threshold: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Functions {
|
|
|
|
pub fn new(threshold: u64) -> Functions {
|
2016-04-14 20:14:03 +02:00
|
|
|
Functions { threshold: threshold }
|
2016-03-09 00:48:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for Functions {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(TOO_MANY_ARGUMENTS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass for Functions {
|
|
|
|
fn check_fn(&mut self, cx: &LateContext, _: intravisit::FnKind, decl: &hir::FnDecl, _: &hir::Block, span: Span, nodeid: ast::NodeId) {
|
2016-04-07 17:46:48 +02:00
|
|
|
use rustc::hir::map::Node::*;
|
2016-03-09 00:48:10 +01:00
|
|
|
|
|
|
|
if let Some(NodeItem(ref item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
|
|
|
|
match item.node {
|
2016-04-14 20:14:03 +02:00
|
|
|
hir::ItemImpl(_, _, _, Some(_), _, _) |
|
|
|
|
hir::ItemDefaultImpl(..) => return,
|
2016-03-09 00:48:10 +01:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.check_arg_number(cx, decl, span);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) {
|
|
|
|
if let hir::MethodTraitItem(ref sig, _) = item.node {
|
|
|
|
self.check_arg_number(cx, &sig.decl, item.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Functions {
|
|
|
|
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 20:14:03 +02:00
|
|
|
span_lint(cx,
|
|
|
|
TOO_MANY_ARGUMENTS,
|
|
|
|
span,
|
2016-03-09 00:48:10 +01:00
|
|
|
&format!("this function has to many arguments ({}/{})", args, self.threshold));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|