3 use rustc::middle::const_val::ConstVal;
4 use rustc::ty::subst::{Subst, TypeSpace};
6 use rustc_const_eval::EvalHint::ExprTypeChecked;
7 use rustc_const_eval::eval_const_expr_partial;
10 use syntax::codemap::Span;
12 use utils::{get_trait_def_id, implements_trait, in_external_macro, in_macro, match_path, match_trait_method,
13 match_type, method_chain_args, return_ty, same_tys, snippet, snippet_opt, span_lint,
14 span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
15 use utils::MethodArgs;
19 pub struct MethodsPass;
21 /// **What it does:** This lint checks for `.unwrap()` calls on `Option`s.
23 /// **Why is this bad?** Usually it is better to handle the `None` case, or to at least call `.expect(_)` with a more helpful message. Still, for a lot of quick-and-dirty code, `unwrap` is a good choice, which is why this lint is `Allow` by default.
25 /// **Known problems:** None
27 /// **Example:** `x.unwrap()`
29 pub OPTION_UNWRAP_USED, Allow,
30 "using `Option.unwrap()`, which should at least get a better message using `expect()`"
33 /// **What it does:** This lint checks for `.unwrap()` calls on `Result`s.
35 /// **Why is this bad?** `result.unwrap()` will let the thread panic on `Err` values. Normally, you want to implement more sophisticated error handling, and propagate errors upwards with `try!`.
37 /// Even if you want to panic on errors, not all `Error`s implement good messages on display. Therefore it may be beneficial to look at the places where they may get displayed. Activate this lint to do just that.
39 /// **Known problems:** None
41 /// **Example:** `x.unwrap()`
43 pub RESULT_UNWRAP_USED, Allow,
44 "using `Result.unwrap()`, which might be better handled"
47 /// **What it does:** This lint checks for methods that should live in a trait implementation of a `std` trait (see [llogiq's blog post](http://llogiq.github.io/2015/07/30/traits.html) for further information) instead of an inherent implementation.
49 /// **Why is this bad?** Implementing the traits improve ergonomics for users of the code, often with very little cost. Also people seeing a `mul(..)` method may expect `*` to work equally, so you should have good reason to disappoint them.
51 /// **Known problems:** None
57 /// fn add(&self, other: &X) -> X { .. }
61 pub SHOULD_IMPLEMENT_TRAIT, Warn,
62 "defining a method that should be implementing a std trait"
65 /// **What it does:** This lint checks for methods with certain name prefixes and which doesn't match how self is taken. The actual rules are:
67 /// |Prefix |`self` taken |
68 /// |-------|--------------------|
69 /// |`as_` |`&self` or &mut self|
72 /// |`is_` |`&self` or none |
75 /// **Why is this bad?** Consistency breeds readability. If you follow the conventions, your users won't be surprised that they e.g. need to supply a mutable reference to a `as_..` function.
77 /// **Known problems:** None
83 /// fn as_str(self) -> &str { .. }
87 pub WRONG_SELF_CONVENTION, Warn,
88 "defining a method named with an established prefix (like \"into_\") that takes \
89 `self` with the wrong convention"
92 /// **What it does:** This is the same as [`wrong_self_convention`](#wrong_self_convention), but for public items.
94 /// **Why is this bad?** See [`wrong_self_convention`](#wrong_self_convention).
96 /// **Known problems:** Actually *renaming* the function may break clients if the function is part of the public interface. In that case, be mindful of the stability guarantees you've given your users.
101 /// pub fn as_str(self) -> &str { .. }
105 pub WRONG_PUB_SELF_CONVENTION, Allow,
106 "defining a public method named with an established prefix (like \"into_\") that takes \
107 `self` with the wrong convention"
110 /// **What it does:** This lint checks for usage of `ok().expect(..)`.
112 /// **Why is this bad?** Because you usually call `expect()` on the `Result` directly to get a good error message.
114 /// **Known problems:** None.
116 /// **Example:** `x.ok().expect("why did I do this again?")`
119 "using `ok().expect()`, which gives worse error messages than \
120 calling `expect` directly on the Result"
123 /// **What it does:** This lint checks for usage of `_.map(_).unwrap_or(_)`.
125 /// **Why is this bad?** Readability, this can be written more concisely as `_.map_or(_, _)`.
127 /// **Known problems:** None.
129 /// **Example:** `x.map(|a| a + 1).unwrap_or(0)`
131 pub OPTION_MAP_UNWRAP_OR, Warn,
132 "using `Option.map(f).unwrap_or(a)`, which is more succinctly expressed as \
136 /// **What it does:** This lint `Warn`s on `_.map(_).unwrap_or_else(_)`.
138 /// **Why is this bad?** Readability, this can be written more concisely as `_.map_or_else(_, _)`.
140 /// **Known problems:** None.
142 /// **Example:** `x.map(|a| a + 1).unwrap_or_else(some_function)`
144 pub OPTION_MAP_UNWRAP_OR_ELSE, Warn,
145 "using `Option.map(f).unwrap_or_else(g)`, which is more succinctly expressed as \
149 /// **What it does:** This lint `Warn`s on `_.filter(_).next()`.
151 /// **Why is this bad?** Readability, this can be written more concisely as `_.find(_)`.
153 /// **Known problems:** None.
155 /// **Example:** `iter.filter(|x| x == 0).next()`
157 pub FILTER_NEXT, Warn,
158 "using `filter(p).next()`, which is more succinctly expressed as `.find(p)`"
161 /// **What it does:** This lint `Warn`s on an iterator search (such as `find()`, `position()`, or
162 /// `rposition()`) followed by a call to `is_some()`.
164 /// **Why is this bad?** Readability, this can be written more concisely as `_.any(_)`.
166 /// **Known problems:** None.
168 /// **Example:** `iter.find(|x| x == 0).is_some()`
170 pub SEARCH_IS_SOME, Warn,
171 "using an iterator search followed by `is_some()`, which is more succinctly \
172 expressed as a call to `any()`"
175 /// **What it does:** This lint `Warn`s on using `.chars().next()` on a `str` to check if it
176 /// starts with a given char.
178 /// **Why is this bad?** Readability, this can be written more concisely as `_.starts_with(_)`.
180 /// **Known problems:** None.
182 /// **Example:** `name.chars().next() == Some('_')`
184 pub CHARS_NEXT_CMP, Warn,
185 "using `.chars().next()` to check if a string starts with a char"
188 /// **What it does:** This lint checks for calls to `.or(foo(..))`, `.unwrap_or(foo(..))`, etc., and
189 /// suggests to use `or_else`, `unwrap_or_else`, etc., or `unwrap_or_default` instead.
191 /// **Why is this bad?** The function will always be called and potentially allocate an object
192 /// in expressions such as:
194 /// foo.unwrap_or(String::new())
196 /// this can instead be written:
198 /// foo.unwrap_or_else(String::new)
202 /// foo.unwrap_or_default()
205 /// **Known problems:** If the function as side-effects, not calling it will change the semantic of
206 /// the program, but you shouldn't rely on that anyway.
208 pub OR_FUN_CALL, Warn,
209 "using any `*or` method when the `*or_else` would do"
212 /// **What it does:** This lint checks for usage of `.extend(s)` on a `Vec` to extend the vector by a slice.
214 /// **Why is this bad?** Since Rust 1.6, the `extend_from_slice(_)` method is stable and at least for now faster.
216 /// **Known problems:** None.
218 /// **Example:** `my_vec.extend(&xs)`
220 pub EXTEND_FROM_SLICE, Warn,
221 "`.extend_from_slice(_)` is a faster way to extend a Vec by a slice"
224 /// **What it does:** This lint warns on using `.clone()` on a `Copy` type.
226 /// **Why is this bad?** The only reason `Copy` types implement `Clone` is for generics, not for
227 /// using the `clone` method on a concrete type.
229 /// **Known problems:** None.
231 /// **Example:** `42u64.clone()`
233 pub CLONE_ON_COPY, Warn, "using `clone` on a `Copy` type"
236 /// **What it does:** This lint warns on using `.clone()` on an `&&T`
238 /// **Why is this bad?** Cloning an `&&T` copies the inner `&T`, instead of cloning the underlying
241 /// **Known problems:** None.
248 /// let z = y.clone();
249 /// println!("{:p} {:p}",*y, z); // prints out the same pointer
253 pub CLONE_DOUBLE_REF, Warn, "using `clone` on `&&T`"
256 /// **What it does:** This lint warns about `new` not returning `Self`.
258 /// **Why is this bad?** As a convention, `new` methods are used to make a new instance of a type.
260 /// **Known problems:** None.
265 /// fn new(..) -> NotAFoo {
270 pub NEW_RET_NO_SELF, Warn, "not returning `Self` in a `new` method"
273 /// **What it does:** This lint checks for string methods that receive a single-character `str` as an argument, e.g. `_.split("x")`.
275 /// **Why is this bad?** Performing these methods using a `char` is faster than using a `str`.
277 /// **Known problems:** Does not catch multi-byte unicode characters.
279 /// **Example:** `_.split("x")` could be `_.split('x')`
281 pub SINGLE_CHAR_PATTERN,
283 "using a single-character str where a char could be used, e.g. \
287 /// **What it does:** This lint checks for getting the inner pointer of a temporary `CString`.
289 /// **Why is this bad?** The inner pointer of a `CString` is only valid as long as the `CString` is
292 /// **Known problems:** None.
296 /// let c_str = CString::new("foo").unwrap().as_ptr();
298 /// call_some_ffi_func(c_str);
301 /// Here `c_str` point to a freed address. The correct use would be:
303 /// let c_str = CString::new("foo").unwrap();
305 /// call_some_ffi_func(c_str.as_ptr());
309 pub TEMPORARY_CSTRING_AS_PTR,
311 "getting the inner pointer of a temporary `CString`"
314 impl LintPass for MethodsPass {
315 fn get_lints(&self) -> LintArray {
316 lint_array!(EXTEND_FROM_SLICE,
319 SHOULD_IMPLEMENT_TRAIT,
320 WRONG_SELF_CONVENTION,
321 WRONG_PUB_SELF_CONVENTION,
323 OPTION_MAP_UNWRAP_OR,
324 OPTION_MAP_UNWRAP_OR_ELSE,
332 TEMPORARY_CSTRING_AS_PTR)
336 impl LateLintPass for MethodsPass {
337 fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
338 if in_macro(cx, expr.span) {
343 ExprMethodCall(name, _, ref args) => {
345 if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
346 lint_unwrap(cx, expr, arglists[0]);
347 } else if let Some(arglists) = method_chain_args(expr, &["ok", "expect"]) {
348 lint_ok_expect(cx, expr, arglists[0]);
349 } else if let Some(arglists) = method_chain_args(expr, &["map", "unwrap_or"]) {
350 lint_map_unwrap_or(cx, expr, arglists[0], arglists[1]);
351 } else if let Some(arglists) = method_chain_args(expr, &["map", "unwrap_or_else"]) {
352 lint_map_unwrap_or_else(cx, expr, arglists[0], arglists[1]);
353 } else if let Some(arglists) = method_chain_args(expr, &["filter", "next"]) {
354 lint_filter_next(cx, expr, arglists[0]);
355 } else if let Some(arglists) = method_chain_args(expr, &["find", "is_some"]) {
356 lint_search_is_some(cx, expr, "find", arglists[0], arglists[1]);
357 } else if let Some(arglists) = method_chain_args(expr, &["position", "is_some"]) {
358 lint_search_is_some(cx, expr, "position", arglists[0], arglists[1]);
359 } else if let Some(arglists) = method_chain_args(expr, &["rposition", "is_some"]) {
360 lint_search_is_some(cx, expr, "rposition", arglists[0], arglists[1]);
361 } else if let Some(arglists) = method_chain_args(expr, &["extend"]) {
362 lint_extend(cx, expr, arglists[0]);
363 } else if let Some(arglists) = method_chain_args(expr, &["unwrap", "as_ptr"]) {
364 lint_cstring_as_ptr(cx, expr, &arglists[0][0], &arglists[1][0]);
367 lint_or_fun_call(cx, expr, &name.node.as_str(), args);
369 let self_ty = cx.tcx.expr_ty_adjusted(&args[0]);
370 if args.len() == 1 && name.node.as_str() == "clone" {
371 lint_clone_on_copy(cx, expr);
372 lint_clone_double_ref(cx, expr, &args[0], self_ty);
376 ty::TyRef(_, ty) if ty.ty.sty == ty::TyStr => {
377 for &(method, pos) in &PATTERN_METHODS {
378 if name.node.as_str() == method && args.len() > pos {
379 lint_single_char_pattern(cx, expr, &args[pos]);
386 ExprBinary(op, ref lhs, ref rhs) if op.node == BiEq || op.node == BiNe => {
387 if !lint_chars_next(cx, expr, lhs, rhs, op.node == BiEq) {
388 lint_chars_next(cx, expr, rhs, lhs, op.node == BiEq);
395 fn check_item(&mut self, cx: &LateContext, item: &Item) {
396 if in_external_macro(cx, item.span) {
400 if let ItemImpl(_, _, _, None, _, ref items) = item.node {
401 for implitem in items {
402 let name = implitem.name;
403 if let ImplItemKind::Method(ref sig, _) = implitem.node {
404 // check missing trait implementations
405 for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
408 name.as_str() == method_name,
409 sig.decl.inputs.len() == n_args,
410 out_type.matches(&sig.decl.output),
411 self_kind.matches(&sig.explicit_self.node, false)
413 span_lint(cx, SHOULD_IMPLEMENT_TRAIT, implitem.span, &format!(
414 "defining a method called `{}` on this type; consider implementing \
415 the `{}` trait or choosing a less ambiguous name", name, trait_name));
420 // check conventions w.r.t. conversion method names and predicates
421 let ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(item.id)).ty;
422 let is_copy = is_copy(cx, ty, item);
423 for &(ref conv, self_kinds) in &CONVENTIONS {
424 if conv.check(&name.as_str()) &&
425 !self_kinds.iter().any(|k| k.matches(&sig.explicit_self.node, is_copy)) {
426 let lint = if item.vis == Visibility::Public {
427 WRONG_PUB_SELF_CONVENTION
429 WRONG_SELF_CONVENTION
433 sig.explicit_self.span,
434 &format!("methods called `{}` usually take {}; consider choosing a less \
438 .map(|k| k.description())
444 let ret_ty = return_ty(cx, implitem.id);
445 if &name.as_str() == &"new" &&
446 !ret_ty.map_or(false, |ret_ty| ret_ty.walk().any(|t| same_tys(cx, t, ty, implitem.id))) {
449 sig.explicit_self.span,
450 "methods called `new` usually return `Self`");
458 /// Checks for the `OR_FUN_CALL` lint.
459 fn lint_or_fun_call(cx: &LateContext, expr: &Expr, name: &str, args: &[P<Expr>]) {
460 /// Check for `unwrap_or(T::new())` or `unwrap_or(T::default())`.
461 fn check_unwrap_or_default(cx: &LateContext, name: &str, fun: &Expr, self_expr: &Expr, arg: &Expr,
462 or_has_args: bool, span: Span)
468 if name == "unwrap_or" {
469 if let ExprPath(_, ref path) = fun.node {
470 let path: &str = &path.segments
472 .expect("A path must have at least one segment")
477 if ["default", "new"].contains(&path) {
478 let arg_ty = cx.tcx.expr_ty(arg);
479 let default_trait_id = if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT) {
485 if implements_trait(cx, arg_ty, default_trait_id, Vec::new()) {
489 &format!("use of `{}` followed by a call to `{}`", name, path))
490 .span_suggestion(span,
492 format!("{}.unwrap_or_default()", snippet(cx, self_expr.span, "_")));
502 /// Check for `*or(foo())`.
503 fn check_general_case(cx: &LateContext, name: &str, fun: &Expr, self_expr: &Expr, arg: &Expr, or_has_args: bool,
505 // (path, fn_has_argument, methods)
506 let know_types: &[(&[_], _, &[_], _)] = &[(&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
507 (&paths::HASHMAP_ENTRY, false, &["or_insert"], "with"),
510 &["map_or", "ok_or", "or", "unwrap_or"],
512 (&paths::RESULT, true, &["or", "unwrap_or"], "else")];
514 let self_ty = cx.tcx.expr_ty(self_expr);
516 let (fn_has_arguments, poss, suffix) = if let Some(&(_, fn_has_arguments, poss, suffix)) =
517 know_types.iter().find(|&&i| match_type(cx, self_ty, i.0)) {
518 (fn_has_arguments, poss, suffix)
523 if !poss.contains(&name) {
527 let sugg: Cow<_> = match (fn_has_arguments, !or_has_args) {
528 (true, _) => format!("|_| {}", snippet(cx, arg.span, "..")).into(),
529 (false, false) => format!("|| {}", snippet(cx, arg.span, "..")).into(),
530 (false, true) => snippet(cx, fun.span, ".."),
533 span_lint(cx, OR_FUN_CALL, span, &format!("use of `{}` followed by a function call", name))
534 .span_suggestion(span,
536 format!("{}.{}_{}({})", snippet(cx, self_expr.span, "_"), name, suffix, sugg));
540 if let ExprCall(ref fun, ref or_args) = args[1].node {
541 let or_has_args = !or_args.is_empty();
542 if !check_unwrap_or_default(cx, name, fun, &args[0], &args[1], or_has_args, expr.span) {
543 check_general_case(cx, name, fun, &args[0], &args[1], or_has_args, expr.span);
549 /// Checks for the `CLONE_ON_COPY` lint.
550 fn lint_clone_on_copy(cx: &LateContext, expr: &Expr) {
551 let ty = cx.tcx.expr_ty(expr);
552 let parent = cx.tcx.map.get_parent(expr.id);
553 let parameter_environment = ty::ParameterEnvironment::for_item(cx.tcx, parent);
555 if !ty.moves_by_default(¶meter_environment, expr.span) {
556 span_lint(cx, CLONE_ON_COPY, expr.span, "using `clone` on a `Copy` type");
560 /// Checks for the `CLONE_DOUBLE_REF` lint.
561 fn lint_clone_double_ref(cx: &LateContext, expr: &Expr, arg: &Expr, ty: ty::Ty) {
562 if let ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) = ty.sty {
563 if let ty::TyRef(..) = inner.sty {
564 let mut db = span_lint(cx,
567 "using `clone` on a double-reference; \
568 this will copy the reference instead of cloning \
570 if let Some(snip) = snippet_opt(cx, arg.span) {
571 db.span_suggestion(expr.span, "try dereferencing it", format!("(*{}).clone()", snip));
577 fn lint_extend(cx: &LateContext, expr: &Expr, args: &MethodArgs) {
578 let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&args[0]));
579 if !match_type(cx, obj_ty, &paths::VEC) {
582 let arg_ty = cx.tcx.expr_ty(&args[1]);
583 if let Some((span, r)) = derefs_to_slice(cx, &args[1], &arg_ty) {
584 span_lint(cx, EXTEND_FROM_SLICE, expr.span, "use of `extend` to extend a Vec by a slice")
585 .span_suggestion(expr.span,
587 format!("{}.extend_from_slice({}{})",
588 snippet(cx, args[0].span, "_"),
590 snippet(cx, span, "_")));
594 fn lint_cstring_as_ptr(cx: &LateContext, expr: &Expr, new: &Expr, unwrap: &Expr) {
596 let ExprCall(ref fun, ref args) = new.node,
598 let ExprPath(None, ref path) = fun.node,
599 match_path(path, &paths::CSTRING_NEW),
601 span_lint_and_then(cx, TEMPORARY_CSTRING_AS_PTR, expr.span,
602 "you are getting the inner pointer of a temporary `CString`",
604 db.note("that pointer will be invalid outside this expression");
605 db.span_help(unwrap.span, "assign the `CString` to a variable to extend its lifetime");
610 fn derefs_to_slice(cx: &LateContext, expr: &Expr, ty: &ty::Ty) -> Option<(Span, &'static str)> {
611 fn may_slice(cx: &LateContext, ty: &ty::Ty) -> bool {
613 ty::TySlice(_) => true,
614 ty::TyStruct(..) => match_type(cx, ty, &paths::VEC),
615 ty::TyArray(_, size) => size < 32,
616 ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) |
617 ty::TyBox(ref inner) => may_slice(cx, inner),
621 if let ExprMethodCall(name, _, ref args) = expr.node {
622 if &name.node.as_str() == &"iter" && may_slice(cx, &cx.tcx.expr_ty(&args[0])) {
623 Some((args[0].span, "&"))
629 ty::TySlice(_) => Some((expr.span, "")),
630 ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) |
631 ty::TyBox(ref inner) => {
632 if may_slice(cx, inner) {
633 Some((expr.span, ""))
644 // Type of MethodArgs is potentially a Vec
645 /// lint use of `unwrap()` for `Option`s and `Result`s
646 fn lint_unwrap(cx: &LateContext, expr: &Expr, unwrap_args: &MethodArgs) {
647 let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&unwrap_args[0]));
649 let mess = if match_type(cx, obj_ty, &paths::OPTION) {
650 Some((OPTION_UNWRAP_USED, "an Option", "None"))
651 } else if match_type(cx, obj_ty, &paths::RESULT) {
652 Some((RESULT_UNWRAP_USED, "a Result", "Err"))
657 if let Some((lint, kind, none_value)) = mess {
661 &format!("used unwrap() on {} value. If you don't want to handle the {} case gracefully, consider \
662 using expect() to provide a better panic
670 // Type of MethodArgs is potentially a Vec
671 /// lint use of `ok().expect()` for `Result`s
672 fn lint_ok_expect(cx: &LateContext, expr: &Expr, ok_args: &MethodArgs) {
673 // lint if the caller of `ok()` is a `Result`
674 if match_type(cx, cx.tcx.expr_ty(&ok_args[0]), &paths::RESULT) {
675 let result_type = cx.tcx.expr_ty(&ok_args[0]);
676 if let Some(error_type) = get_error_type(cx, result_type) {
677 if has_debug_impl(error_type, cx) {
681 "called `ok().expect()` on a Result value. You can call `expect` directly on the `Result`");
688 // Type of MethodArgs is potentially a Vec
689 /// lint use of `map().unwrap_or()` for `Option`s
690 fn lint_map_unwrap_or(cx: &LateContext, expr: &Expr, map_args: &MethodArgs, unwrap_args: &MethodArgs) {
691 // lint if the caller of `map()` is an `Option`
692 if match_type(cx, cx.tcx.expr_ty(&map_args[0]), &paths::OPTION) {
694 let msg = "called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling \
695 `map_or(a, f)` instead";
696 // get snippets for args to map() and unwrap_or()
697 let map_snippet = snippet(cx, map_args[1].span, "..");
698 let unwrap_snippet = snippet(cx, unwrap_args[1].span, "..");
699 // lint, with note if neither arg is > 1 line and both map() and
700 // unwrap_or() have the same span
701 let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
702 let same_span = map_args[1].span.expn_id == unwrap_args[1].span.expn_id;
703 if same_span && !multiline {
704 span_note_and_lint(cx,
705 OPTION_MAP_UNWRAP_OR,
709 &format!("replace `map({0}).unwrap_or({1})` with `map_or({1}, {0})`",
712 } else if same_span && multiline {
713 span_lint(cx, OPTION_MAP_UNWRAP_OR, expr.span, msg);
719 // Type of MethodArgs is potentially a Vec
720 /// lint use of `map().unwrap_or_else()` for `Option`s
721 fn lint_map_unwrap_or_else(cx: &LateContext, expr: &Expr, map_args: &MethodArgs, unwrap_args: &MethodArgs) {
722 // lint if the caller of `map()` is an `Option`
723 if match_type(cx, cx.tcx.expr_ty(&map_args[0]), &paths::OPTION) {
725 let msg = "called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling \
726 `map_or_else(g, f)` instead";
727 // get snippets for args to map() and unwrap_or_else()
728 let map_snippet = snippet(cx, map_args[1].span, "..");
729 let unwrap_snippet = snippet(cx, unwrap_args[1].span, "..");
730 // lint, with note if neither arg is > 1 line and both map() and
731 // unwrap_or_else() have the same span
732 let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
733 let same_span = map_args[1].span.expn_id == unwrap_args[1].span.expn_id;
734 if same_span && !multiline {
735 span_note_and_lint(cx,
736 OPTION_MAP_UNWRAP_OR_ELSE,
740 &format!("replace `map({0}).unwrap_or_else({1})` with `with map_or_else({1}, {0})`",
743 } else if same_span && multiline {
744 span_lint(cx, OPTION_MAP_UNWRAP_OR_ELSE, expr.span, msg);
750 // Type of MethodArgs is potentially a Vec
751 /// lint use of `filter().next() for Iterators`
752 fn lint_filter_next(cx: &LateContext, expr: &Expr, filter_args: &MethodArgs) {
753 // lint if caller of `.filter().next()` is an Iterator
754 if match_trait_method(cx, expr, &paths::ITERATOR) {
755 let msg = "called `filter(p).next()` on an Iterator. This is more succinctly expressed by calling `.find(p)` \
757 let filter_snippet = snippet(cx, filter_args[1].span, "..");
758 if filter_snippet.lines().count() <= 1 {
759 // add note if not multi-line
760 span_note_and_lint(cx,
765 &format!("replace `filter({0}).next()` with `find({0})`", filter_snippet));
767 span_lint(cx, FILTER_NEXT, expr.span, msg);
773 // Type of MethodArgs is potentially a Vec
774 /// lint searching an Iterator followed by `is_some()`
775 fn lint_search_is_some(cx: &LateContext, expr: &Expr, search_method: &str, search_args: &MethodArgs,
776 is_some_args: &MethodArgs) {
777 // lint if caller of search is an Iterator
778 if match_trait_method(cx, &*is_some_args[0], &paths::ITERATOR) {
779 let msg = format!("called `is_some()` after searching an iterator with {}. This is more succinctly expressed \
780 by calling `any()`.",
782 let search_snippet = snippet(cx, search_args[1].span, "..");
783 if search_snippet.lines().count() <= 1 {
784 // add note if not multi-line
785 span_note_and_lint(cx,
790 &format!("replace `{0}({1}).is_some()` with `any({1})`", search_method, search_snippet));
792 span_lint(cx, SEARCH_IS_SOME, expr.span, &msg);
797 /// Checks for the `CHARS_NEXT_CMP` lint.
798 fn lint_chars_next(cx: &LateContext, expr: &Expr, chain: &Expr, other: &Expr, eq: bool) -> bool {
800 let Some(args) = method_chain_args(chain, &["chars", "next"]),
801 let ExprCall(ref fun, ref arg_char) = other.node,
803 let ExprPath(None, ref path) = fun.node,
804 path.segments.len() == 1 && path.segments[0].identifier.name.as_str() == "Some"
806 let self_ty = walk_ptrs_ty(cx.tcx.expr_ty_adjusted(&args[0][0]));
808 if self_ty.sty != ty::TyStr {
812 span_lint_and_then(cx,
815 "you should use the `starts_with` method",
817 let sugg = format!("{}{}.starts_with({})",
818 if eq { "" } else { "!" },
819 snippet(cx, args[0][0].span, "_"),
820 snippet(cx, arg_char[0].span, "_")
823 db.span_suggestion(expr.span, "like this", sugg);
832 /// lint for length-1 `str`s for methods in `PATTERN_METHODS`
833 fn lint_single_char_pattern(cx: &LateContext, expr: &Expr, arg: &Expr) {
834 if let Ok(ConstVal::Str(r)) = eval_const_expr_partial(cx.tcx, arg, ExprTypeChecked, None) {
836 let hint = snippet(cx, expr.span, "..").replace(&format!("\"{}\"", r), &format!("'{}'", r));
837 span_lint_and_then(cx,
840 "single-character string constant used as pattern",
842 db.span_suggestion(expr.span, "try using a char instead:", hint);
848 /// Given a `Result<T, E>` type, return its error type (`E`).
849 fn get_error_type<'a>(cx: &LateContext, ty: ty::Ty<'a>) -> Option<ty::Ty<'a>> {
850 if !match_type(cx, ty, &paths::RESULT) {
853 if let ty::TyEnum(_, substs) = ty.sty {
854 if let Some(err_ty) = substs.types.opt_get(TypeSpace, 1) {
861 /// This checks whether a given type is known to implement Debug.
862 fn has_debug_impl<'a, 'b>(ty: ty::Ty<'a>, cx: &LateContext<'b, 'a>) -> bool {
863 match cx.tcx.lang_items.debug_trait() {
864 Some(debug) => implements_trait(cx, ty, debug, Vec::new()),
871 StartsWith(&'static str),
874 #[cfg_attr(rustfmt, rustfmt_skip)]
875 const CONVENTIONS: [(Convention, &'static [SelfKind]); 6] = [
876 (Convention::Eq("new"), &[SelfKind::No]),
877 (Convention::StartsWith("as_"), &[SelfKind::Ref, SelfKind::RefMut]),
878 (Convention::StartsWith("from_"), &[SelfKind::No]),
879 (Convention::StartsWith("into_"), &[SelfKind::Value]),
880 (Convention::StartsWith("is_"), &[SelfKind::Ref, SelfKind::No]),
881 (Convention::StartsWith("to_"), &[SelfKind::Ref]),
884 #[cfg_attr(rustfmt, rustfmt_skip)]
885 const TRAIT_METHODS: [(&'static str, usize, SelfKind, OutType, &'static str); 30] = [
886 ("add", 2, SelfKind::Value, OutType::Any, "std::ops::Add"),
887 ("as_mut", 1, SelfKind::RefMut, OutType::Ref, "std::convert::AsMut"),
888 ("as_ref", 1, SelfKind::Ref, OutType::Ref, "std::convert::AsRef"),
889 ("bitand", 2, SelfKind::Value, OutType::Any, "std::ops::BitAnd"),
890 ("bitor", 2, SelfKind::Value, OutType::Any, "std::ops::BitOr"),
891 ("bitxor", 2, SelfKind::Value, OutType::Any, "std::ops::BitXor"),
892 ("borrow", 1, SelfKind::Ref, OutType::Ref, "std::borrow::Borrow"),
893 ("borrow_mut", 1, SelfKind::RefMut, OutType::Ref, "std::borrow::BorrowMut"),
894 ("clone", 1, SelfKind::Ref, OutType::Any, "std::clone::Clone"),
895 ("cmp", 2, SelfKind::Ref, OutType::Any, "std::cmp::Ord"),
896 ("default", 0, SelfKind::No, OutType::Any, "std::default::Default"),
897 ("deref", 1, SelfKind::Ref, OutType::Ref, "std::ops::Deref"),
898 ("deref_mut", 1, SelfKind::RefMut, OutType::Ref, "std::ops::DerefMut"),
899 ("div", 2, SelfKind::Value, OutType::Any, "std::ops::Div"),
900 ("drop", 1, SelfKind::RefMut, OutType::Unit, "std::ops::Drop"),
901 ("eq", 2, SelfKind::Ref, OutType::Bool, "std::cmp::PartialEq"),
902 ("from_iter", 1, SelfKind::No, OutType::Any, "std::iter::FromIterator"),
903 ("from_str", 1, SelfKind::No, OutType::Any, "std::str::FromStr"),
904 ("hash", 2, SelfKind::Ref, OutType::Unit, "std::hash::Hash"),
905 ("index", 2, SelfKind::Ref, OutType::Ref, "std::ops::Index"),
906 ("index_mut", 2, SelfKind::RefMut, OutType::Ref, "std::ops::IndexMut"),
907 ("into_iter", 1, SelfKind::Value, OutType::Any, "std::iter::IntoIterator"),
908 ("mul", 2, SelfKind::Value, OutType::Any, "std::ops::Mul"),
909 ("neg", 1, SelfKind::Value, OutType::Any, "std::ops::Neg"),
910 ("next", 1, SelfKind::RefMut, OutType::Any, "std::iter::Iterator"),
911 ("not", 1, SelfKind::Value, OutType::Any, "std::ops::Not"),
912 ("rem", 2, SelfKind::Value, OutType::Any, "std::ops::Rem"),
913 ("shl", 2, SelfKind::Value, OutType::Any, "std::ops::Shl"),
914 ("shr", 2, SelfKind::Value, OutType::Any, "std::ops::Shr"),
915 ("sub", 2, SelfKind::Value, OutType::Any, "std::ops::Sub"),
918 #[cfg_attr(rustfmt, rustfmt_skip)]
919 const PATTERN_METHODS: [(&'static str, usize); 17] = [
927 ("split_terminator", 1),
928 ("rsplit_terminator", 1),
933 ("match_indices", 1),
934 ("rmatch_indices", 1),
935 ("trim_left_matches", 1),
936 ("trim_right_matches", 1),
940 #[derive(Clone, Copy)]
949 fn matches(&self, slf: &ExplicitSelf_, allow_value_for_ref: bool) -> bool {
951 (&SelfKind::Value, &SelfValue(_)) |
952 (&SelfKind::Ref, &SelfRegion(_, Mutability::MutImmutable, _)) |
953 (&SelfKind::RefMut, &SelfRegion(_, Mutability::MutMutable, _)) |
954 (&SelfKind::No, &SelfStatic) => true,
955 (&SelfKind::Ref, &SelfValue(_)) |
956 (&SelfKind::RefMut, &SelfValue(_)) => allow_value_for_ref,
957 (_, &SelfExplicit(ref ty, _)) => self.matches_explicit_type(ty, allow_value_for_ref),
962 fn matches_explicit_type(&self, ty: &Ty, allow_value_for_ref: bool) -> bool {
963 match (self, &ty.node) {
964 (&SelfKind::Value, &TyPath(..)) |
965 (&SelfKind::Ref, &TyRptr(_, MutTy { mutbl: Mutability::MutImmutable, .. })) |
966 (&SelfKind::RefMut, &TyRptr(_, MutTy { mutbl: Mutability::MutMutable, .. })) => true,
967 (&SelfKind::Ref, &TyPath(..)) |
968 (&SelfKind::RefMut, &TyPath(..)) => allow_value_for_ref,
973 fn description(&self) -> &'static str {
975 SelfKind::Value => "self by value",
976 SelfKind::Ref => "self by reference",
977 SelfKind::RefMut => "self by mutable reference",
978 SelfKind::No => "no self",
984 fn check(&self, other: &str) -> bool {
986 Convention::Eq(this) => this == other,
987 Convention::StartsWith(this) => other.starts_with(this),
992 impl fmt::Display for Convention {
993 fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
995 Convention::Eq(this) => this.fmt(f),
996 Convention::StartsWith(this) => this.fmt(f).and_then(|_| '*'.fmt(f)),
1001 #[derive(Clone, Copy)]
1010 fn matches(&self, ty: &FunctionRetTy) -> bool {
1012 (&OutType::Unit, &DefaultReturn(_)) => true,
1013 (&OutType::Unit, &Return(ref ty)) if ty.node == TyTup(vec![].into()) => true,
1014 (&OutType::Bool, &Return(ref ty)) if is_bool(ty) => true,
1015 (&OutType::Any, &Return(ref ty)) if ty.node != TyTup(vec![].into()) => true,
1016 (&OutType::Ref, &Return(ref ty)) => {
1017 if let TyRptr(_, _) = ty.node {
1028 fn is_bool(ty: &Ty) -> bool {
1029 if let TyPath(None, ref p) = ty.node {
1030 if match_path(p, &["bool"]) {
1037 fn is_copy<'a, 'ctx>(cx: &LateContext<'a, 'ctx>, ty: ty::Ty<'ctx>, item: &Item) -> bool {
1038 let env = ty::ParameterEnvironment::for_item(cx.tcx, item.id);
1039 !ty.subst(cx.tcx, &env.free_substs).moves_by_default(&env, item.span)