Skip to content

Commit aa4daea

Browse files
committed
Lint function with too many arguments
1 parent 95e582a commit aa4daea

File tree

5 files changed

+114
-2
lines changed

5 files changed

+114
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Table of contents:
1414
* [License](#license)
1515

1616
##Lints
17-
There are 135 lints included in this crate:
17+
There are 136 lints included in this crate:
1818

1919
name | default | meaning
2020
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -130,6 +130,7 @@ name
130130
[suspicious_assignment_formatting](https://github.com/Manishearth/rust-clippy/wiki#suspicious_assignment_formatting) | warn | suspicious formatting of `*=`, `-=` or `!=`
131131
[suspicious_else_formatting](https://github.com/Manishearth/rust-clippy/wiki#suspicious_else_formatting) | warn | suspicious formatting of `else if`
132132
[temporary_assignment](https://github.com/Manishearth/rust-clippy/wiki#temporary_assignment) | warn | assignments to temporaries
133+
[too_many_arguments](https://github.com/Manishearth/rust-clippy/wiki#too_many_arguments) | warn | functions with too many arguments
133134
[toplevel_ref_arg](https://github.com/Manishearth/rust-clippy/wiki#toplevel_ref_arg) | warn | An entire binding was declared as `ref`, in a function argument (`fn foo(ref x: Bar)`), or a `let` statement (`let ref x = foo()`). In such cases, it is preferred to take references with `&`.
134135
[trivial_regex](https://github.com/Manishearth/rust-clippy/wiki#trivial_regex) | warn | finds trivial regular expressions in `Regex::new(_)` invocations
135136
[type_complexity](https://github.com/Manishearth/rust-clippy/wiki#type_complexity) | warn | usage of very complex types; recommends factoring out parts into `type` definitions

src/functions.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use rustc::lint::*;
2+
use rustc_front::hir;
3+
use rustc_front::intravisit;
4+
use syntax::ast;
5+
use syntax::codemap::Span;
6+
use utils::span_lint;
7+
8+
/// **What it does:** Check for functions with too many parameters.
9+
///
10+
/// **Why is this bad?** Functions with lots of parameters are considered bad style and reduce
11+
/// readability (“what does the 5th parameter means?”). Consider grouping some parameters into a
12+
/// new type.
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
///
18+
/// ```
19+
/// fn foo(x: u32, y: u32, name: &str, c: Color, w: f32, h: f32, a: f32, b: f32) { .. }
20+
/// ```
21+
declare_lint! {
22+
pub TOO_MANY_ARGUMENTS,
23+
Warn,
24+
"functions with too many arguments"
25+
}
26+
27+
#[derive(Copy,Clone)]
28+
pub struct Functions {
29+
threshold: u64,
30+
}
31+
32+
impl Functions {
33+
pub fn new(threshold: u64) -> Functions {
34+
Functions {
35+
threshold: threshold
36+
}
37+
}
38+
}
39+
40+
impl LintPass for Functions {
41+
fn get_lints(&self) -> LintArray {
42+
lint_array!(TOO_MANY_ARGUMENTS)
43+
}
44+
}
45+
46+
impl LateLintPass for Functions {
47+
fn check_fn(&mut self, cx: &LateContext, _: intravisit::FnKind, decl: &hir::FnDecl, _: &hir::Block, span: Span, nodeid: ast::NodeId) {
48+
use rustc::front::map::Node::*;
49+
50+
if let Some(NodeItem(ref item)) = cx.tcx.map.find(cx.tcx.map.get_parent_node(nodeid)) {
51+
match item.node {
52+
hir::ItemImpl(_, _, _, Some(_), _, _) | hir::ItemDefaultImpl(..) => return,
53+
_ => (),
54+
}
55+
}
56+
57+
self.check_arg_number(cx, decl, span);
58+
}
59+
60+
fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) {
61+
if let hir::MethodTraitItem(ref sig, _) = item.node {
62+
self.check_arg_number(cx, &sig.decl, item.span);
63+
}
64+
}
65+
}
66+
67+
impl Functions {
68+
fn check_arg_number(&self, cx: &LateContext, decl: &hir::FnDecl, span: Span) {
69+
let args = decl.inputs.len() as u64;
70+
if args > self.threshold {
71+
span_lint(cx, TOO_MANY_ARGUMENTS, span,
72+
&format!("this function has to many arguments ({}/{})", args, self.threshold));
73+
}
74+
}
75+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub mod escape;
6363
pub mod eta_reduction;
6464
pub mod format;
6565
pub mod formatting;
66+
pub mod functions;
6667
pub mod identity_op;
6768
pub mod if_not_else;
6869
pub mod items_after_statements;
@@ -211,6 +212,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
211212
reg.register_late_lint_pass(box unused_label::UnusedLabel);
212213
reg.register_late_lint_pass(box new_without_default::NewWithoutDefault);
213214
reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names));
215+
reg.register_late_lint_pass(box functions::Functions::new(conf.too_many_arguments_threshold));
214216

215217
reg.register_lint_group("clippy_pedantic", vec![
216218
array_indexing::INDEXING_SLICING,
@@ -263,6 +265,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
263265
format::USELESS_FORMAT,
264266
formatting::SUSPICIOUS_ASSIGNMENT_FORMATTING,
265267
formatting::SUSPICIOUS_ELSE_FORMATTING,
268+
functions::TOO_MANY_ARGUMENTS,
266269
identity_op::IDENTITY_OP,
267270
if_not_else::IF_NOT_ELSE,
268271
items_after_statements::ITEMS_AFTER_STATEMENTS,

src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ define_Conf! {
150150
/// Lint: CYCLOMATIC_COMPLEXITY. The maximum cyclomatic complexity a function can have
151151
("cyclomatic-complexity-threshold", cyclomatic_complexity_threshold, 25 => u64),
152152
/// Lint: TOO_MANY_ARGUMENTS. The maximum number of argument a function or method can have
153-
("too-many-arguments-threshold", too_many_arguments_threshold, 6 => u64),
153+
("too-many-arguments-threshold", too_many_arguments_threshold, 7 => u64),
154154
/// Lint: TYPE_COMPLEXITY. The maximum complexity a type can have
155155
("type-complexity-threshold", type_complexity_threshold, 250 => u64),
156156
}

tests/compile-fail/functions.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
4+
#![deny(clippy)]
5+
#![allow(dead_code)]
6+
7+
fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool) {}
8+
9+
fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {
10+
//~^ ERROR: this function has to many arguments (8/7)
11+
}
12+
13+
trait Foo {
14+
fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool);
15+
fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ());
16+
//~^ ERROR: this function has to many arguments (8/7)
17+
}
18+
19+
struct Bar;
20+
21+
impl Bar {
22+
fn good_method(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool) {}
23+
fn bad_method(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
24+
//~^ ERROR: this function has to many arguments (8/7)
25+
}
26+
27+
// ok, we don’t want to warn implementations
28+
impl Foo for Bar {
29+
fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool) {}
30+
fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
31+
}
32+
33+
fn main() {}

0 commit comments

Comments
 (0)