Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove function body from "too many args" span #4053

Merged
merged 1 commit into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::utils::{iter_input_pats, snippet, span_lint, type_is_unsafe_function};
use std::convert::TryFrom;

use crate::utils::{iter_input_pats, snippet, snippet_opt, span_lint, type_is_unsafe_function};
use matches::matches;
use rustc::hir;
use rustc::hir::def::Def;
Expand All @@ -8,7 +10,7 @@ use rustc::ty;
use rustc::{declare_tool_lint, impl_lint_pass};
use rustc_data_structures::fx::FxHashSet;
use rustc_target::spec::abi::Abi;
use syntax::source_map::Span;
use syntax::source_map::{BytePos, Span};

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

impl<'a, 'tcx> Functions {
fn check_arg_number(self, cx: &LateContext<'_, '_>, decl: &hir::FnDecl, span: Span) {
// Remove the function body from the span. We can't use `SourceMap::def_span` because the
// argument list might span multiple lines.
let span = if let Some(snippet) = snippet_opt(cx, span) {
let snippet = snippet.split('{').nth(0).unwrap_or("").trim_end();
if snippet.is_empty() {
span
} else {
span.with_hi(BytePos(span.lo().0 + u32::try_from(snippet.len()).unwrap()))
}
} else {
span
};

let args = decl.inputs.len() as u64;
if args > self.threshold {
span_lint(
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ fn good(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32,

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

#[rustfmt::skip]
fn bad_multiline(
one: u32,
two: u32,
three: &str,
four: bool,
five: f32,
six: f32,
seven: bool,
eight: ()
) {
let _one = one;
let _two = two;
let _three = three;
let _four = four;
let _five = five;
let _six = six;
let _seven = seven;
}

// don't lint extern fns
extern "C" fn extern_fn(
_one: u32,
Expand Down
40 changes: 26 additions & 14 deletions tests/ui/functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,89 @@ error: this function has too many arguments (8/7)
--> $DIR/functions.rs:8:1
|
LL | fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::too-many-arguments` implied by `-D warnings`

error: this function has too many arguments (8/7)
--> $DIR/functions.rs:25:5
--> $DIR/functions.rs:11:1
|
LL | / fn bad_multiline(
LL | | one: u32,
LL | | two: u32,
LL | | three: &str,
... |
LL | | eight: ()
LL | | ) {
| |_^

error: this function has too many arguments (8/7)
--> $DIR/functions.rs:45:5
|
LL | fn bad(_one: u32, _two: u32, _three: &str, _four: bool, _five: f32, _six: f32, _seven: bool, _eight: ());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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

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

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

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

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

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

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

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

error: aborting due to 12 previous errors
error: aborting due to 13 previous errors