1 mod bind_instead_of_map;
2 mod inefficient_to_string;
3 mod manual_saturating_arithmetic;
4 mod option_map_unwrap_or;
5 mod unnecessary_filter_map;
6 mod unnecessary_lazy_eval;
12 use bind_instead_of_map::BindInsteadOfMap;
13 use if_chain::if_chain;
15 use rustc_errors::Applicability;
17 use rustc_hir::intravisit::{self, Visitor};
18 use rustc_hir::{TraitItem, TraitItemKind};
19 use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
20 use rustc_middle::hir::map::Map;
21 use rustc_middle::lint::in_external_macro;
22 use rustc_middle::ty::{self, TraitRef, Ty, TyS};
23 use rustc_session::{declare_lint_pass, declare_tool_lint};
24 use rustc_span::source_map::Span;
25 use rustc_span::symbol::{sym, SymbolStr};
27 use crate::consts::{constant, Constant};
28 use crate::utils::eager_or_lazy::is_lazyness_candidate;
29 use crate::utils::usage::mutated_variables;
31 contains_ty, get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro,
32 is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath,
33 match_trait_method, match_type, match_var, method_calls, method_chain_args, paths, remove_blocks, return_ty,
34 single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
35 span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, walk_ptrs_ty_depth, SpanlessEq,
38 declare_clippy_lint! {
39 /// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s.
41 /// **Why is this bad?** It is better to handle the `None` or `Err` case,
42 /// or at least call `.expect(_)` with a more helpful message. Still, for a lot of
43 /// quick-and-dirty code, `unwrap` is a good choice, which is why this lint is
44 /// `Allow` by default.
46 /// `result.unwrap()` will let the thread panic on `Err` values.
47 /// Normally, you want to implement more sophisticated error handling,
48 /// and propagate errors upwards with `?` operator.
50 /// Even if you want to panic on errors, not all `Error`s implement good
51 /// messages on display. Therefore, it may be beneficial to look at the places
52 /// where they may get displayed. Activate this lint to do just that.
54 /// **Known problems:** None.
58 /// # let opt = Some(1);
64 /// opt.expect("more helpful message");
70 /// # let res: Result<usize, ()> = Ok(1);
76 /// res.expect("more helpful message");
80 "using `.unwrap()` on `Result` or `Option`, which should at least get a better message using `expect()`"
83 declare_clippy_lint! {
84 /// **What it does:** Checks for `.expect()` calls on `Option`s and `Result`s.
86 /// **Why is this bad?** Usually it is better to handle the `None` or `Err` case.
87 /// Still, for a lot of quick-and-dirty code, `expect` is a good choice, which is why
88 /// this lint is `Allow` by default.
90 /// `result.expect()` will let the thread panic on `Err`
91 /// values. Normally, you want to implement more sophisticated error handling,
92 /// and propagate errors upwards with `?` operator.
94 /// **Known problems:** None.
98 /// # let opt = Some(1);
101 /// opt.expect("one");
104 /// let opt = Some(1);
111 /// # let res: Result<usize, ()> = Ok(1);
114 /// res.expect("one");
118 /// # Ok::<(), ()>(())
122 "using `.expect()` on `Result` or `Option`, which might be better handled"
125 declare_clippy_lint! {
126 /// **What it does:** Checks for methods that should live in a trait
127 /// implementation of a `std` trait (see [llogiq's blog
128 /// post](http://llogiq.github.io/2015/07/30/traits.html) for further
129 /// information) instead of an inherent implementation.
131 /// **Why is this bad?** Implementing the traits improve ergonomics for users of
132 /// the code, often with very little cost. Also people seeing a `mul(...)`
134 /// may expect `*` to work equally, so you should have good reason to disappoint
137 /// **Known problems:** None.
143 /// fn add(&self, other: &X) -> X {
149 pub SHOULD_IMPLEMENT_TRAIT,
151 "defining a method that should be implementing a std trait"
154 declare_clippy_lint! {
155 /// **What it does:** Checks for methods with certain name prefixes and which
156 /// doesn't match how self is taken. The actual rules are:
158 /// |Prefix |`self` taken |
159 /// |-------|----------------------|
160 /// |`as_` |`&self` or `&mut self`|
162 /// |`into_`|`self` |
163 /// |`is_` |`&self` or none |
164 /// |`to_` |`&self` |
166 /// **Why is this bad?** Consistency breeds readability. If you follow the
167 /// conventions, your users won't be surprised that they, e.g., need to supply a
168 /// mutable reference to a `as_..` function.
170 /// **Known problems:** None.
176 /// fn as_str(self) -> &'static str {
182 pub WRONG_SELF_CONVENTION,
184 "defining a method named with an established prefix (like \"into_\") that takes `self` with the wrong convention"
187 declare_clippy_lint! {
188 /// **What it does:** This is the same as
189 /// [`wrong_self_convention`](#wrong_self_convention), but for public items.
191 /// **Why is this bad?** See [`wrong_self_convention`](#wrong_self_convention).
193 /// **Known problems:** Actually *renaming* the function may break clients if
194 /// the function is part of the public interface. In that case, be mindful of
195 /// the stability guarantees you've given your users.
201 /// pub fn as_str(self) -> &'a str {
206 pub WRONG_PUB_SELF_CONVENTION,
208 "defining a public method named with an established prefix (like \"into_\") that takes `self` with the wrong convention"
211 declare_clippy_lint! {
212 /// **What it does:** Checks for usage of `ok().expect(..)`.
214 /// **Why is this bad?** Because you usually call `expect()` on the `Result`
215 /// directly to get a better error message.
217 /// **Known problems:** The error type needs to implement `Debug`
221 /// # let x = Ok::<_, ()>(());
224 /// x.ok().expect("why did I do this again?");
227 /// x.expect("why did I do this again?");
231 "using `ok().expect()`, which gives worse error messages than calling `expect` directly on the Result"
234 declare_clippy_lint! {
235 /// **What it does:** Checks for usage of `option.map(_).unwrap_or(_)` or `option.map(_).unwrap_or_else(_)` or
236 /// `result.map(_).unwrap_or_else(_)`.
238 /// **Why is this bad?** Readability, these can be written more concisely (resp.) as
239 /// `option.map_or(_, _)`, `option.map_or_else(_, _)` and `result.map_or_else(_, _)`.
241 /// **Known problems:** The order of the arguments is not in execution order
245 /// # let x = Some(1);
248 /// x.map(|a| a + 1).unwrap_or(0);
251 /// x.map_or(0, |a| a + 1);
257 /// # let x: Result<usize, ()> = Ok(1);
258 /// # fn some_function(foo: ()) -> usize { 1 }
261 /// x.map(|a| a + 1).unwrap_or_else(some_function);
264 /// x.map_or_else(some_function, |a| a + 1);
268 "using `.map(f).unwrap_or(a)` or `.map(f).unwrap_or_else(func)`, which are more succinctly expressed as `map_or(a, f)` or `map_or_else(a, f)`"
271 declare_clippy_lint! {
272 /// **What it does:** Checks for usage of `_.map_or(None, _)`.
274 /// **Why is this bad?** Readability, this can be written more concisely as
277 /// **Known problems:** The order of the arguments is not in execution order.
281 /// # let opt = Some(1);
284 /// opt.map_or(None, |a| Some(a + 1));
287 /// opt.and_then(|a| Some(a + 1));
289 pub OPTION_MAP_OR_NONE,
291 "using `Option.map_or(None, f)`, which is more succinctly expressed as `and_then(f)`"
294 declare_clippy_lint! {
295 /// **What it does:** Checks for usage of `_.map_or(None, Some)`.
297 /// **Why is this bad?** Readability, this can be written more concisely as
300 /// **Known problems:** None.
306 /// # let r: Result<u32, &str> = Ok(1);
307 /// assert_eq!(Some(1), r.map_or(None, Some));
312 /// # let r: Result<u32, &str> = Ok(1);
313 /// assert_eq!(Some(1), r.ok());
315 pub RESULT_MAP_OR_INTO_OPTION,
317 "using `Result.map_or(None, Some)`, which is more succinctly expressed as `ok()`"
320 declare_clippy_lint! {
321 /// **What it does:** Checks for usage of `_.and_then(|x| Some(y))`, `_.and_then(|x| Ok(y))` or
322 /// `_.or_else(|x| Err(y))`.
324 /// **Why is this bad?** Readability, this can be written more concisely as
325 /// `_.map(|x| y)` or `_.map_err(|x| y)`.
327 /// **Known problems:** None
332 /// # fn opt() -> Option<&'static str> { Some("42") }
333 /// # fn res() -> Result<&'static str, &'static str> { Ok("42") }
334 /// let _ = opt().and_then(|s| Some(s.len()));
335 /// let _ = res().and_then(|s| if s.len() == 42 { Ok(10) } else { Ok(20) });
336 /// let _ = res().or_else(|s| if s.len() == 42 { Err(10) } else { Err(20) });
339 /// The correct use would be:
342 /// # fn opt() -> Option<&'static str> { Some("42") }
343 /// # fn res() -> Result<&'static str, &'static str> { Ok("42") }
344 /// let _ = opt().map(|s| s.len());
345 /// let _ = res().map(|s| if s.len() == 42 { 10 } else { 20 });
346 /// let _ = res().map_err(|s| if s.len() == 42 { 10 } else { 20 });
348 pub BIND_INSTEAD_OF_MAP,
350 "using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`"
353 declare_clippy_lint! {
354 /// **What it does:** Checks for usage of `_.filter(_).next()`.
356 /// **Why is this bad?** Readability, this can be written more concisely as
359 /// **Known problems:** None.
363 /// # let vec = vec![1];
364 /// vec.iter().filter(|x| **x == 0).next();
366 /// Could be written as
368 /// # let vec = vec![1];
369 /// vec.iter().find(|x| **x == 0);
373 "using `filter(p).next()`, which is more succinctly expressed as `.find(p)`"
376 declare_clippy_lint! {
377 /// **What it does:** Checks for usage of `_.skip_while(condition).next()`.
379 /// **Why is this bad?** Readability, this can be written more concisely as
380 /// `_.find(!condition)`.
382 /// **Known problems:** None.
386 /// # let vec = vec![1];
387 /// vec.iter().skip_while(|x| **x == 0).next();
389 /// Could be written as
391 /// # let vec = vec![1];
392 /// vec.iter().find(|x| **x != 0);
396 "using `skip_while(p).next()`, which is more succinctly expressed as `.find(!p)`"
399 declare_clippy_lint! {
400 /// **What it does:** Checks for usage of `_.map(_).flatten(_)`,
402 /// **Why is this bad?** Readability, this can be written more concisely as
405 /// **Known problems:**
409 /// let vec = vec![vec![1]];
412 /// vec.iter().map(|x| x.iter()).flatten();
415 /// vec.iter().flat_map(|x| x.iter());
419 "using combinations of `flatten` and `map` which can usually be written as a single method call"
422 declare_clippy_lint! {
423 /// **What it does:** Checks for usage of `_.filter(_).map(_)`,
424 /// `_.filter(_).flat_map(_)`, `_.filter_map(_).flat_map(_)` and similar.
426 /// **Why is this bad?** Readability, this can be written more concisely as
427 /// `_.filter_map(_)`.
429 /// **Known problems:** Often requires a condition + Option/Iterator creation
430 /// inside the closure.
434 /// let vec = vec![1];
437 /// vec.iter().filter(|x| **x == 0).map(|x| *x * 2);
440 /// vec.iter().filter_map(|x| if *x == 0 {
448 "using combinations of `filter`, `map`, `filter_map` and `flat_map` which can usually be written as a single method call"
451 declare_clippy_lint! {
452 /// **What it does:** Checks for usage of `_.filter_map(_).next()`.
454 /// **Why is this bad?** Readability, this can be written more concisely as
457 /// **Known problems:** None
461 /// (0..3).filter_map(|x| if x == 2 { Some(x) } else { None }).next();
463 /// Can be written as
466 /// (0..3).find_map(|x| if x == 2 { Some(x) } else { None });
470 "using combination of `filter_map` and `next` which can usually be written as a single method call"
473 declare_clippy_lint! {
474 /// **What it does:** Checks for usage of `flat_map(|x| x)`.
476 /// **Why is this bad?** Readability, this can be written more concisely by using `flatten`.
478 /// **Known problems:** None
482 /// # let iter = vec![vec![0]].into_iter();
483 /// iter.flat_map(|x| x);
485 /// Can be written as
487 /// # let iter = vec![vec![0]].into_iter();
490 pub FLAT_MAP_IDENTITY,
492 "call to `flat_map` where `flatten` is sufficient"
495 declare_clippy_lint! {
496 /// **What it does:** Checks for usage of `_.find(_).map(_)`.
498 /// **Why is this bad?** Readability, this can be written more concisely as
501 /// **Known problems:** Often requires a condition + Option/Iterator creation
502 /// inside the closure.
506 /// (0..3).find(|x| *x == 2).map(|x| x * 2);
508 /// Can be written as
510 /// (0..3).find_map(|x| if x == 2 { Some(x * 2) } else { None });
514 "using a combination of `find` and `map` can usually be written as a single method call"
517 declare_clippy_lint! {
518 /// **What it does:** Checks for an iterator or string search (such as `find()`,
519 /// `position()`, or `rposition()`) followed by a call to `is_some()`.
521 /// **Why is this bad?** Readability, this can be written more concisely as
522 /// `_.any(_)` or `_.contains(_)`.
524 /// **Known problems:** None.
528 /// # let vec = vec![1];
529 /// vec.iter().find(|x| **x == 0).is_some();
531 /// Could be written as
533 /// # let vec = vec![1];
534 /// vec.iter().any(|x| *x == 0);
538 "using an iterator or string search followed by `is_some()`, which is more succinctly expressed as a call to `any()` or `contains()`"
541 declare_clippy_lint! {
542 /// **What it does:** Checks for usage of `.chars().next()` on a `str` to check
543 /// if it starts with a given char.
545 /// **Why is this bad?** Readability, this can be written more concisely as
546 /// `_.starts_with(_)`.
548 /// **Known problems:** None.
552 /// let name = "foo";
553 /// if name.chars().next() == Some('_') {};
555 /// Could be written as
557 /// let name = "foo";
558 /// if name.starts_with('_') {};
562 "using `.chars().next()` to check if a string starts with a char"
565 declare_clippy_lint! {
566 /// **What it does:** Checks for calls to `.or(foo(..))`, `.unwrap_or(foo(..))`,
567 /// etc., and suggests to use `or_else`, `unwrap_or_else`, etc., or
568 /// `unwrap_or_default` instead.
570 /// **Why is this bad?** The function will always be called and potentially
571 /// allocate an object acting as the default.
573 /// **Known problems:** If the function has side-effects, not calling it will
574 /// change the semantic of the program, but you shouldn't rely on that anyway.
578 /// # let foo = Some(String::new());
579 /// foo.unwrap_or(String::new());
581 /// this can instead be written:
583 /// # let foo = Some(String::new());
584 /// foo.unwrap_or_else(String::new);
588 /// # let foo = Some(String::new());
589 /// foo.unwrap_or_default();
593 "using any `*or` method with a function call, which suggests `*or_else`"
596 declare_clippy_lint! {
597 /// **What it does:** Checks for calls to `.expect(&format!(...))`, `.expect(foo(..))`,
598 /// etc., and suggests to use `unwrap_or_else` instead
600 /// **Why is this bad?** The function will always be called.
602 /// **Known problems:** If the function has side-effects, not calling it will
603 /// change the semantics of the program, but you shouldn't rely on that anyway.
607 /// # let foo = Some(String::new());
608 /// # let err_code = "418";
609 /// # let err_msg = "I'm a teapot";
610 /// foo.expect(&format!("Err {}: {}", err_code, err_msg));
614 /// # let foo = Some(String::new());
615 /// # let err_code = "418";
616 /// # let err_msg = "I'm a teapot";
617 /// foo.expect(format!("Err {}: {}", err_code, err_msg).as_str());
619 /// this can instead be written:
621 /// # let foo = Some(String::new());
622 /// # let err_code = "418";
623 /// # let err_msg = "I'm a teapot";
624 /// foo.unwrap_or_else(|| panic!("Err {}: {}", err_code, err_msg));
628 "using any `expect` method with a function call"
631 declare_clippy_lint! {
632 /// **What it does:** Checks for usage of `.clone()` on a `Copy` type.
634 /// **Why is this bad?** The only reason `Copy` types implement `Clone` is for
635 /// generics, not for using the `clone` method on a concrete type.
637 /// **Known problems:** None.
645 "using `clone` on a `Copy` type"
648 declare_clippy_lint! {
649 /// **What it does:** Checks for usage of `.clone()` on a ref-counted pointer,
650 /// (`Rc`, `Arc`, `rc::Weak`, or `sync::Weak`), and suggests calling Clone via unified
651 /// function syntax instead (e.g., `Rc::clone(foo)`).
653 /// **Why is this bad?** Calling '.clone()' on an Rc, Arc, or Weak
654 /// can obscure the fact that only the pointer is being cloned, not the underlying
659 /// # use std::rc::Rc;
660 /// let x = Rc::new(1);
668 pub CLONE_ON_REF_PTR,
670 "using 'clone' on a ref-counted pointer"
673 declare_clippy_lint! {
674 /// **What it does:** Checks for usage of `.clone()` on an `&&T`.
676 /// **Why is this bad?** Cloning an `&&T` copies the inner `&T`, instead of
677 /// cloning the underlying `T`.
679 /// **Known problems:** None.
686 /// let z = y.clone();
687 /// println!("{:p} {:p}", *y, z); // prints out the same pointer
690 pub CLONE_DOUBLE_REF,
692 "using `clone` on `&&T`"
695 declare_clippy_lint! {
696 /// **What it does:** Checks for usage of `.to_string()` on an `&&T` where
697 /// `T` implements `ToString` directly (like `&&str` or `&&String`).
699 /// **Why is this bad?** This bypasses the specialized implementation of
700 /// `ToString` and instead goes through the more expensive string formatting
703 /// **Known problems:** None.
707 /// // Generic implementation for `T: Display` is used (slow)
708 /// ["foo", "bar"].iter().map(|s| s.to_string());
710 /// // OK, the specialized impl is used
711 /// ["foo", "bar"].iter().map(|&s| s.to_string());
713 pub INEFFICIENT_TO_STRING,
715 "using `to_string` on `&&T` where `T: ToString`"
718 declare_clippy_lint! {
719 /// **What it does:** Checks for `new` not returning a type that contains `Self`.
721 /// **Why is this bad?** As a convention, `new` methods are used to make a new
722 /// instance of a type.
724 /// **Known problems:** None.
727 /// In an impl block:
730 /// # struct NotAFoo;
732 /// fn new() -> NotAFoo {
742 /// // Bad. The type name must contain `Self`
743 /// fn new() -> Bar {
751 /// # struct FooError;
753 /// // Good. Return type contains `Self`
754 /// fn new() -> Result<Foo, FooError> {
760 /// Or in a trait definition:
762 /// pub trait Trait {
763 /// // Bad. The type name must contain `Self`
769 /// pub trait Trait {
770 /// // Good. Return type contains `Self`
771 /// fn new() -> Self;
776 "not returning type containing `Self` in a `new` method"
779 declare_clippy_lint! {
780 /// **What it does:** Checks for string methods that receive a single-character
781 /// `str` as an argument, e.g., `_.split("x")`.
783 /// **Why is this bad?** Performing these methods using a `char` is faster than
786 /// **Known problems:** Does not catch multi-byte unicode characters.
795 pub SINGLE_CHAR_PATTERN,
797 "using a single-character str where a char could be used, e.g., `_.split(\"x\")`"
800 declare_clippy_lint! {
801 /// **What it does:** Checks for calling `.step_by(0)` on iterators which panics.
803 /// **Why is this bad?** This very much looks like an oversight. Use `panic!()` instead if you
804 /// actually intend to panic.
806 /// **Known problems:** None.
809 /// ```rust,should_panic
810 /// for x in (0..100).step_by(0) {
814 pub ITERATOR_STEP_BY_ZERO,
816 "using `Iterator::step_by(0)`, which will panic at runtime"
819 declare_clippy_lint! {
820 /// **What it does:** Checks for the use of `iter.nth(0)`.
822 /// **Why is this bad?** `iter.next()` is equivalent to
823 /// `iter.nth(0)`, as they both consume the next element,
824 /// but is more readable.
826 /// **Known problems:** None.
831 /// # use std::collections::HashSet;
833 /// # let mut s = HashSet::new();
835 /// let x = s.iter().nth(0);
838 /// # let mut s = HashSet::new();
840 /// let x = s.iter().next();
844 "replace `iter.nth(0)` with `iter.next()`"
847 declare_clippy_lint! {
848 /// **What it does:** Checks for use of `.iter().nth()` (and the related
849 /// `.iter_mut().nth()`) on standard library types with O(1) element access.
851 /// **Why is this bad?** `.get()` and `.get_mut()` are more efficient and more
854 /// **Known problems:** None.
858 /// let some_vec = vec![0, 1, 2, 3];
859 /// let bad_vec = some_vec.iter().nth(3);
860 /// let bad_slice = &some_vec[..].iter().nth(3);
862 /// The correct use would be:
864 /// let some_vec = vec![0, 1, 2, 3];
865 /// let bad_vec = some_vec.get(3);
866 /// let bad_slice = &some_vec[..].get(3);
870 "using `.iter().nth()` on a standard library type with O(1) element access"
873 declare_clippy_lint! {
874 /// **What it does:** Checks for use of `.skip(x).next()` on iterators.
876 /// **Why is this bad?** `.nth(x)` is cleaner
878 /// **Known problems:** None.
882 /// let some_vec = vec![0, 1, 2, 3];
883 /// let bad_vec = some_vec.iter().skip(3).next();
884 /// let bad_slice = &some_vec[..].iter().skip(3).next();
886 /// The correct use would be:
888 /// let some_vec = vec![0, 1, 2, 3];
889 /// let bad_vec = some_vec.iter().nth(3);
890 /// let bad_slice = &some_vec[..].iter().nth(3);
894 "using `.skip(x).next()` on an iterator"
897 declare_clippy_lint! {
898 /// **What it does:** Checks for use of `.get().unwrap()` (or
899 /// `.get_mut().unwrap`) on a standard library type which implements `Index`
901 /// **Why is this bad?** Using the Index trait (`[]`) is more clear and more
904 /// **Known problems:** Not a replacement for error handling: Using either
905 /// `.unwrap()` or the Index trait (`[]`) carries the risk of causing a `panic`
906 /// if the value being accessed is `None`. If the use of `.get().unwrap()` is a
907 /// temporary placeholder for dealing with the `Option` type, then this does
908 /// not mitigate the need for error handling. If there is a chance that `.get()`
909 /// will be `None` in your program, then it is advisable that the `None` case
910 /// is handled in a future refactor instead of using `.unwrap()` or the Index
915 /// let mut some_vec = vec![0, 1, 2, 3];
916 /// let last = some_vec.get(3).unwrap();
917 /// *some_vec.get_mut(0).unwrap() = 1;
919 /// The correct use would be:
921 /// let mut some_vec = vec![0, 1, 2, 3];
922 /// let last = some_vec[3];
927 "using `.get().unwrap()` or `.get_mut().unwrap()` when using `[]` would work instead"
930 declare_clippy_lint! {
931 /// **What it does:** Checks for the use of `.extend(s.chars())` where s is a
932 /// `&str` or `String`.
934 /// **Why is this bad?** `.push_str(s)` is clearer
936 /// **Known problems:** None.
941 /// let def = String::from("def");
942 /// let mut s = String::new();
943 /// s.extend(abc.chars());
944 /// s.extend(def.chars());
946 /// The correct use would be:
949 /// let def = String::from("def");
950 /// let mut s = String::new();
952 /// s.push_str(&def);
954 pub STRING_EXTEND_CHARS,
956 "using `x.extend(s.chars())` where s is a `&str` or `String`"
959 declare_clippy_lint! {
960 /// **What it does:** Checks for the use of `.cloned().collect()` on slice to
963 /// **Why is this bad?** `.to_vec()` is clearer
965 /// **Known problems:** None.
969 /// let s = [1, 2, 3, 4, 5];
970 /// let s2: Vec<isize> = s[..].iter().cloned().collect();
972 /// The better use would be:
974 /// let s = [1, 2, 3, 4, 5];
975 /// let s2: Vec<isize> = s.to_vec();
977 pub ITER_CLONED_COLLECT,
979 "using `.cloned().collect()` on slice to create a `Vec`"
982 declare_clippy_lint! {
983 /// **What it does:** Checks for usage of `_.chars().last()` or
984 /// `_.chars().next_back()` on a `str` to check if it ends with a given char.
986 /// **Why is this bad?** Readability, this can be written more concisely as
987 /// `_.ends_with(_)`.
989 /// **Known problems:** None.
993 /// # let name = "_";
996 /// name.chars().last() == Some('_') || name.chars().next_back() == Some('-');
999 /// name.ends_with('_') || name.ends_with('-');
1003 "using `.chars().last()` or `.chars().next_back()` to check if a string ends with a char"
1006 declare_clippy_lint! {
1007 /// **What it does:** Checks for usage of `.as_ref()` or `.as_mut()` where the
1008 /// types before and after the call are the same.
1010 /// **Why is this bad?** The call is unnecessary.
1012 /// **Known problems:** None.
1016 /// # fn do_stuff(x: &[i32]) {}
1017 /// let x: &[i32] = &[1, 2, 3, 4, 5];
1018 /// do_stuff(x.as_ref());
1020 /// The correct use would be:
1022 /// # fn do_stuff(x: &[i32]) {}
1023 /// let x: &[i32] = &[1, 2, 3, 4, 5];
1028 "using `as_ref` where the types before and after the call are the same"
1031 declare_clippy_lint! {
1032 /// **What it does:** Checks for using `fold` when a more succinct alternative exists.
1033 /// Specifically, this checks for `fold`s which could be replaced by `any`, `all`,
1034 /// `sum` or `product`.
1036 /// **Why is this bad?** Readability.
1038 /// **Known problems:** None.
1042 /// let _ = (0..3).fold(false, |acc, x| acc || x > 2);
1044 /// This could be written as:
1046 /// let _ = (0..3).any(|x| x > 2);
1048 pub UNNECESSARY_FOLD,
1050 "using `fold` when a more succinct alternative exists"
1053 declare_clippy_lint! {
1054 /// **What it does:** Checks for `filter_map` calls which could be replaced by `filter` or `map`.
1055 /// More specifically it checks if the closure provided is only performing one of the
1056 /// filter or map operations and suggests the appropriate option.
1058 /// **Why is this bad?** Complexity. The intent is also clearer if only a single
1059 /// operation is being performed.
1061 /// **Known problems:** None
1065 /// let _ = (0..3).filter_map(|x| if x > 2 { Some(x) } else { None });
1067 /// // As there is no transformation of the argument this could be written as:
1068 /// let _ = (0..3).filter(|&x| x > 2);
1072 /// let _ = (0..4).filter_map(|x| Some(x + 1));
1074 /// // As there is no conditional check on the argument this could be written as:
1075 /// let _ = (0..4).map(|x| x + 1);
1077 pub UNNECESSARY_FILTER_MAP,
1079 "using `filter_map` when a more succinct alternative exists"
1082 declare_clippy_lint! {
1083 /// **What it does:** Checks for `into_iter` calls on references which should be replaced by `iter`
1086 /// **Why is this bad?** Readability. Calling `into_iter` on a reference will not move out its
1087 /// content into the resulting iterator, which is confusing. It is better just call `iter` or
1088 /// `iter_mut` directly.
1090 /// **Known problems:** None
1096 /// let _ = (&vec![3, 4, 5]).into_iter();
1099 /// let _ = (&vec![3, 4, 5]).iter();
1101 pub INTO_ITER_ON_REF,
1103 "using `.into_iter()` on a reference"
1106 declare_clippy_lint! {
1107 /// **What it does:** Checks for calls to `map` followed by a `count`.
1109 /// **Why is this bad?** It looks suspicious. Maybe `map` was confused with `filter`.
1110 /// If the `map` call is intentional, this should be rewritten. Or, if you intend to
1111 /// drive the iterator to completion, you can just use `for_each` instead.
1113 /// **Known problems:** None
1118 /// let _ = (0..3).map(|x| x + 2).count();
1122 "suspicious usage of map"
1125 declare_clippy_lint! {
1126 /// **What it does:** Checks for `MaybeUninit::uninit().assume_init()`.
1128 /// **Why is this bad?** For most types, this is undefined behavior.
1130 /// **Known problems:** For now, we accept empty tuples and tuples / arrays
1131 /// of `MaybeUninit`. There may be other types that allow uninitialized
1132 /// data, but those are not yet rigorously defined.
1137 /// // Beware the UB
1138 /// use std::mem::MaybeUninit;
1140 /// let _: usize = unsafe { MaybeUninit::uninit().assume_init() };
1143 /// Note that the following is OK:
1146 /// use std::mem::MaybeUninit;
1148 /// let _: [MaybeUninit<bool>; 5] = unsafe {
1149 /// MaybeUninit::uninit().assume_init()
1152 pub UNINIT_ASSUMED_INIT,
1154 "`MaybeUninit::uninit().assume_init()`"
1157 declare_clippy_lint! {
1158 /// **What it does:** Checks for `.checked_add/sub(x).unwrap_or(MAX/MIN)`.
1160 /// **Why is this bad?** These can be written simply with `saturating_add/sub` methods.
1165 /// # let y: u32 = 0;
1166 /// # let x: u32 = 100;
1167 /// let add = x.checked_add(y).unwrap_or(u32::MAX);
1168 /// let sub = x.checked_sub(y).unwrap_or(u32::MIN);
1171 /// can be written using dedicated methods for saturating addition/subtraction as:
1174 /// # let y: u32 = 0;
1175 /// # let x: u32 = 100;
1176 /// let add = x.saturating_add(y);
1177 /// let sub = x.saturating_sub(y);
1179 pub MANUAL_SATURATING_ARITHMETIC,
1181 "`.chcked_add/sub(x).unwrap_or(MAX/MIN)`"
1184 declare_clippy_lint! {
1185 /// **What it does:** Checks for `offset(_)`, `wrapping_`{`add`, `sub`}, etc. on raw pointers to
1186 /// zero-sized types
1188 /// **Why is this bad?** This is a no-op, and likely unintended
1190 /// **Known problems:** None
1194 /// unsafe { (&() as *const ()).offset(1) };
1198 "Check for offset calculations on raw pointers to zero-sized types"
1201 declare_clippy_lint! {
1202 /// **What it does:** Checks for `FileType::is_file()`.
1204 /// **Why is this bad?** When people testing a file type with `FileType::is_file`
1205 /// they are testing whether a path is something they can get bytes from. But
1206 /// `is_file` doesn't cover special file types in unix-like systems, and doesn't cover
1207 /// symlink in windows. Using `!FileType::is_dir()` is a better way to that intention.
1213 /// let metadata = std::fs::metadata("foo.txt")?;
1214 /// let filetype = metadata.file_type();
1216 /// if filetype.is_file() {
1219 /// # Ok::<_, std::io::Error>(())
1223 /// should be written as:
1227 /// let metadata = std::fs::metadata("foo.txt")?;
1228 /// let filetype = metadata.file_type();
1230 /// if !filetype.is_dir() {
1233 /// # Ok::<_, std::io::Error>(())
1236 pub FILETYPE_IS_FILE,
1238 "`FileType::is_file` is not recommended to test for readable file type"
1241 declare_clippy_lint! {
1242 /// **What it does:** Checks for usage of `_.as_ref().map(Deref::deref)` or it's aliases (such as String::as_str).
1244 /// **Why is this bad?** Readability, this can be written more concisely as
1247 /// **Known problems:** None.
1251 /// # let opt = Some("".to_string());
1252 /// opt.as_ref().map(String::as_str)
1255 /// Can be written as
1257 /// # let opt = Some("".to_string());
1261 pub OPTION_AS_REF_DEREF,
1263 "using `as_ref().map(Deref::deref)`, which is more succinctly expressed as `as_deref()`"
1266 declare_clippy_lint! {
1267 /// **What it does:** Checks for usage of `iter().next()` on a Slice or an Array
1269 /// **Why is this bad?** These can be shortened into `.get()`
1271 /// **Known problems:** None.
1275 /// # let a = [1, 2, 3];
1276 /// # let b = vec![1, 2, 3];
1277 /// a[2..].iter().next();
1278 /// b.iter().next();
1280 /// should be written as:
1282 /// # let a = [1, 2, 3];
1283 /// # let b = vec![1, 2, 3];
1287 pub ITER_NEXT_SLICE,
1289 "using `.iter().next()` on a sliced array, which can be shortened to just `.get()`"
1292 declare_clippy_lint! {
1293 /// **What it does:** Warns when using `push_str`/`insert_str` with a single-character string literal
1294 /// where `push`/`insert` with a `char` would work fine.
1296 /// **Why is this bad?** It's less clear that we are pushing a single character.
1298 /// **Known problems:** None
1302 /// let mut string = String::new();
1303 /// string.insert_str(0, "R");
1304 /// string.push_str("R");
1306 /// Could be written as
1308 /// let mut string = String::new();
1309 /// string.insert(0, 'R');
1310 /// string.push('R');
1312 pub SINGLE_CHAR_ADD_STR,
1314 "`push_str()` or `insert_str()` used with a single-character string literal as parameter"
1317 declare_clippy_lint! {
1318 /// **What it does:** As the counterpart to `or_fun_call`, this lint looks for unnecessary
1319 /// lazily evaluated closures on `Option` and `Result`.
1321 /// This lint suggests changing the following functions, when eager evaluation results in
1323 /// - `unwrap_or_else` to `unwrap_or`
1324 /// - `and_then` to `and`
1325 /// - `or_else` to `or`
1326 /// - `get_or_insert_with` to `get_or_insert`
1327 /// - `ok_or_else` to `ok_or`
1329 /// **Why is this bad?** Using eager evaluation is shorter and simpler in some cases.
1331 /// **Known problems:** It is possible, but not recommended for `Deref` and `Index` to have
1332 /// side effects. Eagerly evaluating them can change the semantics of the program.
1337 /// // example code where clippy issues a warning
1338 /// let opt: Option<u32> = None;
1340 /// opt.unwrap_or_else(|| 42);
1344 /// let opt: Option<u32> = None;
1346 /// opt.unwrap_or(42);
1348 pub UNNECESSARY_LAZY_EVALUATIONS,
1350 "using unnecessary lazy evaluation, which can be replaced with simpler eager evaluation"
1353 declare_clippy_lint! {
1354 /// **What it does:** Checks for usage of `_.map(_).collect::<Result<(), _>()`.
1356 /// **Why is this bad?** Using `try_for_each` instead is more readable and idiomatic.
1358 /// **Known problems:** None
1363 /// (0..3).map(|t| Err(t)).collect::<Result<(), _>>();
1367 /// (0..3).try_for_each(|t| Err(t));
1369 pub MAP_COLLECT_RESULT_UNIT,
1371 "using `.map(_).collect::<Result<(),_>()`, which can be replaced with `try_for_each`"
1374 declare_clippy_lint! {
1375 /// **What it does:** Checks for `from_iter()` function calls on types that implement the `FromIterator`
1378 /// **Why is this bad?** It is recommended style to use collect. See
1379 /// [FromIterator documentation](https://doc.rust-lang.org/std/iter/trait.FromIterator.html)
1381 /// **Known problems:** None.
1386 /// use std::iter::FromIterator;
1388 /// let five_fives = std::iter::repeat(5).take(5);
1390 /// let v = Vec::from_iter(five_fives);
1392 /// assert_eq!(v, vec![5, 5, 5, 5, 5]);
1396 /// let five_fives = std::iter::repeat(5).take(5);
1398 /// let v: Vec<i32> = five_fives.collect();
1400 /// assert_eq!(v, vec![5, 5, 5, 5, 5]);
1402 pub FROM_ITER_INSTEAD_OF_COLLECT,
1404 "use `.collect()` instead of `::from_iter()`"
1407 declare_lint_pass!(Methods => [
1410 SHOULD_IMPLEMENT_TRAIT,
1411 WRONG_SELF_CONVENTION,
1412 WRONG_PUB_SELF_CONVENTION,
1415 RESULT_MAP_OR_INTO_OPTION,
1417 BIND_INSTEAD_OF_MAP,
1425 INEFFICIENT_TO_STRING,
1427 SINGLE_CHAR_PATTERN,
1428 SINGLE_CHAR_ADD_STR,
1437 ITERATOR_STEP_BY_ZERO,
1443 STRING_EXTEND_CHARS,
1444 ITER_CLONED_COLLECT,
1447 UNNECESSARY_FILTER_MAP,
1450 UNINIT_ASSUMED_INIT,
1451 MANUAL_SATURATING_ARITHMETIC,
1454 OPTION_AS_REF_DEREF,
1455 UNNECESSARY_LAZY_EVALUATIONS,
1456 MAP_COLLECT_RESULT_UNIT,
1457 FROM_ITER_INSTEAD_OF_COLLECT,
1460 impl<'tcx> LateLintPass<'tcx> for Methods {
1461 #[allow(clippy::too_many_lines)]
1462 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
1463 if in_macro(expr.span) {
1467 let (method_names, arg_lists, method_spans) = method_calls(expr, 2);
1468 let method_names: Vec<SymbolStr> = method_names.iter().map(|s| s.as_str()).collect();
1469 let method_names: Vec<&str> = method_names.iter().map(|s| &**s).collect();
1471 match method_names.as_slice() {
1472 ["unwrap", "get"] => lint_get_unwrap(cx, expr, arg_lists[1], false),
1473 ["unwrap", "get_mut"] => lint_get_unwrap(cx, expr, arg_lists[1], true),
1474 ["unwrap", ..] => lint_unwrap(cx, expr, arg_lists[0]),
1475 ["expect", "ok"] => lint_ok_expect(cx, expr, arg_lists[1]),
1476 ["expect", ..] => lint_expect(cx, expr, arg_lists[0]),
1477 ["unwrap_or", "map"] => option_map_unwrap_or::lint(cx, expr, arg_lists[1], arg_lists[0], method_spans[1]),
1478 ["unwrap_or_else", "map"] => {
1479 if !lint_map_unwrap_or_else(cx, expr, arg_lists[1], arg_lists[0]) {
1480 unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "unwrap_or");
1483 ["map_or", ..] => lint_map_or_none(cx, expr, arg_lists[0]),
1484 ["and_then", ..] => {
1485 let biom_option_linted = bind_instead_of_map::OptionAndThenSome::lint(cx, expr, arg_lists[0]);
1486 let biom_result_linted = bind_instead_of_map::ResultAndThenOk::lint(cx, expr, arg_lists[0]);
1487 if !biom_option_linted && !biom_result_linted {
1488 unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "and");
1491 ["or_else", ..] => {
1492 if !bind_instead_of_map::ResultOrElseErrInfo::lint(cx, expr, arg_lists[0]) {
1493 unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "or");
1496 ["next", "filter"] => lint_filter_next(cx, expr, arg_lists[1]),
1497 ["next", "skip_while"] => lint_skip_while_next(cx, expr, arg_lists[1]),
1498 ["next", "iter"] => lint_iter_next(cx, expr, arg_lists[1]),
1499 ["map", "filter"] => lint_filter_map(cx, expr, arg_lists[1], arg_lists[0]),
1500 ["map", "filter_map"] => lint_filter_map_map(cx, expr, arg_lists[1], arg_lists[0]),
1501 ["next", "filter_map"] => lint_filter_map_next(cx, expr, arg_lists[1]),
1502 ["map", "find"] => lint_find_map(cx, expr, arg_lists[1], arg_lists[0]),
1503 ["flat_map", "filter"] => lint_filter_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
1504 ["flat_map", "filter_map"] => lint_filter_map_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
1505 ["flat_map", ..] => lint_flat_map_identity(cx, expr, arg_lists[0], method_spans[0]),
1506 ["flatten", "map"] => lint_map_flatten(cx, expr, arg_lists[1]),
1507 ["is_some", "find"] => lint_search_is_some(cx, expr, "find", arg_lists[1], arg_lists[0], method_spans[1]),
1508 ["is_some", "position"] => {
1509 lint_search_is_some(cx, expr, "position", arg_lists[1], arg_lists[0], method_spans[1])
1511 ["is_some", "rposition"] => {
1512 lint_search_is_some(cx, expr, "rposition", arg_lists[1], arg_lists[0], method_spans[1])
1514 ["extend", ..] => lint_extend(cx, expr, arg_lists[0]),
1515 ["nth", "iter"] => lint_iter_nth(cx, expr, &arg_lists, false),
1516 ["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true),
1517 ["nth", ..] => lint_iter_nth_zero(cx, expr, arg_lists[0]),
1518 ["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
1519 ["next", "skip"] => lint_iter_skip_next(cx, expr, arg_lists[1]),
1520 ["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
1521 ["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
1522 ["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
1523 ["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0], method_spans[0]),
1524 ["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]),
1525 ["count", "map"] => lint_suspicious_map(cx, expr),
1526 ["assume_init"] => lint_maybe_uninit(cx, &arg_lists[0][0], expr),
1527 ["unwrap_or", arith @ ("checked_add" | "checked_sub" | "checked_mul")] => {
1528 manual_saturating_arithmetic::lint(cx, expr, &arg_lists, &arith["checked_".len()..])
1530 ["add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub"] => {
1531 check_pointer_offset(cx, expr, arg_lists[0])
1533 ["is_file", ..] => lint_filetype_is_file(cx, expr, arg_lists[0]),
1534 ["map", "as_ref"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], false),
1535 ["map", "as_mut"] => lint_option_as_ref_deref(cx, expr, arg_lists[1], arg_lists[0], true),
1536 ["unwrap_or_else", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "unwrap_or"),
1537 ["get_or_insert_with", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "get_or_insert"),
1538 ["ok_or_else", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "ok_or"),
1539 ["collect", "map"] => lint_map_collect(cx, expr, arg_lists[1], arg_lists[0]),
1544 hir::ExprKind::Call(ref func, ref args) => {
1545 if let hir::ExprKind::Path(path) = &func.kind {
1546 if match_qpath(path, &["from_iter"]) {
1547 lint_from_iter(cx, expr, args);
1551 hir::ExprKind::MethodCall(ref method_call, ref method_span, ref args, _) => {
1552 lint_or_fun_call(cx, expr, *method_span, &method_call.ident.as_str(), args);
1553 lint_expect_fun_call(cx, expr, *method_span, &method_call.ident.as_str(), args);
1555 let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
1556 if args.len() == 1 && method_call.ident.name == sym!(clone) {
1557 lint_clone_on_copy(cx, expr, &args[0], self_ty);
1558 lint_clone_on_ref_ptr(cx, expr, &args[0]);
1560 if args.len() == 1 && method_call.ident.name == sym!(to_string) {
1561 inefficient_to_string::lint(cx, expr, &args[0], self_ty);
1564 if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
1565 if match_def_path(cx, fn_def_id, &paths::PUSH_STR) {
1566 lint_single_char_push_string(cx, expr, args);
1567 } else if match_def_path(cx, fn_def_id, &paths::INSERT_STR) {
1568 lint_single_char_insert_string(cx, expr, args);
1572 match self_ty.kind() {
1573 ty::Ref(_, ty, _) if *ty.kind() == ty::Str => {
1574 for &(method, pos) in &PATTERN_METHODS {
1575 if method_call.ident.name.as_str() == method && args.len() > pos {
1576 lint_single_char_pattern(cx, expr, &args[pos]);
1580 ty::Ref(..) if method_call.ident.name == sym!(into_iter) => {
1581 lint_into_iter(cx, expr, self_ty, *method_span);
1586 hir::ExprKind::Binary(op, ref lhs, ref rhs)
1587 if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne =>
1589 let mut info = BinaryExprInfo {
1593 eq: op.node == hir::BinOpKind::Eq,
1595 lint_binary_expr_with_method_call(cx, &mut info);
1601 #[allow(clippy::too_many_lines)]
1602 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
1603 if in_external_macro(cx.sess(), impl_item.span) {
1606 let name = impl_item.ident.name.as_str();
1607 let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
1608 let item = cx.tcx.hir().expect_item(parent);
1609 let def_id = cx.tcx.hir().local_def_id(item.hir_id);
1610 let self_ty = cx.tcx.type_of(def_id);
1612 if let hir::ImplItemKind::Fn(ref sig, id) = impl_item.kind;
1613 if let Some(first_arg) = iter_input_pats(&sig.decl, cx.tcx.hir().body(id)).next();
1614 if let hir::ItemKind::Impl{ of_trait: None, .. } = item.kind;
1616 let method_def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
1617 let method_sig = cx.tcx.fn_sig(method_def_id);
1618 let method_sig = cx.tcx.erase_late_bound_regions(method_sig);
1620 let first_arg_ty = &method_sig.inputs().iter().next();
1622 // check conventions w.r.t. conversion method names and predicates
1623 if let Some(first_arg_ty) = first_arg_ty;
1626 if cx.access_levels.is_exported(impl_item.hir_id) {
1627 // check missing trait implementations
1628 for method_config in &TRAIT_METHODS {
1629 if name == method_config.method_name &&
1630 sig.decl.inputs.len() == method_config.param_count &&
1631 method_config.output_type.matches(cx, &sig.decl.output) &&
1632 method_config.self_kind.matches(cx, self_ty, first_arg_ty) &&
1633 fn_header_equals(method_config.fn_header, sig.header) &&
1634 method_config.lifetime_param_cond(&impl_item)
1638 SHOULD_IMPLEMENT_TRAIT,
1641 "method `{}` can be confused for the standard trait method `{}::{}`",
1642 method_config.method_name,
1643 method_config.trait_name,
1644 method_config.method_name
1648 "consider implementing the trait `{}` or choosing a less ambiguous method name",
1649 method_config.trait_name
1656 if let Some((ref conv, self_kinds)) = &CONVENTIONS
1658 .find(|(ref conv, _)| conv.check(&name))
1660 if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1661 let lint = if item.vis.node.is_pub() {
1662 WRONG_PUB_SELF_CONVENTION
1664 WRONG_SELF_CONVENTION
1671 &format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
1675 .map(|k| k.description())
1676 .collect::<Vec<_>>()
1685 // if this impl block implements a trait, lint in trait definition instead
1686 if let hir::ItemKind::Impl { of_trait: Some(_), .. } = item.kind {
1690 if let hir::ImplItemKind::Fn(_, _) = impl_item.kind {
1691 let ret_ty = return_ty(cx, impl_item.hir_id);
1693 // walk the return type and check for Self (this does not check associated types)
1694 if contains_ty(ret_ty, self_ty) {
1698 // if return type is impl trait, check the associated types
1699 if let ty::Opaque(def_id, _) = *ret_ty.kind() {
1700 // one of the associated types must be Self
1701 for &(predicate, _span) in cx.tcx.explicit_item_bounds(def_id) {
1702 if let ty::PredicateAtom::Projection(projection_predicate) = predicate.skip_binders() {
1703 // walk the associated type and check for Self
1704 if contains_ty(projection_predicate.ty, self_ty) {
1711 if name == "new" && !TyS::same_type(ret_ty, self_ty) {
1716 "methods called `new` usually return `Self`",
1722 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
1724 if !in_external_macro(cx.tcx.sess, item.span);
1725 if item.ident.name == sym::new;
1726 if let TraitItemKind::Fn(_, _) = item.kind;
1727 let ret_ty = return_ty(cx, item.hir_id);
1728 let self_ty = TraitRef::identity(cx.tcx, item.hir_id.owner.to_def_id()).self_ty();
1729 if !contains_ty(ret_ty, self_ty);
1736 "methods called `new` usually return `Self`",
1743 /// Checks for the `OR_FUN_CALL` lint.
1744 #[allow(clippy::too_many_lines)]
1745 fn lint_or_fun_call<'tcx>(
1746 cx: &LateContext<'tcx>,
1747 expr: &hir::Expr<'_>,
1750 args: &'tcx [hir::Expr<'_>],
1752 /// Checks for `unwrap_or(T::new())` or `unwrap_or(T::default())`.
1753 fn check_unwrap_or_default(
1754 cx: &LateContext<'_>,
1756 fun: &hir::Expr<'_>,
1757 self_expr: &hir::Expr<'_>,
1758 arg: &hir::Expr<'_>,
1764 if name == "unwrap_or";
1765 if let hir::ExprKind::Path(ref qpath) = fun.kind;
1766 let path = &*last_path_segment(qpath).ident.as_str();
1767 if ["default", "new"].contains(&path);
1768 let arg_ty = cx.typeck_results().expr_ty(arg);
1769 if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
1770 if implements_trait(cx, arg_ty, default_trait_id, &[]);
1773 let mut applicability = Applicability::MachineApplicable;
1778 &format!("use of `{}` followed by a call to `{}`", name, path),
1781 "{}.unwrap_or_default()",
1782 snippet_with_applicability(cx, self_expr.span, "..", &mut applicability)
1794 /// Checks for `*or(foo())`.
1795 #[allow(clippy::too_many_arguments)]
1796 fn check_general_case<'tcx>(
1797 cx: &LateContext<'tcx>,
1800 self_expr: &hir::Expr<'_>,
1801 arg: &'tcx hir::Expr<'_>,
1803 // None if lambda is required
1804 fun_span: Option<Span>,
1806 // (path, fn_has_argument, methods, suffix)
1807 static KNOW_TYPES: [(&[&str], bool, &[&str], &str); 4] = [
1808 (&paths::BTREEMAP_ENTRY, false, &["or_insert"], "with"),
1809 (&paths::HASHMAP_ENTRY, false, &["or_insert"], "with"),
1810 (&paths::OPTION, false, &["map_or", "ok_or", "or", "unwrap_or"], "else"),
1811 (&paths::RESULT, true, &["or", "unwrap_or"], "else"),
1814 if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = &arg.kind {
1815 if path.ident.as_str() == "len" {
1816 let ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
1819 ty::Slice(_) | ty::Array(_, _) => return,
1823 if is_type_diagnostic_item(cx, ty, sym::vec_type) {
1830 if KNOW_TYPES.iter().any(|k| k.2.contains(&name));
1832 if is_lazyness_candidate(cx, arg);
1833 if !contains_return(&arg);
1835 let self_ty = cx.typeck_results().expr_ty(self_expr);
1837 if let Some(&(_, fn_has_arguments, poss, suffix)) =
1838 KNOW_TYPES.iter().find(|&&i| match_type(cx, self_ty, i.0));
1840 if poss.contains(&name);
1843 let sugg: Cow<'_, str> = {
1844 let (snippet_span, use_lambda) = match (fn_has_arguments, fun_span) {
1845 (false, Some(fun_span)) => (fun_span, false),
1846 _ => (arg.span, true),
1848 let snippet = snippet_with_macro_callsite(cx, snippet_span, "..");
1850 let l_arg = if fn_has_arguments { "_" } else { "" };
1851 format!("|{}| {}", l_arg, snippet).into()
1856 let span_replace_word = method_span.with_hi(span.hi());
1861 &format!("use of `{}` followed by a function call", name),
1863 format!("{}_{}({})", name, suffix, sugg),
1864 Applicability::HasPlaceholders,
1870 if args.len() == 2 {
1871 match args[1].kind {
1872 hir::ExprKind::Call(ref fun, ref or_args) => {
1873 let or_has_args = !or_args.is_empty();
1874 if !check_unwrap_or_default(cx, name, fun, &args[0], &args[1], or_has_args, expr.span) {
1875 let fun_span = if or_has_args { None } else { Some(fun.span) };
1876 check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, fun_span);
1879 hir::ExprKind::Index(..) | hir::ExprKind::MethodCall(..) => {
1880 check_general_case(cx, name, method_span, &args[0], &args[1], expr.span, None);
1887 /// Checks for the `EXPECT_FUN_CALL` lint.
1888 #[allow(clippy::too_many_lines)]
1889 fn lint_expect_fun_call(
1890 cx: &LateContext<'_>,
1891 expr: &hir::Expr<'_>,
1894 args: &[hir::Expr<'_>],
1896 // Strip `&`, `as_ref()` and `as_str()` off `arg` until we're left with either a `String` or
1898 fn get_arg_root<'a>(cx: &LateContext<'_>, arg: &'a hir::Expr<'a>) -> &'a hir::Expr<'a> {
1899 let mut arg_root = arg;
1901 arg_root = match &arg_root.kind {
1902 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, expr) => expr,
1903 hir::ExprKind::MethodCall(method_name, _, call_args, _) => {
1904 if call_args.len() == 1
1905 && (method_name.ident.name == sym::as_str || method_name.ident.name == sym!(as_ref))
1907 let arg_type = cx.typeck_results().expr_ty(&call_args[0]);
1908 let base_type = arg_type.peel_refs();
1909 *base_type.kind() == ty::Str || is_type_diagnostic_item(cx, base_type, sym::string_type)
1923 // Only `&'static str` or `String` can be used directly in the `panic!`. Other types should be
1924 // converted to string.
1925 fn requires_to_string(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
1926 let arg_ty = cx.typeck_results().expr_ty(arg);
1927 if is_type_diagnostic_item(cx, arg_ty, sym::string_type) {
1930 if let ty::Ref(_, ty, ..) = arg_ty.kind() {
1931 if *ty.kind() == ty::Str && can_be_static_str(cx, arg) {
1938 // Check if an expression could have type `&'static str`, knowing that it
1939 // has type `&str` for some lifetime.
1940 fn can_be_static_str(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
1942 hir::ExprKind::Lit(_) => true,
1943 hir::ExprKind::Call(fun, _) => {
1944 if let hir::ExprKind::Path(ref p) = fun.kind {
1945 match cx.qpath_res(p, fun.hir_id) {
1946 hir::def::Res::Def(hir::def::DefKind::Fn | hir::def::DefKind::AssocFn, def_id) => matches!(
1947 cx.tcx.fn_sig(def_id).output().skip_binder().kind(),
1948 ty::Ref(ty::ReStatic, ..)
1956 hir::ExprKind::MethodCall(..) => {
1958 .type_dependent_def_id(arg.hir_id)
1959 .map_or(false, |method_id| {
1961 cx.tcx.fn_sig(method_id).output().skip_binder().kind(),
1962 ty::Ref(ty::ReStatic, ..)
1966 hir::ExprKind::Path(ref p) => matches!(
1967 cx.qpath_res(p, arg.hir_id),
1968 hir::def::Res::Def(hir::def::DefKind::Const | hir::def::DefKind::Static, _)
1974 fn generate_format_arg_snippet(
1975 cx: &LateContext<'_>,
1977 applicability: &mut Applicability,
1980 if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, ref format_arg) = a.kind;
1981 if let hir::ExprKind::Match(ref format_arg_expr, _, _) = format_arg.kind;
1982 if let hir::ExprKind::Tup(ref format_arg_expr_tup) = format_arg_expr.kind;
1987 .map(|a| snippet_with_applicability(cx, a.span, "..", applicability).into_owned())
1995 fn is_call(node: &hir::ExprKind<'_>) -> bool {
1997 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, expr) => {
2000 hir::ExprKind::Call(..)
2001 | hir::ExprKind::MethodCall(..)
2002 // These variants are debatable or require further examination
2003 | hir::ExprKind::Match(..)
2004 | hir::ExprKind::Block{ .. } => true,
2009 if args.len() != 2 || name != "expect" || !is_call(&args[1].kind) {
2013 let receiver_type = cx.typeck_results().expr_ty_adjusted(&args[0]);
2014 let closure_args = if is_type_diagnostic_item(cx, receiver_type, sym::option_type) {
2016 } else if is_type_diagnostic_item(cx, receiver_type, sym::result_type) {
2022 let arg_root = get_arg_root(cx, &args[1]);
2024 let span_replace_word = method_span.with_hi(expr.span.hi());
2026 let mut applicability = Applicability::MachineApplicable;
2028 //Special handling for `format!` as arg_root
2030 if let hir::ExprKind::Block(block, None) = &arg_root.kind;
2031 if block.stmts.len() == 1;
2032 if let hir::StmtKind::Local(local) = &block.stmts[0].kind;
2033 if let Some(arg_root) = &local.init;
2034 if let hir::ExprKind::Call(ref inner_fun, ref inner_args) = arg_root.kind;
2035 if is_expn_of(inner_fun.span, "format").is_some() && inner_args.len() == 1;
2036 if let hir::ExprKind::Call(_, format_args) = &inner_args[0].kind;
2038 let fmt_spec = &format_args[0];
2039 let fmt_args = &format_args[1];
2041 let mut args = vec![snippet(cx, fmt_spec.span, "..").into_owned()];
2043 args.extend(generate_format_arg_snippet(cx, fmt_args, &mut applicability));
2045 let sugg = args.join(", ");
2051 &format!("use of `{}` followed by a function call", name),
2053 format!("unwrap_or_else({} panic!({}))", closure_args, sugg),
2061 let mut arg_root_snippet: Cow<'_, _> = snippet_with_applicability(cx, arg_root.span, "..", &mut applicability);
2062 if requires_to_string(cx, arg_root) {
2063 arg_root_snippet.to_mut().push_str(".to_string()");
2070 &format!("use of `{}` followed by a function call", name),
2072 format!("unwrap_or_else({} {{ panic!({}) }})", closure_args, arg_root_snippet),
2077 /// Checks for the `CLONE_ON_COPY` lint.
2078 fn lint_clone_on_copy(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>, arg_ty: Ty<'_>) {
2079 let ty = cx.typeck_results().expr_ty(expr);
2080 if let ty::Ref(_, inner, _) = arg_ty.kind() {
2081 if let ty::Ref(_, innermost, _) = inner.kind() {
2086 "using `clone` on a double-reference; \
2087 this will copy the reference instead of cloning the inner type",
2089 if let Some(snip) = sugg::Sugg::hir_opt(cx, arg) {
2090 let mut ty = innermost;
2092 while let ty::Ref(_, inner, _) = ty.kind() {
2096 let refs: String = iter::repeat('&').take(n + 1).collect();
2097 let derefs: String = iter::repeat('*').take(n).collect();
2098 let explicit = format!("<{}{}>::clone({})", refs, ty, snip);
2099 diag.span_suggestion(
2101 "try dereferencing it",
2102 format!("{}({}{}).clone()", refs, derefs, snip.deref()),
2103 Applicability::MaybeIncorrect,
2105 diag.span_suggestion(
2107 "or try being explicit if you are sure, that you want to clone a reference",
2109 Applicability::MaybeIncorrect,
2114 return; // don't report clone_on_copy
2118 if is_copy(cx, ty) {
2120 if let Some(snippet) = sugg::Sugg::hir_opt(cx, arg) {
2121 let parent = cx.tcx.hir().get_parent_node(expr.hir_id);
2122 match &cx.tcx.hir().get(parent) {
2123 hir::Node::Expr(parent) => match parent.kind {
2124 // &*x is a nop, &x.clone() is not
2125 hir::ExprKind::AddrOf(..) => return,
2126 // (*x).func() is useless, x.clone().func() can work in case func borrows mutably
2127 hir::ExprKind::MethodCall(_, _, parent_args, _) if expr.hir_id == parent_args[0].hir_id => {
2133 hir::Node::Stmt(stmt) => {
2134 if let hir::StmtKind::Local(ref loc) = stmt.kind {
2135 if let hir::PatKind::Ref(..) = loc.pat.kind {
2136 // let ref y = *x borrows x, let ref y = x.clone() does not
2144 // x.clone() might have dereferenced x, possibly through Deref impls
2145 if cx.typeck_results().expr_ty(arg) == ty {
2146 snip = Some(("try removing the `clone` call", format!("{}", snippet)));
2148 let deref_count = cx
2150 .expr_adjustments(arg)
2152 .filter(|adj| matches!(adj.kind, ty::adjustment::Adjust::Deref(_)))
2154 let derefs: String = iter::repeat('*').take(deref_count).collect();
2155 snip = Some(("try dereferencing it", format!("{}{}", derefs, snippet)));
2160 span_lint_and_then(cx, CLONE_ON_COPY, expr.span, "using `clone` on a `Copy` type", |diag| {
2161 if let Some((text, snip)) = snip {
2162 diag.span_suggestion(expr.span, text, snip, Applicability::MachineApplicable);
2168 fn lint_clone_on_ref_ptr(cx: &LateContext<'_>, expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
2169 let obj_ty = cx.typeck_results().expr_ty(arg).peel_refs();
2171 if let ty::Adt(_, subst) = obj_ty.kind() {
2172 let caller_type = if is_type_diagnostic_item(cx, obj_ty, sym::Rc) {
2174 } else if is_type_diagnostic_item(cx, obj_ty, sym::Arc) {
2176 } else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
2182 let snippet = snippet_with_macro_callsite(cx, arg.span, "..");
2188 "using `.clone()` on a ref-counted pointer",
2190 format!("{}::<{}>::clone(&{})", caller_type, subst.type_at(0), snippet),
2191 Applicability::Unspecified, // Sometimes unnecessary ::<_> after Rc/Arc/Weak
2196 fn lint_string_extend(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
2198 if let Some(arglists) = method_chain_args(arg, &["chars"]) {
2199 let target = &arglists[0][0];
2200 let self_ty = cx.typeck_results().expr_ty(target).peel_refs();
2201 let ref_str = if *self_ty.kind() == ty::Str {
2203 } else if is_type_diagnostic_item(cx, self_ty, sym::string_type) {
2209 let mut applicability = Applicability::MachineApplicable;
2212 STRING_EXTEND_CHARS,
2214 "calling `.extend(_.chars())`",
2217 "{}.push_str({}{})",
2218 snippet_with_applicability(cx, args[0].span, "..", &mut applicability),
2220 snippet_with_applicability(cx, target.span, "..", &mut applicability)
2227 fn lint_extend(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
2228 let obj_ty = cx.typeck_results().expr_ty(&args[0]).peel_refs();
2229 if is_type_diagnostic_item(cx, obj_ty, sym::string_type) {
2230 lint_string_extend(cx, expr, args);
2234 fn lint_iter_cloned_collect<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, iter_args: &'tcx [hir::Expr<'_>]) {
2236 if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(expr), sym::vec_type);
2237 if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0]));
2238 if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
2243 ITER_CLONED_COLLECT,
2245 "called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
2248 ".to_vec()".to_string(),
2249 Applicability::MachineApplicable,
2255 fn lint_unnecessary_fold(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args: &[hir::Expr<'_>], fold_span: Span) {
2256 fn check_fold_with_op(
2257 cx: &LateContext<'_>,
2258 expr: &hir::Expr<'_>,
2259 fold_args: &[hir::Expr<'_>],
2262 replacement_method_name: &str,
2263 replacement_has_args: bool,
2266 // Extract the body of the closure passed to fold
2267 if let hir::ExprKind::Closure(_, _, body_id, _, _) = fold_args[2].kind;
2268 let closure_body = cx.tcx.hir().body(body_id);
2269 let closure_expr = remove_blocks(&closure_body.value);
2271 // Check if the closure body is of the form `acc <op> some_expr(x)`
2272 if let hir::ExprKind::Binary(ref bin_op, ref left_expr, ref right_expr) = closure_expr.kind;
2273 if bin_op.node == op;
2275 // Extract the names of the two arguments to the closure
2276 if let Some(first_arg_ident) = get_arg_name(&closure_body.params[0].pat);
2277 if let Some(second_arg_ident) = get_arg_name(&closure_body.params[1].pat);
2279 if match_var(&*left_expr, first_arg_ident);
2280 if replacement_has_args || match_var(&*right_expr, second_arg_ident);
2283 let mut applicability = Applicability::MachineApplicable;
2284 let sugg = if replacement_has_args {
2286 "{replacement}(|{s}| {r})",
2287 replacement = replacement_method_name,
2288 s = second_arg_ident,
2289 r = snippet_with_applicability(cx, right_expr.span, "EXPR", &mut applicability),
2294 replacement = replacement_method_name,
2301 fold_span.with_hi(expr.span.hi()),
2302 // TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
2303 "this `.fold` can be written more succinctly using another method",
2312 // Check that this is a call to Iterator::fold rather than just some function called fold
2313 if !match_trait_method(cx, expr, &paths::ITERATOR) {
2318 fold_args.len() == 3,
2319 "Expected fold_args to have three entries - the receiver, the initial value and the closure"
2322 // Check if the first argument to .fold is a suitable literal
2323 if let hir::ExprKind::Lit(ref lit) = fold_args[1].kind {
2325 ast::LitKind::Bool(false) => {
2326 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Or, "any", true)
2328 ast::LitKind::Bool(true) => {
2329 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::And, "all", true)
2331 ast::LitKind::Int(0, _) => {
2332 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Add, "sum", false)
2334 ast::LitKind::Int(1, _) => {
2335 check_fold_with_op(cx, expr, fold_args, fold_span, hir::BinOpKind::Mul, "product", false)
2342 fn lint_step_by<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, args: &'tcx [hir::Expr<'_>]) {
2343 if match_trait_method(cx, expr, &paths::ITERATOR) {
2344 if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), &args[1]) {
2347 ITERATOR_STEP_BY_ZERO,
2349 "Iterator::step_by(0) will panic at runtime",
2355 fn lint_iter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, iter_args: &'tcx [hir::Expr<'_>]) {
2356 let caller_expr = &iter_args[0];
2358 // Skip lint if the `iter().next()` expression is a for loop argument,
2359 // since it is already covered by `&loops::ITER_NEXT_LOOP`
2360 let mut parent_expr_opt = get_parent_expr(cx, expr);
2361 while let Some(parent_expr) = parent_expr_opt {
2362 if higher::for_loop(parent_expr).is_some() {
2365 parent_expr_opt = get_parent_expr(cx, parent_expr);
2368 if derefs_to_slice(cx, caller_expr, cx.typeck_results().expr_ty(caller_expr)).is_some() {
2369 // caller is a Slice
2371 if let hir::ExprKind::Index(ref caller_var, ref index_expr) = &caller_expr.kind;
2372 if let Some(higher::Range { start: Some(start_expr), end: None, limits: ast::RangeLimits::HalfOpen })
2373 = higher::range(index_expr);
2374 if let hir::ExprKind::Lit(ref start_lit) = &start_expr.kind;
2375 if let ast::LitKind::Int(start_idx, _) = start_lit.node;
2377 let mut applicability = Applicability::MachineApplicable;
2382 "using `.iter().next()` on a Slice without end index",
2384 format!("{}.get({})", snippet_with_applicability(cx, caller_var.span, "..", &mut applicability), start_idx),
2389 } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(caller_expr), sym::vec_type)
2391 &cx.typeck_results().expr_ty(caller_expr).peel_refs().kind(),
2395 // caller is a Vec or an Array
2396 let mut applicability = Applicability::MachineApplicable;
2401 "using `.iter().next()` on an array",
2405 snippet_with_applicability(cx, caller_expr.span, "..", &mut applicability)
2412 fn lint_iter_nth<'tcx>(
2413 cx: &LateContext<'tcx>,
2414 expr: &hir::Expr<'_>,
2415 nth_and_iter_args: &[&'tcx [hir::Expr<'tcx>]],
2418 let iter_args = nth_and_iter_args[1];
2419 let mut_str = if is_mut { "_mut" } else { "" };
2420 let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.typeck_results().expr_ty(&iter_args[0])).is_some() {
2422 } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) {
2424 } else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) {
2427 let nth_args = nth_and_iter_args[0];
2428 lint_iter_nth_zero(cx, expr, &nth_args);
2429 return; // caller is not a type that we want to lint
2436 &format!("called `.iter{0}().nth()` on a {1}", mut_str, caller_type),
2438 &format!("calling `.get{}()` is both faster and more readable", mut_str),
2442 fn lint_iter_nth_zero<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, nth_args: &'tcx [hir::Expr<'_>]) {
2444 if match_trait_method(cx, expr, &paths::ITERATOR);
2445 if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), &nth_args[1]);
2447 let mut applicability = Applicability::MachineApplicable;
2452 "called `.nth(0)` on a `std::iter::Iterator`, when `.next()` is equivalent",
2453 "try calling `.next()` instead of `.nth(0)`",
2454 format!("{}.next()", snippet_with_applicability(cx, nth_args[0].span, "..", &mut applicability)),
2461 fn lint_get_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, get_args: &'tcx [hir::Expr<'_>], is_mut: bool) {
2462 // Note: we don't want to lint `get_mut().unwrap` for `HashMap` or `BTreeMap`,
2463 // because they do not implement `IndexMut`
2464 let mut applicability = Applicability::MachineApplicable;
2465 let expr_ty = cx.typeck_results().expr_ty(&get_args[0]);
2466 let get_args_str = if get_args.len() > 1 {
2467 snippet_with_applicability(cx, get_args[1].span, "..", &mut applicability)
2469 return; // not linting on a .get().unwrap() chain or variant
2472 let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
2473 needs_ref = get_args_str.parse::<usize>().is_ok();
2475 } else if is_type_diagnostic_item(cx, expr_ty, sym::vec_type) {
2476 needs_ref = get_args_str.parse::<usize>().is_ok();
2478 } else if is_type_diagnostic_item(cx, expr_ty, sym!(vecdeque_type)) {
2479 needs_ref = get_args_str.parse::<usize>().is_ok();
2481 } else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym!(hashmap_type)) {
2484 } else if !is_mut && match_type(cx, expr_ty, &paths::BTREEMAP) {
2488 return; // caller is not a type that we want to lint
2491 let mut span = expr.span;
2493 // Handle the case where the result is immediately dereferenced
2494 // by not requiring ref and pulling the dereference into the
2498 if let Some(parent) = get_parent_expr(cx, expr);
2499 if let hir::ExprKind::Unary(hir::UnOp::UnDeref, _) = parent.kind;
2506 let mut_str = if is_mut { "_mut" } else { "" };
2507 let borrow_str = if !needs_ref {
2520 "called `.get{0}().unwrap()` on a {1}. Using `[]` is more clear and more concise",
2521 mut_str, caller_type
2527 snippet_with_applicability(cx, get_args[0].span, "..", &mut applicability),
2534 fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[hir::Expr<'_>]) {
2535 // lint if caller of skip is an Iterator
2536 if match_trait_method(cx, expr, &paths::ITERATOR) {
2537 if let [caller, n] = skip_args {
2538 let hint = format!(".nth({})", snippet(cx, n.span, ".."));
2542 expr.span.trim_start(caller.span).unwrap(),
2543 "called `skip(..).next()` on an iterator",
2544 "use `nth` instead",
2546 Applicability::MachineApplicable,
2552 fn derefs_to_slice<'tcx>(
2553 cx: &LateContext<'tcx>,
2554 expr: &'tcx hir::Expr<'tcx>,
2556 ) -> Option<&'tcx hir::Expr<'tcx>> {
2557 fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
2559 ty::Slice(_) => true,
2560 ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
2561 ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::vec_type),
2562 ty::Array(_, size) => size
2563 .try_eval_usize(cx.tcx, cx.param_env)
2564 .map_or(false, |size| size < 32),
2565 ty::Ref(_, inner, _) => may_slice(cx, inner),
2570 if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind {
2571 if path.ident.name == sym::iter && may_slice(cx, cx.typeck_results().expr_ty(&args[0])) {
2578 ty::Slice(_) => Some(expr),
2579 ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
2580 ty::Ref(_, inner, _) => {
2581 if may_slice(cx, inner) {
2592 /// lint use of `unwrap()` for `Option`s and `Result`s
2593 fn lint_unwrap(cx: &LateContext<'_>, expr: &hir::Expr<'_>, unwrap_args: &[hir::Expr<'_>]) {
2594 let obj_ty = cx.typeck_results().expr_ty(&unwrap_args[0]).peel_refs();
2596 let mess = if is_type_diagnostic_item(cx, obj_ty, sym::option_type) {
2597 Some((UNWRAP_USED, "an Option", "None"))
2598 } else if is_type_diagnostic_item(cx, obj_ty, sym::result_type) {
2599 Some((UNWRAP_USED, "a Result", "Err"))
2604 if let Some((lint, kind, none_value)) = mess {
2609 &format!("used `unwrap()` on `{}` value", kind,),
2612 "if you don't want to handle the `{}` case gracefully, consider \
2613 using `expect()` to provide a better panic message",
2620 /// lint use of `expect()` for `Option`s and `Result`s
2621 fn lint_expect(cx: &LateContext<'_>, expr: &hir::Expr<'_>, expect_args: &[hir::Expr<'_>]) {
2622 let obj_ty = cx.typeck_results().expr_ty(&expect_args[0]).peel_refs();
2624 let mess = if is_type_diagnostic_item(cx, obj_ty, sym!(option_type)) {
2625 Some((EXPECT_USED, "an Option", "None"))
2626 } else if is_type_diagnostic_item(cx, obj_ty, sym!(result_type)) {
2627 Some((EXPECT_USED, "a Result", "Err"))
2632 if let Some((lint, kind, none_value)) = mess {
2637 &format!("used `expect()` on `{}` value", kind,),
2639 &format!("if this value is an `{}`, it will panic", none_value,),
2644 /// lint use of `ok().expect()` for `Result`s
2645 fn lint_ok_expect(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ok_args: &[hir::Expr<'_>]) {
2647 // lint if the caller of `ok()` is a `Result`
2648 if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&ok_args[0]), sym::result_type);
2649 let result_type = cx.typeck_results().expr_ty(&ok_args[0]);
2650 if let Some(error_type) = get_error_type(cx, result_type);
2651 if has_debug_impl(error_type, cx);
2658 "called `ok().expect()` on a `Result` value",
2660 "you can call `expect()` directly on the `Result`",
2666 /// lint use of `map().flatten()` for `Iterators` and 'Options'
2667 fn lint_map_flatten<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map_args: &'tcx [hir::Expr<'_>]) {
2668 // lint if caller of `.map().flatten()` is an Iterator
2669 if match_trait_method(cx, expr, &paths::ITERATOR) {
2670 let map_closure_ty = cx.typeck_results().expr_ty(&map_args[1]);
2671 let is_map_to_option = match map_closure_ty.kind() {
2672 ty::Closure(_, _) | ty::FnDef(_, _) | ty::FnPtr(_) => {
2673 let map_closure_sig = match map_closure_ty.kind() {
2674 ty::Closure(_, substs) => substs.as_closure().sig(),
2675 _ => map_closure_ty.fn_sig(cx.tcx),
2677 let map_closure_return_ty = cx.tcx.erase_late_bound_regions(map_closure_sig.output());
2678 is_type_diagnostic_item(cx, map_closure_return_ty, sym::option_type)
2683 let method_to_use = if is_map_to_option {
2684 // `(...).map(...)` has type `impl Iterator<Item=Option<...>>
2687 // `(...).map(...)` has type `impl Iterator<Item=impl Iterator<...>>
2690 let func_snippet = snippet(cx, map_args[1].span, "..");
2691 let hint = format!(".{0}({1})", method_to_use, func_snippet);
2695 expr.span.with_lo(map_args[0].span.hi()),
2696 "called `map(..).flatten()` on an `Iterator`",
2697 &format!("try using `{}` instead", method_to_use),
2699 Applicability::MachineApplicable,
2703 // lint if caller of `.map().flatten()` is an Option
2704 if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_args[0]), sym::option_type) {
2705 let func_snippet = snippet(cx, map_args[1].span, "..");
2706 let hint = format!(".and_then({})", func_snippet);
2710 expr.span.with_lo(map_args[0].span.hi()),
2711 "called `map(..).flatten()` on an `Option`",
2712 "try using `and_then` instead",
2714 Applicability::MachineApplicable,
2719 /// lint use of `map().unwrap_or_else()` for `Option`s and `Result`s
2720 /// Return true if lint triggered
2721 fn lint_map_unwrap_or_else<'tcx>(
2722 cx: &LateContext<'tcx>,
2723 expr: &'tcx hir::Expr<'_>,
2724 map_args: &'tcx [hir::Expr<'_>],
2725 unwrap_args: &'tcx [hir::Expr<'_>],
2727 // lint if the caller of `map()` is an `Option`
2728 let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_args[0]), sym::option_type);
2729 let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_args[0]), sym::result_type);
2731 if is_option || is_result {
2732 // Don't make a suggestion that may fail to compile due to mutably borrowing
2733 // the same variable twice.
2734 let map_mutated_vars = mutated_variables(&map_args[0], cx);
2735 let unwrap_mutated_vars = mutated_variables(&unwrap_args[1], cx);
2736 if let (Some(map_mutated_vars), Some(unwrap_mutated_vars)) = (map_mutated_vars, unwrap_mutated_vars) {
2737 if map_mutated_vars.intersection(&unwrap_mutated_vars).next().is_some() {
2745 let msg = if is_option {
2746 "called `map(<f>).unwrap_or_else(<g>)` on an `Option` value. This can be done more directly by calling \
2747 `map_or_else(<g>, <f>)` instead"
2749 "called `map(<f>).unwrap_or_else(<g>)` on a `Result` value. This can be done more directly by calling \
2750 `.map_or_else(<g>, <f>)` instead"
2752 // get snippets for args to map() and unwrap_or_else()
2753 let map_snippet = snippet(cx, map_args[1].span, "..");
2754 let unwrap_snippet = snippet(cx, unwrap_args[1].span, "..");
2755 // lint, with note if neither arg is > 1 line and both map() and
2756 // unwrap_or_else() have the same span
2757 let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
2758 let same_span = map_args[1].span.ctxt() == unwrap_args[1].span.ctxt();
2759 if same_span && !multiline {
2760 let var_snippet = snippet(cx, map_args[0].span, "..");
2767 format!("{}.map_or_else({}, {})", var_snippet, unwrap_snippet, map_snippet),
2768 Applicability::MachineApplicable,
2771 } else if same_span && multiline {
2772 span_lint(cx, MAP_UNWRAP_OR, expr.span, msg);
2780 /// lint use of `_.map_or(None, _)` for `Option`s and `Result`s
2781 fn lint_map_or_none<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map_or_args: &'tcx [hir::Expr<'_>]) {
2782 let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_or_args[0]), sym::option_type);
2783 let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&map_or_args[0]), sym::result_type);
2785 // There are two variants of this `map_or` lint:
2786 // (1) using `map_or` as an adapter from `Result<T,E>` to `Option<T>`
2787 // (2) using `map_or` as a combinator instead of `and_then`
2789 // (For this lint) we don't care if any other type calls `map_or`
2790 if !is_option && !is_result {
2794 let (lint_name, msg, instead, hint) = {
2795 let default_arg_is_none = if let hir::ExprKind::Path(ref qpath) = map_or_args[1].kind {
2796 match_qpath(qpath, &paths::OPTION_NONE)
2801 if !default_arg_is_none {
2806 let f_arg_is_some = if let hir::ExprKind::Path(ref qpath) = map_or_args[2].kind {
2807 match_qpath(qpath, &paths::OPTION_SOME)
2813 let self_snippet = snippet(cx, map_or_args[0].span, "..");
2814 let func_snippet = snippet(cx, map_or_args[2].span, "..");
2815 let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
2816 `and_then(..)` instead";
2820 "try using `and_then` instead",
2821 format!("{0}.and_then({1})", self_snippet, func_snippet),
2823 } else if f_arg_is_some {
2824 let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
2826 let self_snippet = snippet(cx, map_or_args[0].span, "..");
2828 RESULT_MAP_OR_INTO_OPTION,
2830 "try using `ok` instead",
2831 format!("{0}.ok()", self_snippet),
2846 Applicability::MachineApplicable,
2850 /// lint use of `filter().next()` for `Iterators`
2851 fn lint_filter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, filter_args: &'tcx [hir::Expr<'_>]) {
2852 // lint if caller of `.filter().next()` is an Iterator
2853 if match_trait_method(cx, expr, &paths::ITERATOR) {
2854 let msg = "called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling \
2855 `.find(..)` instead.";
2856 let filter_snippet = snippet(cx, filter_args[1].span, "..");
2857 if filter_snippet.lines().count() <= 1 {
2858 let iter_snippet = snippet(cx, filter_args[0].span, "..");
2859 // add note if not multi-line
2866 format!("{}.find({})", iter_snippet, filter_snippet),
2867 Applicability::MachineApplicable,
2870 span_lint(cx, FILTER_NEXT, expr.span, msg);
2875 /// lint use of `skip_while().next()` for `Iterators`
2876 fn lint_skip_while_next<'tcx>(
2877 cx: &LateContext<'tcx>,
2878 expr: &'tcx hir::Expr<'_>,
2879 _skip_while_args: &'tcx [hir::Expr<'_>],
2881 // lint if caller of `.skip_while().next()` is an Iterator
2882 if match_trait_method(cx, expr, &paths::ITERATOR) {
2887 "called `skip_while(<p>).next()` on an `Iterator`",
2889 "this is more succinctly expressed by calling `.find(!<p>)` instead",
2894 /// lint use of `filter().map()` for `Iterators`
2895 fn lint_filter_map<'tcx>(
2896 cx: &LateContext<'tcx>,
2897 expr: &'tcx hir::Expr<'_>,
2898 _filter_args: &'tcx [hir::Expr<'_>],
2899 _map_args: &'tcx [hir::Expr<'_>],
2901 // lint if caller of `.filter().map()` is an Iterator
2902 if match_trait_method(cx, expr, &paths::ITERATOR) {
2903 let msg = "called `filter(..).map(..)` on an `Iterator`";
2904 let hint = "this is more succinctly expressed by calling `.filter_map(..)` instead";
2905 span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
2909 /// lint use of `filter_map().next()` for `Iterators`
2910 fn lint_filter_map_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, filter_args: &'tcx [hir::Expr<'_>]) {
2911 if match_trait_method(cx, expr, &paths::ITERATOR) {
2912 let msg = "called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling \
2913 `.find_map(..)` instead.";
2914 let filter_snippet = snippet(cx, filter_args[1].span, "..");
2915 if filter_snippet.lines().count() <= 1 {
2916 let iter_snippet = snippet(cx, filter_args[0].span, "..");
2923 format!("{}.find_map({})", iter_snippet, filter_snippet),
2924 Applicability::MachineApplicable,
2927 span_lint(cx, FILTER_MAP_NEXT, expr.span, msg);
2932 /// lint use of `find().map()` for `Iterators`
2933 fn lint_find_map<'tcx>(
2934 cx: &LateContext<'tcx>,
2935 expr: &'tcx hir::Expr<'_>,
2936 _find_args: &'tcx [hir::Expr<'_>],
2937 map_args: &'tcx [hir::Expr<'_>],
2939 // lint if caller of `.filter().map()` is an Iterator
2940 if match_trait_method(cx, &map_args[0], &paths::ITERATOR) {
2941 let msg = "called `find(..).map(..)` on an `Iterator`";
2942 let hint = "this is more succinctly expressed by calling `.find_map(..)` instead";
2943 span_lint_and_help(cx, FIND_MAP, expr.span, msg, None, hint);
2947 /// lint use of `filter_map().map()` for `Iterators`
2948 fn lint_filter_map_map<'tcx>(
2949 cx: &LateContext<'tcx>,
2950 expr: &'tcx hir::Expr<'_>,
2951 _filter_args: &'tcx [hir::Expr<'_>],
2952 _map_args: &'tcx [hir::Expr<'_>],
2954 // lint if caller of `.filter().map()` is an Iterator
2955 if match_trait_method(cx, expr, &paths::ITERATOR) {
2956 let msg = "called `filter_map(..).map(..)` on an `Iterator`";
2957 let hint = "this is more succinctly expressed by only calling `.filter_map(..)` instead";
2958 span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
2962 /// lint use of `filter().flat_map()` for `Iterators`
2963 fn lint_filter_flat_map<'tcx>(
2964 cx: &LateContext<'tcx>,
2965 expr: &'tcx hir::Expr<'_>,
2966 _filter_args: &'tcx [hir::Expr<'_>],
2967 _map_args: &'tcx [hir::Expr<'_>],
2969 // lint if caller of `.filter().flat_map()` is an Iterator
2970 if match_trait_method(cx, expr, &paths::ITERATOR) {
2971 let msg = "called `filter(..).flat_map(..)` on an `Iterator`";
2972 let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2973 and filtering by returning `iter::empty()`";
2974 span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
2978 /// lint use of `filter_map().flat_map()` for `Iterators`
2979 fn lint_filter_map_flat_map<'tcx>(
2980 cx: &LateContext<'tcx>,
2981 expr: &'tcx hir::Expr<'_>,
2982 _filter_args: &'tcx [hir::Expr<'_>],
2983 _map_args: &'tcx [hir::Expr<'_>],
2985 // lint if caller of `.filter_map().flat_map()` is an Iterator
2986 if match_trait_method(cx, expr, &paths::ITERATOR) {
2987 let msg = "called `filter_map(..).flat_map(..)` on an `Iterator`";
2988 let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2989 and filtering by returning `iter::empty()`";
2990 span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
2994 /// lint use of `flat_map` for `Iterators` where `flatten` would be sufficient
2995 fn lint_flat_map_identity<'tcx>(
2996 cx: &LateContext<'tcx>,
2997 expr: &'tcx hir::Expr<'_>,
2998 flat_map_args: &'tcx [hir::Expr<'_>],
2999 flat_map_span: Span,
3001 if match_trait_method(cx, expr, &paths::ITERATOR) {
3002 let arg_node = &flat_map_args[1].kind;
3004 let apply_lint = |message: &str| {
3008 flat_map_span.with_hi(expr.span.hi()),
3011 "flatten()".to_string(),
3012 Applicability::MachineApplicable,
3017 if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node;
3018 let body = cx.tcx.hir().body(*body_id);
3020 if let hir::PatKind::Binding(_, _, binding_ident, _) = body.params[0].pat.kind;
3021 if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind;
3023 if path.segments.len() == 1;
3024 if path.segments[0].ident.as_str() == binding_ident.as_str();
3027 apply_lint("called `flat_map(|x| x)` on an `Iterator`");
3032 if let hir::ExprKind::Path(ref qpath) = arg_node;
3034 if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
3037 apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
3043 /// lint searching an Iterator followed by `is_some()`
3044 /// or calling `find()` on a string followed by `is_some()`
3045 fn lint_search_is_some<'tcx>(
3046 cx: &LateContext<'tcx>,
3047 expr: &'tcx hir::Expr<'_>,
3048 search_method: &str,
3049 search_args: &'tcx [hir::Expr<'_>],
3050 is_some_args: &'tcx [hir::Expr<'_>],
3053 // lint if caller of search is an Iterator
3054 if match_trait_method(cx, &is_some_args[0], &paths::ITERATOR) {
3056 "called `is_some()` after searching an `Iterator` with `{}`",
3059 let hint = "this is more succinctly expressed by calling `any()`";
3060 let search_snippet = snippet(cx, search_args[1].span, "..");
3061 if search_snippet.lines().count() <= 1 {
3062 // suggest `any(|x| ..)` instead of `any(|&x| ..)` for `find(|&x| ..).is_some()`
3063 // suggest `any(|..| *..)` instead of `any(|..| **..)` for `find(|..| **..).is_some()`
3064 let any_search_snippet = if_chain! {
3065 if search_method == "find";
3066 if let hir::ExprKind::Closure(_, _, body_id, ..) = search_args[1].kind;
3067 let closure_body = cx.tcx.hir().body(body_id);
3068 if let Some(closure_arg) = closure_body.params.get(0);
3070 if let hir::PatKind::Ref(..) = closure_arg.pat.kind {
3071 Some(search_snippet.replacen('&', "", 1))
3072 } else if let Some(name) = get_arg_name(&closure_arg.pat) {
3073 Some(search_snippet.replace(&format!("*{}", name), &name.as_str()))
3081 // add note if not multi-line
3085 method_span.with_hi(expr.span.hi()),
3087 "use `any()` instead",
3090 any_search_snippet.as_ref().map_or(&*search_snippet, String::as_str)
3092 Applicability::MachineApplicable,
3095 span_lint_and_help(cx, SEARCH_IS_SOME, expr.span, &msg, None, hint);
3098 // lint if `find()` is called by `String` or `&str`
3099 else if search_method == "find" {
3100 let is_string_or_str_slice = |e| {
3101 let self_ty = cx.typeck_results().expr_ty(e).peel_refs();
3102 if is_type_diagnostic_item(cx, self_ty, sym!(string_type)) {
3105 *self_ty.kind() == ty::Str
3109 if is_string_or_str_slice(&search_args[0]);
3110 if is_string_or_str_slice(&search_args[1]);
3112 let msg = "called `is_some()` after calling `find()` on a string";
3113 let mut applicability = Applicability::MachineApplicable;
3114 let find_arg = snippet_with_applicability(cx, search_args[1].span, "..", &mut applicability);
3118 method_span.with_hi(expr.span.hi()),
3120 "use `contains()` instead",
3121 format!("contains({})", find_arg),
3129 /// Used for `lint_binary_expr_with_method_call`.
3130 #[derive(Copy, Clone)]
3131 struct BinaryExprInfo<'a> {
3132 expr: &'a hir::Expr<'a>,
3133 chain: &'a hir::Expr<'a>,
3134 other: &'a hir::Expr<'a>,
3138 /// Checks for the `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
3139 fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExprInfo<'_>) {
3140 macro_rules! lint_with_both_lhs_and_rhs {
3141 ($func:ident, $cx:expr, $info:ident) => {
3142 if !$func($cx, $info) {
3143 ::std::mem::swap(&mut $info.chain, &mut $info.other);
3144 if $func($cx, $info) {
3151 lint_with_both_lhs_and_rhs!(lint_chars_next_cmp, cx, info);
3152 lint_with_both_lhs_and_rhs!(lint_chars_last_cmp, cx, info);
3153 lint_with_both_lhs_and_rhs!(lint_chars_next_cmp_with_unwrap, cx, info);
3154 lint_with_both_lhs_and_rhs!(lint_chars_last_cmp_with_unwrap, cx, info);
3157 /// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints.
3159 cx: &LateContext<'_>,
3160 info: &BinaryExprInfo<'_>,
3161 chain_methods: &[&str],
3162 lint: &'static Lint,
3166 if let Some(args) = method_chain_args(info.chain, chain_methods);
3167 if let hir::ExprKind::Call(ref fun, ref arg_char) = info.other.kind;
3168 if arg_char.len() == 1;
3169 if let hir::ExprKind::Path(ref qpath) = fun.kind;
3170 if let Some(segment) = single_segment_path(qpath);
3171 if segment.ident.name == sym::Some;
3173 let mut applicability = Applicability::MachineApplicable;
3174 let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0][0]).peel_refs();
3176 if *self_ty.kind() != ty::Str {
3184 &format!("you should use the `{}` method", suggest),
3186 format!("{}{}.{}({})",
3187 if info.eq { "" } else { "!" },
3188 snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
3190 snippet_with_applicability(cx, arg_char[0].span, "..", &mut applicability)),
3201 /// Checks for the `CHARS_NEXT_CMP` lint.
3202 fn lint_chars_next_cmp<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
3203 lint_chars_cmp(cx, info, &["chars", "next"], CHARS_NEXT_CMP, "starts_with")
3206 /// Checks for the `CHARS_LAST_CMP` lint.
3207 fn lint_chars_last_cmp<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
3208 if lint_chars_cmp(cx, info, &["chars", "last"], CHARS_LAST_CMP, "ends_with") {
3211 lint_chars_cmp(cx, info, &["chars", "next_back"], CHARS_LAST_CMP, "ends_with")
3215 /// Wrapper fn for `CHARS_NEXT_CMP` and `CHARS_LAST_CMP` lints with `unwrap()`.
3216 fn lint_chars_cmp_with_unwrap<'tcx>(
3217 cx: &LateContext<'tcx>,
3218 info: &BinaryExprInfo<'_>,
3219 chain_methods: &[&str],
3220 lint: &'static Lint,
3224 if let Some(args) = method_chain_args(info.chain, chain_methods);
3225 if let hir::ExprKind::Lit(ref lit) = info.other.kind;
3226 if let ast::LitKind::Char(c) = lit.node;
3228 let mut applicability = Applicability::MachineApplicable;
3233 &format!("you should use the `{}` method", suggest),
3235 format!("{}{}.{}('{}')",
3236 if info.eq { "" } else { "!" },
3237 snippet_with_applicability(cx, args[0][0].span, "..", &mut applicability),
3250 /// Checks for the `CHARS_NEXT_CMP` lint with `unwrap()`.
3251 fn lint_chars_next_cmp_with_unwrap<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
3252 lint_chars_cmp_with_unwrap(cx, info, &["chars", "next", "unwrap"], CHARS_NEXT_CMP, "starts_with")
3255 /// Checks for the `CHARS_LAST_CMP` lint with `unwrap()`.
3256 fn lint_chars_last_cmp_with_unwrap<'tcx>(cx: &LateContext<'tcx>, info: &BinaryExprInfo<'_>) -> bool {
3257 if lint_chars_cmp_with_unwrap(cx, info, &["chars", "last", "unwrap"], CHARS_LAST_CMP, "ends_with") {
3260 lint_chars_cmp_with_unwrap(cx, info, &["chars", "next_back", "unwrap"], CHARS_LAST_CMP, "ends_with")
3264 fn get_hint_if_single_char_arg(
3265 cx: &LateContext<'_>,
3266 arg: &hir::Expr<'_>,
3267 applicability: &mut Applicability,
3268 ) -> Option<String> {
3270 if let hir::ExprKind::Lit(lit) = &arg.kind;
3271 if let ast::LitKind::Str(r, style) = lit.node;
3272 let string = r.as_str();
3273 if string.chars().count() == 1;
3275 let snip = snippet_with_applicability(cx, arg.span, &string, applicability);
3276 let ch = if let ast::StrStyle::Raw(nhash) = style {
3277 let nhash = nhash as usize;
3278 // for raw string: r##"a"##
3279 &snip[(nhash + 2)..(snip.len() - 1 - nhash)]
3281 // for regular string: "a"
3282 &snip[1..(snip.len() - 1)]
3284 let hint = format!("'{}'", if ch == "'" { "\\'" } else { ch });
3292 /// lint for length-1 `str`s for methods in `PATTERN_METHODS`
3293 fn lint_single_char_pattern(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
3294 let mut applicability = Applicability::MachineApplicable;
3295 if let Some(hint) = get_hint_if_single_char_arg(cx, arg, &mut applicability) {
3298 SINGLE_CHAR_PATTERN,
3300 "single-character string constant used as pattern",
3301 "try using a `char` instead",
3308 /// lint for length-1 `str`s as argument for `push_str`
3309 fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
3310 let mut applicability = Applicability::MachineApplicable;
3311 if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[1], &mut applicability) {
3312 let base_string_snippet =
3313 snippet_with_applicability(cx, args[0].span.source_callsite(), "..", &mut applicability);
3314 let sugg = format!("{}.push({})", base_string_snippet, extension_string);
3317 SINGLE_CHAR_ADD_STR,
3319 "calling `push_str()` using a single-character string literal",
3320 "consider using `push` with a character literal",
3327 /// lint for length-1 `str`s as argument for `insert_str`
3328 fn lint_single_char_insert_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
3329 let mut applicability = Applicability::MachineApplicable;
3330 if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[2], &mut applicability) {
3331 let base_string_snippet =
3332 snippet_with_applicability(cx, args[0].span.source_callsite(), "_", &mut applicability);
3333 let pos_arg = snippet_with_applicability(cx, args[1].span, "..", &mut applicability);
3334 let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
3337 SINGLE_CHAR_ADD_STR,
3339 "calling `insert_str()` using a single-character string literal",
3340 "consider using `insert` with a character literal",
3347 /// Checks for the `USELESS_ASREF` lint.
3348 fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_ref_args: &[hir::Expr<'_>]) {
3349 // when we get here, we've already checked that the call name is "as_ref" or "as_mut"
3350 // check if the call is to the actual `AsRef` or `AsMut` trait
3351 if match_trait_method(cx, expr, &paths::ASREF_TRAIT) || match_trait_method(cx, expr, &paths::ASMUT_TRAIT) {
3352 // check if the type after `as_ref` or `as_mut` is the same as before
3353 let recvr = &as_ref_args[0];
3354 let rcv_ty = cx.typeck_results().expr_ty(recvr);
3355 let res_ty = cx.typeck_results().expr_ty(expr);
3356 let (base_res_ty, res_depth) = walk_ptrs_ty_depth(res_ty);
3357 let (base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth(rcv_ty);
3358 if base_rcv_ty == base_res_ty && rcv_depth >= res_depth {
3359 // allow the `as_ref` or `as_mut` if it is followed by another method call
3361 if let Some(parent) = get_parent_expr(cx, expr);
3362 if let hir::ExprKind::MethodCall(_, ref span, _, _) = parent.kind;
3363 if span != &expr.span;
3369 let mut applicability = Applicability::MachineApplicable;
3374 &format!("this call to `{}` does nothing", call_name),
3376 snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
3383 fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> {
3384 has_iter_method(cx, self_ref_ty).map(|ty_name| {
3385 let mutbl = match self_ref_ty.kind() {
3386 ty::Ref(_, _, mutbl) => mutbl,
3387 _ => unreachable!(),
3389 let method_name = match mutbl {
3390 hir::Mutability::Not => "iter",
3391 hir::Mutability::Mut => "iter_mut",
3393 (ty_name, method_name)
3397 fn lint_into_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_>, method_span: Span) {
3398 if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) {
3401 if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
3407 "this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`",
3411 method_name.to_string(),
3412 Applicability::MachineApplicable,
3417 /// lint for `MaybeUninit::uninit().assume_init()` (we already have the latter)
3418 fn lint_maybe_uninit(cx: &LateContext<'_>, expr: &hir::Expr<'_>, outer: &hir::Expr<'_>) {
3420 if let hir::ExprKind::Call(ref callee, ref args) = expr.kind;
3422 if let hir::ExprKind::Path(ref path) = callee.kind;
3423 if match_qpath(path, &paths::MEM_MAYBEUNINIT_UNINIT);
3424 if !is_maybe_uninit_ty_valid(cx, cx.typeck_results().expr_ty_adjusted(outer));
3428 UNINIT_ASSUMED_INIT,
3430 "this call for this type may be undefined behavior"
3436 fn is_maybe_uninit_ty_valid(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
3438 ty::Array(ref component, _) => is_maybe_uninit_ty_valid(cx, component),
3439 ty::Tuple(ref types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
3440 ty::Adt(ref adt, _) => match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT),
3445 fn lint_suspicious_map(cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
3450 "this call to `map()` won't have an effect on the call to `count()`",
3452 "make sure you did not confuse `map` with `filter` or `for_each`",
3456 /// lint use of `_.as_ref().map(Deref::deref)` for `Option`s
3457 fn lint_option_as_ref_deref<'tcx>(
3458 cx: &LateContext<'tcx>,
3459 expr: &hir::Expr<'_>,
3460 as_ref_args: &[hir::Expr<'_>],
3461 map_args: &[hir::Expr<'_>],
3464 let same_mutability = |m| (is_mut && m == &hir::Mutability::Mut) || (!is_mut && m == &hir::Mutability::Not);
3466 let option_ty = cx.typeck_results().expr_ty(&as_ref_args[0]);
3467 if !is_type_diagnostic_item(cx, option_ty, sym::option_type) {
3471 let deref_aliases: [&[&str]; 9] = [
3472 &paths::DEREF_TRAIT_METHOD,
3473 &paths::DEREF_MUT_TRAIT_METHOD,
3474 &paths::CSTRING_AS_C_STR,
3475 &paths::OS_STRING_AS_OS_STR,
3476 &paths::PATH_BUF_AS_PATH,
3477 &paths::STRING_AS_STR,
3478 &paths::STRING_AS_MUT_STR,
3479 &paths::VEC_AS_SLICE,
3480 &paths::VEC_AS_MUT_SLICE,
3483 let is_deref = match map_args[1].kind {
3484 hir::ExprKind::Path(ref expr_qpath) => cx
3485 .qpath_res(expr_qpath, map_args[1].hir_id)
3487 .map_or(false, |fun_def_id| {
3488 deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
3490 hir::ExprKind::Closure(_, _, body_id, _, _) => {
3491 let closure_body = cx.tcx.hir().body(body_id);
3492 let closure_expr = remove_blocks(&closure_body.value);
3494 match &closure_expr.kind {
3495 hir::ExprKind::MethodCall(_, _, args, _) => {
3498 if let hir::ExprKind::Path(qpath) = &args[0].kind;
3499 if let hir::def::Res::Local(local_id) = cx.qpath_res(qpath, args[0].hir_id);
3500 if closure_body.params[0].pat.hir_id == local_id;
3503 .expr_adjustments(&args[0])
3506 .collect::<Box<[_]>>();
3507 if let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj;
3509 let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
3510 deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
3516 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, m, ref inner) if same_mutability(m) => {
3518 if let hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner1) = inner.kind;
3519 if let hir::ExprKind::Unary(hir::UnOp::UnDeref, ref inner2) = inner1.kind;
3520 if let hir::ExprKind::Path(ref qpath) = inner2.kind;
3521 if let hir::def::Res::Local(local_id) = cx.qpath_res(qpath, inner2.hir_id);
3523 closure_body.params[0].pat.hir_id == local_id
3536 let current_method = if is_mut {
3537 format!(".as_mut().map({})", snippet(cx, map_args[1].span, ".."))
3539 format!(".as_ref().map({})", snippet(cx, map_args[1].span, ".."))
3541 let method_hint = if is_mut { "as_deref_mut" } else { "as_deref" };
3542 let hint = format!("{}.{}()", snippet(cx, as_ref_args[0].span, ".."), method_hint);
3543 let suggestion = format!("try using {} instead", method_hint);
3546 "called `{0}` on an Option value. This can be done more directly \
3547 by calling `{1}` instead",
3548 current_method, hint
3552 OPTION_AS_REF_DEREF,
3557 Applicability::MachineApplicable,
3562 fn lint_map_collect(
3563 cx: &LateContext<'_>,
3564 expr: &hir::Expr<'_>,
3565 map_args: &[hir::Expr<'_>],
3566 collect_args: &[hir::Expr<'_>],
3569 // called on Iterator
3570 if let [map_expr] = collect_args;
3571 if match_trait_method(cx, map_expr, &paths::ITERATOR);
3572 // return of collect `Result<(),_>`
3573 let collect_ret_ty = cx.typeck_results().expr_ty(expr);
3574 if is_type_diagnostic_item(cx, collect_ret_ty, sym::result_type);
3575 if let ty::Adt(_, substs) = collect_ret_ty.kind();
3576 if let Some(result_t) = substs.types().next();
3577 if result_t.is_unit();
3578 // get parts for snippet
3579 if let [iter, map_fn] = map_args;
3583 MAP_COLLECT_RESULT_UNIT,
3585 "`.map().collect()` can be replaced with `.try_for_each()`",
3588 "{}.try_for_each({})",
3589 snippet(cx, iter.span, ".."),
3590 snippet(cx, map_fn.span, "..")
3592 Applicability::MachineApplicable,
3598 /// Given a `Result<T, E>` type, return its error type (`E`).
3599 fn get_error_type<'a>(cx: &LateContext<'_>, ty: Ty<'a>) -> Option<Ty<'a>> {
3601 ty::Adt(_, substs) if is_type_diagnostic_item(cx, ty, sym::result_type) => substs.types().nth(1),
3606 /// This checks whether a given type is known to implement Debug.
3607 fn has_debug_impl<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {
3609 .get_diagnostic_item(sym::debug_trait)
3610 .map_or(false, |debug| implements_trait(cx, ty, debug, &[]))
3615 StartsWith(&'static str),
3619 const CONVENTIONS: [(Convention, &[SelfKind]); 7] = [
3620 (Convention::Eq("new"), &[SelfKind::No]),
3621 (Convention::StartsWith("as_"), &[SelfKind::Ref, SelfKind::RefMut]),
3622 (Convention::StartsWith("from_"), &[SelfKind::No]),
3623 (Convention::StartsWith("into_"), &[SelfKind::Value]),
3624 (Convention::StartsWith("is_"), &[SelfKind::Ref, SelfKind::No]),
3625 (Convention::Eq("to_mut"), &[SelfKind::RefMut]),
3626 (Convention::StartsWith("to_"), &[SelfKind::Ref]),
3629 const FN_HEADER: hir::FnHeader = hir::FnHeader {
3630 unsafety: hir::Unsafety::Normal,
3631 constness: hir::Constness::NotConst,
3632 asyncness: hir::IsAsync::NotAsync,
3633 abi: rustc_target::spec::abi::Abi::Rust,
3636 struct ShouldImplTraitCase {
3637 trait_name: &'static str,
3638 method_name: &'static str,
3640 fn_header: hir::FnHeader,
3641 // implicit self kind expected (none, self, &self, ...)
3642 self_kind: SelfKind,
3643 // checks against the output type
3644 output_type: OutType,
3645 // certain methods with explicit lifetimes can't implement the equivalent trait method
3646 lint_explicit_lifetime: bool,
3648 impl ShouldImplTraitCase {
3650 trait_name: &'static str,
3651 method_name: &'static str,
3653 fn_header: hir::FnHeader,
3654 self_kind: SelfKind,
3655 output_type: OutType,
3656 lint_explicit_lifetime: bool,
3657 ) -> ShouldImplTraitCase {
3658 ShouldImplTraitCase {
3665 lint_explicit_lifetime,
3669 fn lifetime_param_cond(&self, impl_item: &hir::ImplItem<'_>) -> bool {
3670 self.lint_explicit_lifetime
3671 || !impl_item.generics.params.iter().any(|p| {
3674 hir::GenericParamKind::Lifetime {
3675 kind: hir::LifetimeParamKind::Explicit
3683 const TRAIT_METHODS: [ShouldImplTraitCase; 30] = [
3684 ShouldImplTraitCase::new("std::ops::Add", "add", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3685 ShouldImplTraitCase::new("std::convert::AsMut", "as_mut", 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true),
3686 ShouldImplTraitCase::new("std::convert::AsRef", "as_ref", 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true),
3687 ShouldImplTraitCase::new("std::ops::BitAnd", "bitand", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3688 ShouldImplTraitCase::new("std::ops::BitOr", "bitor", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3689 ShouldImplTraitCase::new("std::ops::BitXor", "bitxor", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3690 ShouldImplTraitCase::new("std::borrow::Borrow", "borrow", 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true),
3691 ShouldImplTraitCase::new("std::borrow::BorrowMut", "borrow_mut", 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true),
3692 ShouldImplTraitCase::new("std::clone::Clone", "clone", 1, FN_HEADER, SelfKind::Ref, OutType::Any, true),
3693 ShouldImplTraitCase::new("std::cmp::Ord", "cmp", 2, FN_HEADER, SelfKind::Ref, OutType::Any, true),
3694 // FIXME: default doesn't work
3695 ShouldImplTraitCase::new("std::default::Default", "default", 0, FN_HEADER, SelfKind::No, OutType::Any, true),
3696 ShouldImplTraitCase::new("std::ops::Deref", "deref", 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true),
3697 ShouldImplTraitCase::new("std::ops::DerefMut", "deref_mut", 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true),
3698 ShouldImplTraitCase::new("std::ops::Div", "div", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3699 ShouldImplTraitCase::new("std::ops::Drop", "drop", 1, FN_HEADER, SelfKind::RefMut, OutType::Unit, true),
3700 ShouldImplTraitCase::new("std::cmp::PartialEq", "eq", 2, FN_HEADER, SelfKind::Ref, OutType::Bool, true),
3701 ShouldImplTraitCase::new("std::iter::FromIterator", "from_iter", 1, FN_HEADER, SelfKind::No, OutType::Any, true),
3702 ShouldImplTraitCase::new("std::str::FromStr", "from_str", 1, FN_HEADER, SelfKind::No, OutType::Any, true),
3703 ShouldImplTraitCase::new("std::hash::Hash", "hash", 2, FN_HEADER, SelfKind::Ref, OutType::Unit, true),
3704 ShouldImplTraitCase::new("std::ops::Index", "index", 2, FN_HEADER, SelfKind::Ref, OutType::Ref, true),
3705 ShouldImplTraitCase::new("std::ops::IndexMut", "index_mut", 2, FN_HEADER, SelfKind::RefMut, OutType::Ref, true),
3706 ShouldImplTraitCase::new("std::iter::IntoIterator", "into_iter", 1, FN_HEADER, SelfKind::Value, OutType::Any, true),
3707 ShouldImplTraitCase::new("std::ops::Mul", "mul", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3708 ShouldImplTraitCase::new("std::ops::Neg", "neg", 1, FN_HEADER, SelfKind::Value, OutType::Any, true),
3709 ShouldImplTraitCase::new("std::iter::Iterator", "next", 1, FN_HEADER, SelfKind::RefMut, OutType::Any, false),
3710 ShouldImplTraitCase::new("std::ops::Not", "not", 1, FN_HEADER, SelfKind::Value, OutType::Any, true),
3711 ShouldImplTraitCase::new("std::ops::Rem", "rem", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3712 ShouldImplTraitCase::new("std::ops::Shl", "shl", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3713 ShouldImplTraitCase::new("std::ops::Shr", "shr", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3714 ShouldImplTraitCase::new("std::ops::Sub", "sub", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
3718 const PATTERN_METHODS: [(&str, usize); 17] = [
3726 ("split_terminator", 1),
3727 ("rsplit_terminator", 1),
3732 ("match_indices", 1),
3733 ("rmatch_indices", 1),
3734 ("trim_start_matches", 1),
3735 ("trim_end_matches", 1),
3738 #[derive(Clone, Copy, PartialEq, Debug)]
3747 fn matches<'a>(self, cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
3748 fn matches_value<'a>(cx: &LateContext<'a>, parent_ty: Ty<'_>, ty: Ty<'_>) -> bool {
3749 if ty == parent_ty {
3751 } else if ty.is_box() {
3752 ty.boxed_ty() == parent_ty
3753 } else if is_type_diagnostic_item(cx, ty, sym::Rc) || is_type_diagnostic_item(cx, ty, sym::Arc) {
3754 if let ty::Adt(_, substs) = ty.kind() {
3755 substs.types().next().map_or(false, |t| t == parent_ty)
3764 fn matches_ref<'a>(cx: &LateContext<'a>, mutability: hir::Mutability, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
3765 if let ty::Ref(_, t, m) = *ty.kind() {
3766 return m == mutability && t == parent_ty;
3769 let trait_path = match mutability {
3770 hir::Mutability::Not => &paths::ASREF_TRAIT,
3771 hir::Mutability::Mut => &paths::ASMUT_TRAIT,
3774 let trait_def_id = match get_trait_def_id(cx, trait_path) {
3776 None => return false,
3778 implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
3782 Self::Value => matches_value(cx, parent_ty, ty),
3783 Self::Ref => matches_ref(cx, hir::Mutability::Not, parent_ty, ty) || ty == parent_ty && is_copy(cx, ty),
3784 Self::RefMut => matches_ref(cx, hir::Mutability::Mut, parent_ty, ty),
3785 Self::No => ty != parent_ty,
3790 fn description(self) -> &'static str {
3792 Self::Value => "self by value",
3793 Self::Ref => "self by reference",
3794 Self::RefMut => "self by mutable reference",
3795 Self::No => "no self",
3802 fn check(&self, other: &str) -> bool {
3804 Self::Eq(this) => this == other,
3805 Self::StartsWith(this) => other.starts_with(this) && this != other,
3810 impl fmt::Display for Convention {
3811 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
3813 Self::Eq(this) => this.fmt(f),
3814 Self::StartsWith(this) => this.fmt(f).and_then(|_| '*'.fmt(f)),
3819 #[derive(Clone, Copy)]
3828 fn matches(self, cx: &LateContext<'_>, ty: &hir::FnRetTy<'_>) -> bool {
3829 let is_unit = |ty: &hir::Ty<'_>| SpanlessEq::new(cx).eq_ty_kind(&ty.kind, &hir::TyKind::Tup(&[]));
3831 (Self::Unit, &hir::FnRetTy::DefaultReturn(_)) => true,
3832 (Self::Unit, &hir::FnRetTy::Return(ref ty)) if is_unit(ty) => true,
3833 (Self::Bool, &hir::FnRetTy::Return(ref ty)) if is_bool(ty) => true,
3834 (Self::Any, &hir::FnRetTy::Return(ref ty)) if !is_unit(ty) => true,
3835 (Self::Ref, &hir::FnRetTy::Return(ref ty)) => matches!(ty.kind, hir::TyKind::Rptr(_, _)),
3841 fn is_bool(ty: &hir::Ty<'_>) -> bool {
3842 if let hir::TyKind::Path(ref p) = ty.kind {
3843 match_qpath(p, &["bool"])
3849 // Returns `true` if `expr` contains a return expression
3850 fn contains_return(expr: &hir::Expr<'_>) -> bool {
3851 struct RetCallFinder {
3855 impl<'tcx> intravisit::Visitor<'tcx> for RetCallFinder {
3856 type Map = Map<'tcx>;
3858 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
3862 if let hir::ExprKind::Ret(..) = &expr.kind {
3865 intravisit::walk_expr(self, expr);
3869 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
3870 intravisit::NestedVisitorMap::None
3874 let mut visitor = RetCallFinder { found: false };
3875 visitor.visit_expr(expr);
3879 fn check_pointer_offset(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
3882 if let ty::RawPtr(ty::TypeAndMut { ref ty, .. }) = cx.typeck_results().expr_ty(&args[0]).kind();
3883 if let Ok(layout) = cx.tcx.layout_of(cx.param_env.and(ty));
3886 span_lint(cx, ZST_OFFSET, expr.span, "offset calculation on zero-sized value");
3891 fn lint_filetype_is_file(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
3892 let ty = cx.typeck_results().expr_ty(&args[0]);
3894 if !match_type(cx, ty, &paths::FILE_TYPE) {
3900 let lint_unary: &str;
3901 let help_unary: &str;
3903 if let Some(parent) = get_parent_expr(cx, expr);
3904 if let hir::ExprKind::Unary(op, _) = parent.kind;
3905 if op == hir::UnOp::UnNot;
3918 let lint_msg = format!("`{}FileType::is_file()` only {} regular files", lint_unary, verb);
3919 let help_msg = format!("use `{}FileType::is_dir()` instead", help_unary);
3920 span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg);
3923 fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
3924 let ty = cx.typeck_results().expr_ty(expr);
3925 let arg_ty = cx.typeck_results().expr_ty(&args[0]);
3928 if let Some(from_iter_id) = get_trait_def_id(cx, &paths::FROM_ITERATOR);
3929 if let Some(iter_id) = get_trait_def_id(cx, &paths::ITERATOR);
3931 if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]);
3933 // `expr` implements `FromIterator` trait
3934 let iter_expr = snippet(cx, args[0].span, "..");
3937 FROM_ITER_INSTEAD_OF_COLLECT,
3939 "usage of `FromIterator::from_iter`",
3940 "use `.collect()` instead of `::from_iter()`",
3941 format!("{}.collect()", iter_expr),
3942 Applicability::MaybeIncorrect,
3948 fn fn_header_equals(expected: hir::FnHeader, actual: hir::FnHeader) -> bool {
3949 expected.constness == actual.constness
3950 && expected.unsafety == actual.unsafety
3951 && expected.asyncness == actual.asyncness