rust/clippy_lints/src/functions.rs

78 lines
2.1 KiB
Rust
Raw Normal View History

2016-03-08 17:48:10 -06:00
use rustc::lint::*;
use rustc::hir;
use rustc::hir::intravisit;
2016-03-08 17:48:10 -06: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
2016-05-12 10:52:51 -05:00
/// readability (“what does the 5th parameter mean?”). Consider grouping some parameters into a
2016-03-08 17:48:10 -06:00
/// 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 13:14:03 -05:00
Functions { threshold: threshold }
2016-03-08 17:48:10 -06: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) {
use rustc::hir::map::Node::*;
2016-03-08 17:48:10 -06:00
if let Some(NodeItem(ref item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
match item.node {
2016-04-14 13:14:03 -05:00
hir::ItemImpl(_, _, _, Some(_), _, _) |
hir::ItemDefaultImpl(..) => return,
2016-03-08 17:48:10 -06: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 13:14:03 -05:00
span_lint(cx,
TOO_MANY_ARGUMENTS,
span,
2016-05-12 10:52:51 -05:00
&format!("this function has too many arguments ({}/{})", args, self.threshold));
2016-03-08 17:48:10 -06:00
}
}
}