From 8d2328d9a5ed45bf20739b0164dfad2bc97da9a7 Mon Sep 17 00:00:00 2001 From: llogiq Date: Mon, 4 May 2015 08:15:24 +0200 Subject: [PATCH] Added &String matching and renamed to vec_ptr_arg to ptr_arg, also added README section --- README.md | 3 +++ src/lib.rs | 9 ++++----- src/{vec_ptr_arg.rs => ptr_arg.rs} | 16 ++++++++++------ tests/compile-fail/ptr_arg.rs | 20 ++++++++++++++++++++ tests/compile-fail/vec_ptr_arg.rs | 14 -------------- 5 files changed, 37 insertions(+), 25 deletions(-) rename src/{vec_ptr_arg.rs => ptr_arg.rs} (78%) create mode 100644 tests/compile-fail/ptr_arg.rs delete mode 100644 tests/compile-fail/vec_ptr_arg.rs diff --git a/README.md b/README.md index 0fc9a6c88..322491632 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,9 @@ Lints included in this crate: - `eq_op`: Warns on equal operands on both sides of a comparison or bitwise combination - `bad_bit_mask`: Denies expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have) - `needless_bool` : Warns on if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }` + - `ptr_arg`: Warns on fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively + +In your code, you may add `#![plugin(clippy)]` to use it (you may also need to include a `#![feature(plugin)]` line) You can allow/warn/deny the whole set using the `clippy` lint group (`#[allow(clippy)]`, etc) diff --git a/src/lib.rs b/src/lib.rs index a9faf1998..8cf8650c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,8 +18,8 @@ pub mod types; pub mod misc; pub mod eq_op; pub mod bit_mask; +pub mod ptr_arg; pub mod needless_bool; -pub mod vec_ptr_arg; #[plugin_registrar] pub fn plugin_registrar(reg: &mut Registry) { @@ -29,14 +29,13 @@ pub fn plugin_registrar(reg: &mut Registry) { reg.register_lint_pass(box misc::TopLevelRefPass as LintPassObject); reg.register_lint_pass(box eq_op::EqOp as LintPassObject); reg.register_lint_pass(box bit_mask::BitMask as LintPassObject); + reg.register_lint_pass(box ptr_arg::PtrArg as LintPassObject); reg.register_lint_pass(box needless_bool::NeedlessBool as LintPassObject); - reg.register_lint_pass(box vec_ptr_arg::VecPtrArg as LintPassObject); reg.register_lint_group("clippy", vec![types::BOX_VEC, types::LINKEDLIST, misc::SINGLE_MATCH, misc::STR_TO_STRING, misc::TOPLEVEL_REF_ARG, eq_op::EQ_OP, - bit_mask::BAD_BIT_MASK, - needless_bool::NEEDLESS_BOOL, - vec_ptr_arg::VEC_PTR_ARG + bit_mask::BAD_BIT_MASK, ptr_arg::PTR_ARG, + needless_bool::NEEDLESS_BOOL ]); } diff --git a/src/vec_ptr_arg.rs b/src/ptr_arg.rs similarity index 78% rename from src/vec_ptr_arg.rs rename to src/ptr_arg.rs index 88fdc5cf0..378d30de5 100644 --- a/src/vec_ptr_arg.rs +++ b/src/ptr_arg.rs @@ -13,18 +13,18 @@ use syntax::codemap::Span; use types::match_ty_unwrap; declare_lint! { - pub VEC_PTR_ARG, + pub PTR_ARG, Allow, - "Warn on declaration of a &Vec-typed method argument" + "Warn on declaration of a &Vec- or &String-typed method argument" } #[derive(Copy,Clone)] -pub struct VecPtrArg; +pub struct PtrArg; -impl LintPass for VecPtrArg { +impl LintPass for PtrArg { fn get_lints(&self) -> LintArray { - lint_array!(VEC_PTR_ARG) + lint_array!(PTR_ARG) } fn check_item(&mut self, cx: &Context, item: &Item) { @@ -59,7 +59,11 @@ fn check_fn(cx: &Context, decl: &FnDecl) { fn check_ptr_subtype(cx: &Context, span: Span, ty: &Ty) { if match_ty_unwrap(ty, &["Vec"]).is_some() { - cx.span_lint(VEC_PTR_ARG, span, + cx.span_lint(PTR_ARG, span, "Writing '&Vec<_>' instead of '&[_]' involves one more reference and cannot be used with non-vec-based slices. Consider changing the type to &[...]"); + } else { if match_ty_unwrap(ty, &["String"]).is_some() { + cx.span_lint(PTR_ARG, span, + "Writing '&String' instead of '&str' involves a new Object where a slices will do. Consider changing the type to &str"); + } } } diff --git a/tests/compile-fail/ptr_arg.rs b/tests/compile-fail/ptr_arg.rs new file mode 100644 index 000000000..2fe36eafa --- /dev/null +++ b/tests/compile-fail/ptr_arg.rs @@ -0,0 +1,20 @@ +#![feature(plugin)] +#![plugin(clippy)] + +#[deny(ptr_arg)] +#[allow(unused)] +fn do_vec(x: &Vec) { //~ERROR: Writing '&Vec<_>' instead of '&[_]' + //Nothing here +} + +#[deny(ptr_arg)] +#[allow(unused)] +fn do_str(x: &String) { //~ERROR + //Nothing here either +} + +fn main() { + let x = vec![1i64, 2, 3]; + do_vec(&x); + do_str(&"hello".to_owned()); +} diff --git a/tests/compile-fail/vec_ptr_arg.rs b/tests/compile-fail/vec_ptr_arg.rs deleted file mode 100644 index 5c4338e35..000000000 --- a/tests/compile-fail/vec_ptr_arg.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![feature(plugin)] -#![plugin(clippy)] - -#[deny(vec_ptr_arg)] -#[allow(unused)] -fn go(x: &Vec) { //~ERROR: Writing '&Vec<_>' instead of '&[_]' - //Nothing here -} - - -fn main() { - let x = vec![1i64, 2, 3]; - go(&x); -}