Lint function with too many arguments

This commit is contained in:
mcarton 2016-03-09 00:48:10 +01:00
parent 95e582a338
commit aa4daea364
5 changed files with 114 additions and 2 deletions

75
src/functions.rs Normal file
View file

@ -0,0 +1,75 @@
use rustc::lint::*;
use rustc_front::hir;
use rustc_front::intravisit;
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 {
Functions {
threshold: threshold
}
}
}
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) {
use rustc::front::map::Node::*;
if let Some(NodeItem(ref item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
match item.node {
hir::ItemImpl(_, _, _, Some(_), _, _) | hir::ItemDefaultImpl(..) => return,
_ => (),
}
}
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 {
span_lint(cx, TOO_MANY_ARGUMENTS, span,
&format!("this function has to many arguments ({}/{})", args, self.threshold));
}
}
}

View file

@ -63,6 +63,7 @@ pub mod escape;
pub mod eta_reduction;
pub mod format;
pub mod formatting;
pub mod functions;
pub mod identity_op;
pub mod if_not_else;
pub mod items_after_statements;
@ -211,6 +212,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_late_lint_pass(box unused_label::UnusedLabel);
reg.register_late_lint_pass(box new_without_default::NewWithoutDefault);
reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names));
reg.register_late_lint_pass(box functions::Functions::new(conf.too_many_arguments_threshold));
reg.register_lint_group("clippy_pedantic", vec![
array_indexing::INDEXING_SLICING,
@ -263,6 +265,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
format::USELESS_FORMAT,
formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING,
formatting::SUSPICIOUS_ELSE_FORMATTING,
functions::TOO_MANY_ARGUMENTS,
identity_op::IDENTITY_OP,
if_not_else::IF_NOT_ELSE,
items_after_statements::ITEMS_AFTER_STATEMENTS,

View file

@ -150,7 +150,7 @@ define_Conf! {
/// Lint: CYCLOMATIC_COMPLEXITY. The maximum cyclomatic complexity a function can have
("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25 => u64),
/// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have
("too-many-arguments-threshold", too_many_arguments_threshold, 6 => u64),
("too-many-arguments-threshold", too_many_arguments_threshold, 7 => u64),
/// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have
("type-complexity-threshold", type_complexity_threshold, 250 => u64),
}