Skip to content

Commit 8b906f9

Browse files
committed
Auto merge of #4053 - euclio:too-many-args-span, r=flip1995
remove function body from "too many args" span changelog: Remove the function body from the "too many arguments" span.
2 parents 8151a17 + 9253506 commit 8b906f9

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

clippy_lints/src/functions.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::utils::{iter_input_pats, snippet, span_lint, type_is_unsafe_function};
1+
use std::convert::TryFrom;
2+
3+
use crate::utils::{iter_input_pats, snippet, snippet_opt, span_lint, type_is_unsafe_function};
24
use matches::matches;
35
use rustc::hir;
46
use rustc::hir::def::Def;
@@ -8,7 +10,7 @@ use rustc::ty;
810
use rustc::{declare_tool_lint, impl_lint_pass};
911
use rustc_data_structures::fx::FxHashSet;
1012
use rustc_target::spec::abi::Abi;
11-
use syntax::source_map::Span;
13+
use syntax::source_map::{BytePos, Span};
1214

1315
declare_clippy_lint! {
1416
/// **What it does:** Checks for functions with too many parameters.
@@ -162,6 +164,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Functions {
162164

163165
impl<'a, 'tcx> Functions {
164166
fn check_arg_number(self, cx: &LateContext<'_, '_>, decl: &hir::FnDecl, span: Span) {
167+
// Remove the function body from the span. We can't use `SourceMap::def_span` because the
168+
// argument list might span multiple lines.
169+
let span = if let Some(snippet) = snippet_opt(cx, span) {
170+
let snippet = snippet.split('{').nth(0).unwrap_or("").trim_end();
171+
if snippet.is_empty() {
172+
span
173+
} else {
174+
span.with_hi(BytePos(span.lo().0 + u32::try_from(snippet.len()).unwrap()))
175+
}
176+
} else {
177+
span
178+
};
179+
165180
let args = decl.inputs.len() as u64;
166181
if args > self.threshold {
167182
span_lint(

tests/ui/functions.rs

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32,
77

88
fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
99

10+
#[rustfmt::skip]
11+
fn bad_multiline(
12+
one: u32,
13+
two: u32,
14+
three: &str,
15+
four: bool,
16+
five: f32,
17+
six: f32,
18+
seven: bool,
19+
eight: ()
20+
) {
21+
let _one = one;
22+
let _two = two;
23+
let _three = three;
24+
let _four = four;
25+
let _five = five;
26+
let _six = six;
27+
let _seven = seven;
28+
}
29+
1030
// don't lint extern fns
1131
extern "C" fn extern_fn(
1232
_one: u32,

tests/ui/functions.stderr

+26-14
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,89 @@ error: this function has too many arguments (8/7)
22
--> $DIR/functions.rs:8:1
33
|
44
LL | fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::too-many-arguments` implied by `-D warnings`
88

99
error: this function has too many arguments (8/7)
10-
--> $DIR/functions.rs:25:5
10+
--> $DIR/functions.rs:11:1
11+
|
12+
LL | / fn bad_multiline(
13+
LL | | one: u32,
14+
LL | | two: u32,
15+
LL | | three: &str,
16+
... |
17+
LL | | eight: ()
18+
LL | | ) {
19+
| |_^
20+
21+
error: this function has too many arguments (8/7)
22+
--> $DIR/functions.rs:45:5
1123
|
1224
LL | fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ());
1325
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1426

1527
error: this function has too many arguments (8/7)
16-
--> $DIR/functions.rs:34:5
28+
--> $DIR/functions.rs:54:5
1729
|
1830
LL | fn bad_method(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2032

2133
error: this public function dereferences a raw pointer but is not marked `unsafe`
22-
--> $DIR/functions.rs:43:34
34+
--> $DIR/functions.rs:63:34
2335
|
2436
LL | println!("{}", unsafe { *p });
2537
| ^
2638
|
2739
= note: `-D clippy::not-unsafe-ptr-arg-deref` implied by `-D warnings`
2840

2941
error: this public function dereferences a raw pointer but is not marked `unsafe`
30-
--> $DIR/functions.rs:44:35
42+
--> $DIR/functions.rs:64:35
3143
|
3244
LL | println!("{:?}", unsafe { p.as_ref() });
3345
| ^
3446

3547
error: this public function dereferences a raw pointer but is not marked `unsafe`
36-
--> $DIR/functions.rs:45:33
48+
--> $DIR/functions.rs:65:33
3749
|
3850
LL | unsafe { std::ptr::read(p) };
3951
| ^
4052

4153
error: this public function dereferences a raw pointer but is not marked `unsafe`
42-
--> $DIR/functions.rs:56:30
54+
--> $DIR/functions.rs:76:30
4355
|
4456
LL | println!("{}", unsafe { *p });
4557
| ^
4658

4759
error: this public function dereferences a raw pointer but is not marked `unsafe`
48-
--> $DIR/functions.rs:57:31
60+
--> $DIR/functions.rs:77:31
4961
|
5062
LL | println!("{:?}", unsafe { p.as_ref() });
5163
| ^
5264

5365
error: this public function dereferences a raw pointer but is not marked `unsafe`
54-
--> $DIR/functions.rs:58:29
66+
--> $DIR/functions.rs:78:29
5567
|
5668
LL | unsafe { std::ptr::read(p) };
5769
| ^
5870

5971
error: this public function dereferences a raw pointer but is not marked `unsafe`
60-
--> $DIR/functions.rs:67:34
72+
--> $DIR/functions.rs:87:34
6173
|
6274
LL | println!("{}", unsafe { *p });
6375
| ^
6476

6577
error: this public function dereferences a raw pointer but is not marked `unsafe`
66-
--> $DIR/functions.rs:68:35
78+
--> $DIR/functions.rs:88:35
6779
|
6880
LL | println!("{:?}", unsafe { p.as_ref() });
6981
| ^
7082

7183
error: this public function dereferences a raw pointer but is not marked `unsafe`
72-
--> $DIR/functions.rs:69:33
84+
--> $DIR/functions.rs:89:33
7385
|
7486
LL | unsafe { std::ptr::read(p) };
7587
| ^
7688

77-
error: aborting due to 12 previous errors
89+
error: aborting due to 13 previous errors
7890

0 commit comments

Comments
 (0)