|
| 1 | +mod append_instead_of_extend; |
1 | 2 | mod bind_instead_of_map; |
2 | 3 | mod bytes_nth; |
3 | 4 | mod chars_cmp; |
@@ -1032,6 +1033,30 @@ declare_clippy_lint! { |
1032 | 1033 | "using `.get().unwrap()` or `.get_mut().unwrap()` when using `[]` would work instead" |
1033 | 1034 | } |
1034 | 1035 |
|
| 1036 | +declare_clippy_lint! { |
| 1037 | + /// **What it does:** Checks for occurrences where one vector gets extended instead of append |
| 1038 | + /// |
| 1039 | + /// **Why is this bad?** Using `append` instead of `extend` is more concise and faster |
| 1040 | + /// |
| 1041 | + /// **Known problems:** None. |
| 1042 | + /// |
| 1043 | + /// **Example:** |
| 1044 | + /// |
| 1045 | + /// ```rust |
| 1046 | + /// let mut a = vec![1, 2, 3]; |
| 1047 | + /// let mut b = vec![4, 5, 6]; |
| 1048 | + /// |
| 1049 | + /// // Bad |
| 1050 | + /// a.extend(b.drain(..)); |
| 1051 | + /// |
| 1052 | + /// // Good |
| 1053 | + /// a.append(&mut b); |
| 1054 | + /// ``` |
| 1055 | + pub APPEND_INSTEAD_OF_EXTEND, |
| 1056 | + perf, |
| 1057 | + "using vec.append(&mut vec) to move the full range of a vecor to another" |
| 1058 | +} |
| 1059 | + |
1035 | 1060 | declare_clippy_lint! { |
1036 | 1061 | /// **What it does:** Checks for the use of `.extend(s.chars())` where s is a |
1037 | 1062 | /// `&str` or `String`. |
@@ -1785,7 +1810,8 @@ impl_lint_pass!(Methods => [ |
1785 | 1810 | INSPECT_FOR_EACH, |
1786 | 1811 | IMPLICIT_CLONE, |
1787 | 1812 | SUSPICIOUS_SPLITN, |
1788 | | - MANUAL_STR_REPEAT |
| 1813 | + MANUAL_STR_REPEAT, |
| 1814 | + APPEND_INSTEAD_OF_EXTEND |
1789 | 1815 | ]); |
1790 | 1816 |
|
1791 | 1817 | /// Extracts a method call name, args, and `Span` of the method name. |
@@ -2047,7 +2073,10 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio |
2047 | 2073 | Some(("ok", [recv], _)) => ok_expect::check(cx, expr, recv), |
2048 | 2074 | _ => expect_used::check(cx, expr, recv), |
2049 | 2075 | }, |
2050 | | - ("extend", [arg]) => string_extend_chars::check(cx, expr, recv, arg), |
| 2076 | + ("extend", [arg]) => { |
| 2077 | + string_extend_chars::check(cx, expr, recv, arg); |
| 2078 | + append_instead_of_extend::check(cx, expr, recv, arg); |
| 2079 | + }, |
2051 | 2080 | ("filter_map", [arg]) => { |
2052 | 2081 | unnecessary_filter_map::check(cx, expr, arg); |
2053 | 2082 | filter_map_identity::check(cx, expr, arg, span); |
|
0 commit comments